我發瘋。我可以看到沒有理由爲什麼以下不起作用。它總是返回值的地址,而不是值本身。指針不工作返回地址不珍惜
我曾經嘗試都十幾個不同的方式初始化指針和引用指針無濟於事。
初始化和循環
//INIT the Motors
//Motor MOTORNAME(PWM_SHIELD_POSITION, POINTER TO PWM SHIELD)
Motor LFUpper;
Motor* ptr1 = &LFUpper;
Motor LFLower;
Motor* ptr2 = &LFUpper;
Motor RFUpper;
Motor* ptr3 = &LFUpper;
Motor RFLower;
Motor* ptr4 = &RFLower;
Motor BRUpper;
Motor* ptr5 = &BRUpper
Motor BRLower;
Motor* ptr6 = &BRLower
Motor BLUpper;
Motor* ptr7 = &BLUpper
Motor BLLower;
Motor* ptr8 = &BLLower
//Init The Menu System
Menu MenuSys(ptr1, ptr2, ptr3, ptr4, ptr5, ptr6, ptr7, ptr8);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(9600);
Serial.print("HELLO");
ptr1->INIT(FRONT_UPPER_LEFT_MOTOR);
ptr1->INIT(FRONT_LOWER_LEFT_MOTOR);
ptr1->INIT(FRONT_UPPER_RIGHT_MOTOR);
ptr1->INIT(FRONT_LOWER_RIGHT_MOTOR);
ptr1->INIT(BACK_UPPER_RIGHT_MOTOR);
ptr1->INIT(BACK_LOWER_RIGHT_MOTOR);
ptr1->INIT(BACK_UPPER_LEFT_MOTOR);
ptr1->INIT(BACK_LOWER_LEFT_MOTOR);
}
void loop() {
// put your main code here, to run repeatedly:
// Poll The Menu For Changes
MenuSys.MenuPoll();
}
菜單句柄
Menu::Menu(Motor *LFUpper1, Motor *LFLower1, Motor *RFUpper1, Motor *RFLower1, Motor *RBUpper1, Motor *RBLower1, Motor *LBUpper1, Motor *LBLower1)
{
LFUpper = LFUpper1;
LFLower = LFLower1;
RFUpper = RFUpper1;
RFLower = RFLower1;
LBUpper = LBUpper1;
LBLower = LBLower1;
RBUpper = RBUpper1;
RBLower = RBLower1;
}
void Menu::MenuPoll()
{
if (Serial2.available())
{
String readVal;
int rw = 0;
readVal = Serial2.readString();
Serial.println(readVal);
if (readVal.startsWith("W"))
{
rw = 1;
}
else
{
rw = 0;
}
int MenuCommand = readVal.substring(1, 4).toInt();
int Value = readVal.substring(4, readVal.length()).toInt();
MenuHandler(rw, MenuCommand, Value);
}
}
void Menu::MenuHandler(int rw, int MenuCommand, int Value)
{
Serial.println(rw);
Serial.println(MenuCommand);
Serial.println(Value);
switch (MenuCommand) {
case 111:
if (rw == 1)
{
Serial.print(LFUpper->pwmMIN);
LFUpper->pwmMIN = Value;
}
線Serial.print(LFUpper->pwmMIN);
不打印正確的值
它打印什麼值,你期望什麼值 – chbchb55