Étape 3: Serial Communication - Code
Maintenant que vous avez votre Arduino filaire vers le haut, copiez et collez ce code dans l’IDE Arduino. Que signifie ce code est lu pour un signal que vous devez taper manuellement dans le moniteur de la série. Lorsque la valeur 1 ou 2 est entré, le moteur tournerait soit dans le sens horaire ou antihoraire pour une courte période de temps. Expérimenter un peu ! Tapez en multiples de 1 ou 2 et voir ce qui se passe !
int in1pin = 6;int in2pin = 7; // connections to H-Bridge, clockwise / counterchar receivedChar; // store info boolean newData = false; // create a true/false statementvoid setup() { pinMode(in1pin, OUTPUT); pinMode(in2pin, OUTPUT); // set pins to OUTPUTS Serial.begin(9600); // start up serial communication }void loop() { recvData(); // read and store data moveMotor(); // move motor according to data and then reset }void recvData() { if (Serial.available() > 0) { // if the serial monitor has a reading receivedChar = Serial.read(); // set char to be what is read newData = true; // make statement true } }void moveMotor() { int motordirection = (receivedChar - '0'); // turn recieved data into usable form and give it a name while(newData == true) { Serial.println(motordirection); // print motor direction if (motordirection == 1) { // if it reads 1... digitalWrite(in1pin, HIGH); // turn motor one way digitalWrite(in2pin, LOW); delay(250); } else if (motordirection == 2) { // if it reads 2... digitalWrite(in1pin, LOW); // turn motor other way digitalWrite(in2pin, HIGH); delay(250); } else { // if nothing is read digitalWrite(in1pin, LOW); // motor is off digitalWrite(in2pin, LOW); } newData = false; // reset value to false } }