Stap 5: Arduino Sketch
1 - nu Arduino verbinding te maken met uw computer via USB-kabel.
2 - download de Arduino IDE op uw computer.
3-download 1Sheeld bibliotheek.
4-put 1Sheeld bibliotheek in Arduino-1.0.5-r2\libraries.
5 - Kopieer de Arduino onderstaande code en plak deze in uw tekening.
Als u bepalen met volledige snelheid wilt, zonder gebruik van PWM in controle is er de onderstaande code.
// 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); } }
Als u bepalen met aflegdelen snelheid met behulp van PWM in controle wilt, is hier de code hieronder.
// 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); } }
De 1Sheeld van de 7-download van app opslag op uw smart phone.