Paso 19: Cargar el código
Copie el código siguiente y péguelo en la pantalla del ide de arduino. Es necesario extraer el módulo de BT antes de subir como los pines rx y tx se asocian con el chip de programación por lo que no debe estar conectado a estos pin mientras se cargan el código. Así que quite tu arduino del escudo y subir el código o quite el módulo de BT de su zócalo.
#include // add your new ping library here #include // add your liquid crystal library here LiquidCrystal lcd(11, 8, 7, 4, 3, 2); float temp =1; int temppin = A0; int r_motor_n = 9; //PWM control Right Motor + int r_motor_p = 10; //PWM control Right Motor - int l_motor_p = 6; //PWM control Left Motor - int l_motor_n = 5; //PWM control Left Motor + int f_light = A3; int b_light = A4; int horn = 12; int n_light = 13; int speedy = 255; int incomingByte = 0; // for incoming serial data #define TRIGGER A2 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO A1 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER, ECHO, MAX); // NewPing setup of pins and maximum distance. void setup() { 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(f_light, OUTPUT); pinMode(b_light, OUTPUT); pinMode(n_light, OUTPUT); pinMode(horn, 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); Serial.begin(9600); Serial.println("Start"); lcd.begin(16,2); lcd.clear(); lcd.print(" Hello "); lcd.setCursor(0,1); lcd.print(" I'm Robot "); delay(3000); } void loop() {</liquidcrystal.h></newping.h></p><p>if (Serial.available() > 0) { incomingByte = Serial.read(); }</p><p>switch(incomingByte) {</p><p>case 'S': // control to stop the robot digitalWrite(r_motor_n, LOW); digitalWrite(r_motor_p, LOW); digitalWrite(l_motor_p, LOW); digitalWrite(l_motor_n, LOW); Serial.println("Stop"); lcd.clear(); lcd.print("Stop"); incomingByte='*'; break;</p><p>case 'R': //control for right analogWrite(r_motor_n, speedy); digitalWrite(r_motor_p, LOW); analogWrite(l_motor_p, speedy); digitalWrite(l_motor_n, LOW); Serial.println("right"); lcd.clear(); lcd.print("Right"); incomingByte='*'; break;</p><p>case 'L': //control for left digitalWrite(r_motor_n, LOW); analogWrite(r_motor_p, speedy); digitalWrite(l_motor_p, LOW); analogWrite(l_motor_n, speedy); Serial.println("left"); lcd.clear(); lcd.print("Left"); incomingByte='*'; break;</p><p>case 'F': //control for forward analogWrite(r_motor_n, speedy); digitalWrite(r_motor_p, LOW); digitalWrite(l_motor_p, LOW); analogWrite(l_motor_n, speedy); Serial.println("forward"); lcd.clear(); lcd.print("Forward"); incomingByte='*'; break;</p><p>case 'B': //control for backward digitalWrite(r_motor_n, LOW); analogWrite(r_motor_p, speedy); analogWrite(l_motor_p, speedy); digitalWrite(l_motor_n, LOW); Serial.println("backwards"); lcd.clear(); lcd.print("Backward"); incomingByte='*'; break;</p><p>case 'f': //control for stop digitalWrite(r_motor_n, LOW); digitalWrite(r_motor_p, LOW); digitalWrite(l_motor_p, LOW); digitalWrite(l_motor_n, LOW); Serial.println("Stop"); lcd.clear(); lcd.print("Stop"); incomingByte='*'; break;</p><p>case 'd': // control for right analogWrite(r_motor_n, speedy); digitalWrite(r_motor_p, LOW); analogWrite(l_motor_p, speedy); digitalWrite(l_motor_n, LOW); Serial.println("right"); lcd.clear(); lcd.print("Right"); incomingByte='*'; break;</p><p>case 'a': // control for left digitalWrite(r_motor_n, LOW); analogWrite(r_motor_p, speedy); digitalWrite(l_motor_p, LOW); analogWrite(l_motor_n, speedy); Serial.println("left"); lcd.clear(); lcd.print("Left"); incomingByte='*'; break;</p><p>case 'w': // control for forward analogWrite(r_motor_n, speedy); digitalWrite(r_motor_p, LOW); digitalWrite(l_motor_p, LOW); analogWrite(l_motor_n, speedy); Serial.println("forward"); lcd.clear(); lcd.print("Forward"); incomingByte='*'; break;</p><p>case 's': // control for backward digitalWrite(r_motor_n, LOW); analogWrite(r_motor_p, speedy); analogWrite(l_motor_p, speedy); digitalWrite(l_motor_n, LOW); Serial.println("backwards"); lcd.clear(); lcd.print("Backward"); incomingByte='*'; break;</p><p>case 'J': // front lights on digitalWrite(f_light, HIGH); Serial.println("front lights on"); incomingByte='*'; break;</p><p>case 'j': digitalWrite(f_light, LOW); // off Serial.println("front lights off"); incomingByte='*'; break;</p><p>case 'K': digitalWrite(b_light, HIGH); // back lights on Serial.println("back lights on"); incomingByte='*'; break;</p><p>case 'k': digitalWrite(b_light, LOW); // off Serial.println("back lights off"); incomingByte='*'; break;</p><p>case 'G': digitalWrite(n_light, HIGH); // neon lights on Serial.println("neon lights on"); incomingByte='*'; break;</p><p>case 'g': digitalWrite(n_light, LOW); off Serial.println("neon lights off"); incomingByte='*'; break;</p><p>case 'H': digitalWrite(horn, HIGH); // horn on Serial.println("horn on"); incomingByte='*'; break;</p><p>case 'h': digitalWrite(horn, LOW); off Serial.println("horn off"); incomingByte='*'; break;</p><p>case 'O': // PWM speed values speedy = 0 ; Serial.println("speed= 0"); lcd.clear(); lcd.print("Speed=0"); incomingByte='*'; break;</p><p>case '1': speedy = 155; Serial.println("speed= 10"); lcd.clear(); lcd.print("Speed=10"); incomingByte='*'; break;</p><p>case '2': speedy = 165; Serial.println("speed= 20"); lcd.clear(); lcd.print("Speed=20"); incomingByte='*'; break;</p><p>case '3': speedy = 175; Serial.println("speed= 30"); lcd.clear(); lcd.print("Speed =30"); incomingByte='*'; break;</p><p>case '4': speedy = 185; Serial.println("speed= 40"); lcd.clear(); lcd.print("Speed=40"); incomingByte='*'; break;</p><p>case '5': speedy = 195; Serial.println("speed= 50"); lcd.clear(); lcd.print("Speed=50"); incomingByte='*'; break;</p><p>case '6': speedy = 205; Serial.println("speed= 60"); lcd.clear(); lcd.print("Speed=60"); incomingByte='*'; break;</p><p>case '7': speedy = 215; Serial.println("speed= 70"); lcd.clear(); lcd.print("Speed=70"); incomingByte='*'; break;</p><p>case '8': speedy = 225; Serial.println("speed= 80"); lcd.clear(); lcd.print("Speed=80"); incomingByte='*'; break;</p><p>case '9': speedy = 235; Serial.println("speed= 90"); lcd.clear(); lcd.print("Speed=90"); incomingByte='*'; break;</p><p>case 'q': speedy = 255; Serial.println("speed= 100"); lcd.clear(); lcd.print("Speed=100"); incomingByte='*'; break;</p><p>case 'p': delay(50); // display temp. and distance unsigned int uS = sonar.ping(); Serial.print("Distance: "); Serial.print(uS / US_ROUNDTRIP_CM); Serial.println("cm"); lcd.clear(); lcd.print("Distance: "); lcd.print(uS / US_ROUNDTRIP_CM); lcd.print("cm"); lcd.setCursor(0,1); temp = analogRead(temppin); temp = temp * 0.48828125; Serial.print("Temperature = "); Serial.print(temp); Serial.print("*C"); Serial.println(); lcd.print("Temp. = "); lcd.print(temp); lcd.print("*C"); delay(1000); incomingByte='*'; break;</p><p>delay(5000); } }</p>