Paso 5: Sketch de Arduino
1 - ahora conectar Arduino al ordenador mediante cable USB.
2 - descargar el IDE de Arduino en tu ordenador.
3-Descargar biblioteca de 1Sheeld.
4-Ponga 1Sheeld biblioteca de Arduino-1.0.5-r2\libraries.
5 - copiar el código de Arduino más abajo y pégalo en tu bosquejo.
Si desea controlar con velocidad completa, sin usar PWM en el control hay en el código siguiente.
// have fun with smart car // control using gravity sensor // Author:Ahmed mahmoud // Date:18 Augst, 2014 include <OneSheeld.h> float x, y ; // define xand y to store values int forward = 9; // define forward direction int bacward = 10; // define backward direction int right = 11; // define right direction int left = 6; // define left direction void setup() { // define output ports OneSheeld.begin(); pinMode(forward, OUTPUT); pinMode(bacward, OUTPUT); pinMode(right, OUTPUT); pinMode(left, OUTPUT); } void loop() { // get sensor value x=GravitySensor.getX(); y=GravitySensor.getY(); // control right and left using y sensor direction if (y >= 1) { digitalWrite(right, HIGH); digitalWrite(left, LOW); } else if ( y <= -1) { digitalWrite(right, LOW); digitalWrite(left, HIGH); } else { digitalWrite(right, LOW); digitalWrite(left, LOW); }//forward and backward if (x >= 2) { digitalWrite(forward, HIGH); digitalWrite(bacward, LOW); } else if ( x <= -2) { digitalWrite(forward, LOW); digitalWrite(bacward, HIGH); } else { digitalWrite(forward, LOW); digitalWrite(bacward, LOW); } }
Si desea controlar con velocidad varible utilizando PWM en el control, aquí está el código a continuación.
// have fun with smart car // control using gravity sensor // Author:Ahmed mahmoud // Date:18 Augst, 2014 #include <OneSheeld.h> float x, y , xval, yval; // define xand y to store values ,and abslute value also int forward = 9; // define forward direction int bacward = 10; // define backward direction int right = 11; // define right direction int left = 6; // define left direction void setup() { // define output ports OneSheeld.begin(); pinMode(forward, OUTPUT); pinMode(bacward, OUTPUT); pinMode(right, OUTPUT); pinMode(left, OUTPUT); } void loop() { // get sensor value x=GravitySensor.getX(); y=GravitySensor.getY(); xval=abs(x)*20; //save abslute value for x and multbli it in 20 ( how get multibli number "1023/sensor range" ) yval=abs(y)*30; // save abslute value for x and multbli it in 30 // control right and left using y sensor direction if (y >= 1) { analogWrite(right, yval); digitalWrite(left, LOW); } else if ( y <= -1) { digitalWrite(right, LOW); analogWrite(left, yval); } else { digitalWrite(right, LOW); digitalWrite(left, LOW); }//forward and backward if (x >= 2) { analogWrite(forward, xval); digitalWrite(bacward, LOW); } else if ( x <= -2) { digitalWrite(forward, LOW); analogWrite(bacward, xval); } else { digitalWrite(forward, LOW); digitalWrite(bacward, LOW); } }
1Sheeld 7-descargar desde app store en tu teléfono inteligente.