do //this cycle waits until someone click the ON button on the app{ if(Serial.available()>0) { state=Serial.read(); Serial.println(readString); //prints string to serial port out } }while(state!='V'); //system ON commanddigitalWrite(8, LOW); do //this cycle waits that the user push either Home Button or OFF button { if(Serial.available()>0) { state=Serial.read(); Serial.println(readString); //prints string to serial port out } } while((state!='O')&&(state!='Z'));
2)-你还记得在应用程序中我们曾经发送命令'd'来向后发送马达吗?-你还记得我们把Y限位开关连接到插脚10和GND?最初,我们检查Y电机是否能够实际后退:如果按下引脚10上的限位开关,Arduino将读取GND,即使在应用程序上按下后退按钮,也不会发生任何事情。如果不按下限位开关,我们只需用简单的步进指令将电机旋转72°。旋转(72)并减小1的y变量。if(limitYstate==HIGH) { if(c == 'B') //Y motor backward { stepperY.rotate(72); //the value is not negative because I mounted the motors upside-down in my assembly, sorry delay(5); Y--; Serial.println("Y value, "); Serial.println(Y); Serial.println("Y motor backward"); readString=""; } }3)这是一个难点,基本上我们在这里捕获用户拖动应用程序上的麦克风图标时发送的字符串,然后我们解析这些数据。例如,也就是说,我们将子字符串转换为整数:x=“17”变成xm=17y=“24”变为ym=24在这个过程中,我们将每个字符串加载到缓冲区数组中,最后使用atoi()命令获取整数值。你还记得拖动麦克风徽标时应用程序中的弹跳问题吗?好吧,电机不会移动,直到在序列上读到字符“u”,然后电机才会根据最后的xm和ym值旋转。
if (c == ';') // character ; means that the user has dragged the microphone icon on the app { Serial.println(); Serial.print("captured String is : "); //we have captured the string with the X Y Z values, now we need to parse it and elaborate it, the following commands are for parsing Serial.println(readString); //prints string to serial port out, used for debugging ind1 = readString.indexOf('.'); //finds location of . ind2 = readString.indexOf(','); //finds location of , XString = readString.substring(ind1+1, ind2); //captures the string between charactes . and , ind3 = readString.indexOf('*'); //find location of * which delimits the end string YString = readString.substring(ind2+1, ind3); //captures the string between charactes , and * char buf1[XString.length()+1]; //create a characters array long as substring X XString.toCharArray(buf1, XString.length()+1); //load string X in the array buf1 char buf2[YString.length()+1]; //create a characters array long as substring Y YString.toCharArray(buf2, YString.length()+1); //load the string Y in array buf2 Xm = atoi(buf1); //convert the string in arra buf 1, which is a character with the value of X in an integer Ym = atoi(buf2); //convert the string in arra buf 2, which is a character with the value of Y in an integer readString=""; //clears variables for new input XString=""; YString=""; Serial.print("X = "); //we print everything on the serial monitor for debugging Serial.println(X); Serial.print("Y = "); Serial.println(Y); Serial.print("Xm = "); Serial.println(Xm); Serial.print("Ym = "); Serial.println(Ym); } if (c == 'U') //move the motors in the position of the X Y values read from the app movement { Serial.print("X = "); Serial.println(X); Serial.print("Y = "); Serial.println(Y); Serial.print("final Xm = "); Serial.println(Xm); Serial.print("final Ym = "); Serial.println(Ym); stepperX.rotate(-72*(Xm-X)); delay(5); stepperY.rotate(-72*(Ym-Y)); delay(5); X=Xm; Y=Ym; readString=""; }