Stap 10: Code
Voor het uploaden van de code die u nodig hebt de Arduino IDE met de Intel Edison plug in, de code om te worden geüpload kan hieronder worden gevonden. De Intel Edison Plugin kan van de Arduino IDE bestuur manager worden geïnstalleerd door de Intel x86-pakket te installeren. Zorg ervoor dat u de koos de juiste poort tijdens het uploaden van de code op het bord.
#includeServo servoMain;float temp;int tempPin = 0;int r_motor_n = 4; //PWM control Right Motor -int r_motor_p = 5; //PWM control Right Motor +int l_motor_p = 6; //PWM control Left Motor +int l_motor_n = 7; //PWM control Left Motor -int enable = 3;int light = 9;int enable2 = 6; //PWM CONTROL SPEEDint speed1 = 0; // PWM controll speedint incomingByte = 0; // for incoming serial data#include#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.void setup(){servoMain.attach(10);pinMode(r_motor_n, OUTPUT); //Set control pins to be outputspinMode(r_motor_p, OUTPUT);pinMode(l_motor_p, OUTPUT);pinMode(l_motor_n, OUTPUT);pinMode(enable, OUTPUT);pinMode(enable2, OUTPUT);pinMode(light, OUTPUT);digitalWrite(r_motor_n, LOW); //set both motors off for start-updigitalWrite(r_motor_p, LOW);digitalWrite(l_motor_p, LOW);digitalWrite(l_motor_n, LOW);digitalWrite(enable, LOW);digitalWrite(enable2, LOW);digitalWrite(light, HIGH);Serial.begin(9600);Serial.print("Whats up im ATOM, geared up!!!! \n");Serial.print("w = Forward \n");Serial.print("s = Backward \n");Serial.print("d = Right \n");Serial.print("a = Left \n");Serial.print("f = Stop \n");Serial.print("lame freaks robotics");}void loop(){if (Serial.available() > 0) {// read the incoming byte:incomingByte = Serial.read();}switch(incomingByte){case 'S': // control to stop the robotdigitalWrite(r_motor_n, LOW); //Set motor direction, 1 low, 2 lowdigitalWrite(r_motor_p, LOW);digitalWrite(l_motor_p, LOW);digitalWrite(l_motor_n, LOW);analogWrite(enable, 0);analogWrite(enable2, 0);Serial.println("Stop\n");incomingByte='*';break;case 'R': //control for rightdigitalWrite(r_motor_n, HIGH); //Set motor direction, 1 high, 2 lowdigitalWrite(r_motor_p, LOW);digitalWrite(l_motor_p, HIGH);digitalWrite(l_motor_n, LOW);analogWrite(enable, speed1);analogWrite(enable2, speed1);Serial.println("right\n");incomingByte='*';break;case 'L': //control for leftdigitalWrite(r_motor_n, LOW); //Set motor direction, 1 low, 2 highdigitalWrite(r_motor_p, HIGH);digitalWrite(l_motor_p, LOW);digitalWrite(l_motor_n, HIGH);analogWrite(enable, speed1);analogWrite(enable2, speed1);Serial.println("left\n");incomingByte='*';break;case 'F': //control for forwarddigitalWrite(r_motor_n, HIGH); //Set motor direction, 1 high, 2 highdigitalWrite(r_motor_p, LOW);digitalWrite(l_motor_p, LOW);digitalWrite(l_motor_n, HIGH);analogWrite(enable, speed1);analogWrite(enable2, speed1);Serial.println("forward\n");incomingByte='*';break;case 'B': //control for backwarddigitalWrite(r_motor_n, LOW); //Set motor direction, 1 low, 2 lowdigitalWrite(r_motor_p, HIGH);digitalWrite(l_motor_p, HIGH);digitalWrite(l_motor_n, LOW);analogWrite(enable, speed1);analogWrite(enable2, speed1);Serial.println("backwards\n");incomingByte='*';break;case 'f':digitalWrite(r_motor_n, LOW); //Set motor direction, 1 low, 2 lowdigitalWrite(r_motor_p, LOW);digitalWrite(l_motor_p, LOW);digitalWrite(l_motor_n, LOW);analogWrite(enable, 0);analogWrite(enable2, 0);Serial.println("Stop\n");incomingByte='*';break;case 'd':digitalWrite(r_motor_n, HIGH); //Set motor direction, 1 high, 2 lowdigitalWrite(r_motor_p, LOW);digitalWrite(l_motor_p, HIGH);digitalWrite(l_motor_n, LOW);analogWrite(enable, speed1);analogWrite(enable2, speed1);Serial.println("right\n");incomingByte='*';break;case 'a':digitalWrite(r_motor_n, LOW); //Set motor direction, 1 low, 2 highdigitalWrite(r_motor_p, HIGH);digitalWrite(l_motor_p, LOW);digitalWrite(l_motor_n, HIGH);analogWrite(enable, speed1);analogWrite(enable2, speed1);Serial.println("left\n");incomingByte='*';break;case 'w':digitalWrite(r_motor_n, HIGH); //Set motor direction, 1 high, 2 highdigitalWrite(r_motor_p, LOW);digitalWrite(l_motor_p, LOW);digitalWrite(l_motor_n, HIGH);analogWrite(enable, speed1);analogWrite(enable2, speed1);Serial.println("forward\n");incomingByte='*';break;case 's':digitalWrite(r_motor_n, LOW); //Set motor direction, 1 low, 2 lowdigitalWrite(r_motor_p, HIGH);digitalWrite(l_motor_p, HIGH);digitalWrite(l_motor_n, LOW);analogWrite(enable, speed1);analogWrite(enable2, speed1);Serial.println("backwards\n");incomingByte='*';break;case 'r': // servo anglesservoMain.write(0);break;case 't':servoMain.write(45);break;case 'y':servoMain.write(90);break;case 'u':servoMain.write(135);break;case 'i':servoMain.write(180);break;case 'O': // PWM speed valuesspeed1 = 0 ;break;case '1':speed1 = 155;break;case '2':speed1 = 165;break;case '3':speed1 = 175;break;case '4':speed1 = 185;break;case '5':speed1 = 195;break;case '6':speed1 = 205;break;case '7':speed1 = 215;break;case '8':speed1 = 225;break;case '9':speed1 = 235;break;case 'q':speed1 = 255;break;case 'm':temp = analogRead(tempPin); // Read the temperaturetemp = temp * 0.48828125;Serial.print("TEMPRATURE = ");Serial.print(temp);Serial.print("*C");Serial.println();delay(1000);break;case 'p': // time to ping thr ultasonic sensordelay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).Serial.print("Ping: ");Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)Serial.println("cm");break;delay(5000);}}