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=""; }