Stap 6: Code
Hier is de Code uplaod naar de robot of de Arduino UNO-
#include Servo servoMain; float temp; int tempPin = 0; int r_motor_n = 2; //PWM control Right Motor - int r_motor_p = 4; //PWM control Right Motor + int l_motor_p = 5; //PWM control Left Motor + int l_motor_n = 7; //PWM control Left Motor - int enable = 3; int light = 9; int enable2 = 6;
int incomingByte = 0; // for incoming serial data
void setup() { servoMain.attach(10); pinMode(r_motor_n, OUTPUT); //Set control pins to be outputs pinMode(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-up digitalWrite(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("stive robotics"); }
void loop() {
if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); }
switch(incomingByte) { case 'f': digitalWrite(r_motor_n, LOW); //Set motor direction, 1 low, 2 high digitalWrite(r_motor_p, LOW); digitalWrite(l_motor_p, LOW); digitalWrite(l_motor_n, LOW); digitalWrite(enable, LOW); digitalWrite(enable2, LOW); Serial.println("Stop\n"); incomingByte='*'; break; case 'd': digitalWrite(r_motor_n, HIGH); //Set motor direction, 1 low, 2 high digitalWrite(r_motor_p, LOW); digitalWrite(l_motor_p, HIGH); digitalWrite(l_motor_n, LOW); digitalWrite(enable, HIGH); digitalWrite(enable2, HIGH); Serial.println("right\n"); incomingByte='*'; break; case 'a': digitalWrite(r_motor_n, LOW); //Set motor direction, 1 low, 2 high digitalWrite(r_motor_p, HIGH); digitalWrite(l_motor_p, LOW); digitalWrite(l_motor_n, HIGH); digitalWrite(enable, HIGH); digitalWrite(enable2, HIGH); Serial.println("left\n"); incomingByte='*'; break; case 'w': digitalWrite(r_motor_n, HIGH); //Set motor direction, 1 low, 2 high digitalWrite(r_motor_p, LOW); digitalWrite(l_motor_p, LOW); digitalWrite(l_motor_n, HIGH); digitalWrite(enable, HIGH); digitalWrite(enable2, HIGH); Serial.println("forward\n"); incomingByte='*'; break;
case 's': digitalWrite(r_motor_n, LOW); //Set motor direction, 1 low, 2 high digitalWrite(r_motor_p, HIGH); digitalWrite(l_motor_p, HIGH); digitalWrite(l_motor_n, LOW); digitalWrite(enable, HIGH); digitalWrite(enable2, HIGH); Serial.println("backwards\n"); incomingByte='*'; break; case 'O': digitalWrite(light, LOW); break; case '1': servoMain.write(0); break; case '2': servoMain.write(45); break; case '3': servoMain.write(90); break; case '4': servoMain.write(135); break; case '5': servoMain.write(180); break; case 't': temp = analogRead(tempPin); temp = temp * 0.48828125; Serial.print("TEMPRATURE = "); Serial.print(temp); Serial.print("*C"); Serial.println(); delay(1000); break; case 'v': Serial.print("atom "); Serial.println(); Serial.print("stive robotics atom"); incomingByte='*'; break; delay(5000); }