Este programa possibilita que a cada vez que o robô seja ligado, os valores de intensidade de absorção de luz das superfícies clara e escura, possam ser setados. Ele pede via display, para que o robô seja posicionado sobre ambas as superfícies, uma de cada vez. Assim ele calcula a media entre as duas e carrega este valor em uma variável, que é consultada a cada loop, como condição para virar para algum lado. Também foi acrescentado um Buser e o sensor ultrassônico para parar o carrinho com obstáculos a 15 cm. Ele também vai parar por 5 segundos quando os dois sensores estiverem sobre a fita ao mesmo tempo:
#include <NewPing.h>
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 7 // 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.
#include <NewTone.h>
# define SensorR A0
# define SensorT A1
int sensorR;
int sensorT;
int motorR;
int motorT;
int MaxValue = 80; // potencia maxima dos motores a frente;
int sensorrFita;
int sensortFita;
int sensorrSup;
int sensortSup;
int mediaR;
int mediaT;
int distancia = 15;
// PARAMETROS DO LCD ****************************************
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADOR 0x3F // <<
#define BACKLIGHT_PIN 3
#define En 2
#define Rw 1
#define Rs 0
#define D4 4
#define D5 5
#define D6 6
#define D7 7
LiquidCrystal_I2C lcd(I2C_ADOR, En, Rw, Rs, D4, D5, D6, D7);
//**************************************************************
void setup() {
lcd.begin (16, 2);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(10, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
// calibração dos sensores:
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("prepare-se para");
lcd.setCursor(0, 1);
lcd.write("pos. os sensores");
NewTone(13, 500, 500);
delay(5000);
// calibração do sensor R sobre a superfície;
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("pos. R na sup :");
NewTone(13, 500, 500);
lcd.setCursor(3, 1);
delay(5000);
sensorR = analogRead(SensorR);
sensorrSup = sensorR - 10;
sensorT = analogRead(SensorT);
sensortFita = sensorT + 10;
// calibração do sensor R sobre a fita;
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("pos. R na fita :");
NewTone(13, 500, 500);
lcd.setCursor(3, 1);
delay(5000);
sensorR = analogRead(SensorR);
sensorrFita = sensorR + 10;
sensorT = analogRead(SensorT);
sensortSup = sensorT - 10;
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("Calib. Concluida");
NewTone(13, 500, 500);
lcd.setCursor(0, 1);
lcd.write("Rs:");
lcd.setCursor(4, 1);
lcd.print(sensorrSup);
lcd.setCursor(8, 1);
lcd.write("Rf:");
lcd.setCursor(12, 1);
lcd.print(sensorrFita);
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("Calib. Concluida");
NewTone(13, 500, 500);
lcd.setCursor(0, 1);
lcd.write("Ts:");
lcd.setCursor(4, 1);
lcd.print(sensortSup);
lcd.setCursor(8, 1);
lcd.write(" Tf:");
lcd.setCursor(12, 1);
lcd.print(sensortFita);
Serial.print(" Rs = ");
Serial.print(sensorrSup);
Serial.print(" Rf = ");
Serial.print(sensorrFita);
Serial.print(" Ts = ");
Serial.print(sensortSup);
Serial.print(" Tf = ");
Serial.println(sensortFita);
delay(5000);
mediaR = (sensorrFita - sensorrSup) / 2;
mediaT = (sensorrFita - sensorrSup) / 2;
}
void loop() {
sensorR = analogRead(SensorR);
sensorT = analogRead(SensorT);
motorR = map(sensorR, sensorrSup, sensorrFita, MaxValue, 10);
motorT = map(sensorT, sensortSup, sensortFita, MaxValue, 10);
Serial.print(" SensorR = ");
Serial.print(sensorR);
Serial.print(" sensorT = ");
Serial.print(sensorT);
Serial.print("motorR = ");
Serial.print(motorR);
Serial.print("motorT = ");
Serial.print(motorT);
Serial.print(" Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if (distancia < (sonar.ping_cm()) || (sonar.ping_cm()) == 0) {
if (sensorR < mediaR && sensorT < mediaT) {
analogWrite(3, MaxValue);
analogWrite(6, MaxValue);
}
if (sensorR > mediaR && sensorT > mediaT) {
NewTone(13, 500, 500);
analogWrite(3, 255);
analogWrite(6, 255);
analogWrite(9, 255);
analogWrite(10, 255);
delay(2000);
analogWrite(3, 0);
analogWrite(6, 0);
analogWrite(9, 0);
analogWrite(10, 0);
delay(3000);
}
if (sensorR > mediaR) {
analogWrite(3, 0);
analogWrite(6, MaxValue);
while (sensorT < mediaT) {
sensorR = analogRead(SensorR);
sensorT = analogRead(SensorT);
}
}
if (sensorT > mediaT) {
analogWrite(3, MaxValue);
analogWrite(6, 0);
while (sensorR < mediaR) {
sensorR = analogRead(SensorR);
sensorT = analogRead(SensorT);
}
}
}
if (distancia > (sonar.ping_cm()) && (sonar.ping_cm()) > 0) {
NewTone(13, 500, 500);
analogWrite(3, 255);
analogWrite(6, 255);
analogWrite(9, 255);
analogWrite(10, 255);
delay(2000);
analogWrite(3, 0);
analogWrite(6, 0);
analogWrite(9, 0);
analogWrite(10, 0);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Sketch para dar meia volta no fim do caminho:
#include <NewPing.h>
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 7 // 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.
#include <NewTone.h>
# define SensorR A0
# define SensorT A1
int sensorR;
int sensorT;
int motorR;
int motorT;
int MaxValue = 80; // potencia maxima dos motores a frente;
int sensorrFita;
int sensortFita;
int sensorrSup;
int sensortSup;
int mediaR;
int mediaT;
int distancia = 15;
// PARAMETROS DO LCD ****************************************
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADOR 0x3F // <<
#define BACKLIGHT_PIN 3
#define En 2
#define Rw 1
#define Rs 0
#define D4 4
#define D5 5
#define D6 6
#define D7 7
LiquidCrystal_I2C lcd(I2C_ADOR, En, Rw, Rs, D4, D5, D6, D7);
//**************************************************************
void setup() {
lcd.begin (16, 2);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(10, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
// calibração dos sensores:
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("prepare-se para");
lcd.setCursor(0, 1);
lcd.write("pos. os sensores");
NewTone(13, 500, 500);
delay(5000);
// calibração do sensor R sobre a superfície;
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("pos. R na sup :");
NewTone(13, 500, 500);
lcd.setCursor(3, 1);
delay(5000);
sensorR = analogRead(SensorR);
sensorrSup = sensorR - 10;
sensorT = analogRead(SensorT);
sensortFita = sensorT + 10;
// calibração do sensor R sobre a fita;
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("pos. R na fita :");
NewTone(13, 500, 500);
lcd.setCursor(3, 1);
delay(5000);
sensorR = analogRead(SensorR);
sensorrFita = sensorR + 10;
sensorT = analogRead(SensorT);
sensortSup = sensorT - 10;
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("Calib. Concluida");
NewTone(13, 500, 500);
lcd.setCursor(0, 1);
lcd.write("Rs:");
lcd.setCursor(4, 1);
lcd.print(sensorrSup);
lcd.setCursor(8, 1);
lcd.write("Rf:");
lcd.setCursor(12, 1);
lcd.print(sensorrFita);
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("Calib. Concluida");
NewTone(13, 500, 500);
lcd.setCursor(0, 1);
lcd.write("Ts:");
lcd.setCursor(4, 1);
lcd.print(sensortSup);
lcd.setCursor(8, 1);
lcd.write(" Tf:");
lcd.setCursor(12, 1);
lcd.print(sensortFita);
Serial.print(" Rs = ");
Serial.print(sensorrSup);
Serial.print(" Rf = ");
Serial.print(sensorrFita);
Serial.print(" Ts = ");
Serial.print(sensortSup);
Serial.print(" Tf = ");
Serial.println(sensortFita);
delay(5000);
mediaR = (sensorrFita - sensorrSup) / 2;
mediaT = (sensorrFita - sensorrSup) / 2;
}
void loop() {
sensorR = analogRead(SensorR);
sensorT = analogRead(SensorT);
motorR = map(sensorR, sensorrSup, sensorrFita, MaxValue, 10);
motorT = map(sensorT, sensortSup, sensortFita, MaxValue, 10);
Serial.print(" SensorR = ");
Serial.print(sensorR);
Serial.print(" sensorT = ");
Serial.print(sensorT);
Serial.print("motorR = ");
Serial.print(motorR);
Serial.print("motorT = ");
Serial.print(motorT);
Serial.print(" Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if (distancia < (sonar.ping_cm()) || (sonar.ping_cm()) == 0) {
if (sensorR < mediaR && sensorT < mediaT) {
analogWrite(3, MaxValue);
analogWrite(6, MaxValue);
}
if (sensorR > mediaR && sensorT > mediaT) {
NewTone(13, 500, 500);
analogWrite(3, 255);
analogWrite(6, 255);
analogWrite(9, 255);
analogWrite(10, 255);
delay(1000);
analogWrite(3, 0);
analogWrite(6, 0);
analogWrite(9, 0);
analogWrite(10, 0);
delay(1000);
analogWrite(3, 100);
analogWrite(10, 100);
delay(1000);
sensorR = analogRead(SensorR);
sensorT = analogRead(SensorT);
// Meia volta...
while (sensorR < mediaR && sensorT < mediaT) {
sensorR = analogRead(SensorR);
sensorT = analogRead(SensorT);
Serial.print("sensorR = ");
Serial.print(sensorR);
Serial.print("---mediaR = ");
Serial.print(mediaR);
Serial.print("---sensorT = ");
Serial.print(sensorT);
Serial.print("---mediaT = ");
Serial.println(mediaT);
}
NewTone(13, 500, 500);
analogWrite(3, 255);
analogWrite(6, 255);
analogWrite(9, 255);
analogWrite(10, 255);
delay(1000);
analogWrite(3, 0);
analogWrite(6, 100);
analogWrite(9, 100);
analogWrite(10, 0);
delay(500);
analogWrite(9,0);
analogWrite(6,0);
}
if (sensorR > mediaR) {
analogWrite(3, 0);
analogWrite(6, MaxValue);
while (sensorT < mediaT) {
sensorR = analogRead(SensorR);
sensorT = analogRead(SensorT);
}
}
if (sensorT > mediaT) {
analogWrite(3, MaxValue);
analogWrite(6, 0);
while (sensorR < mediaR) {
sensorR = analogRead(SensorR);
sensorT = analogRead(SensorT);
}
}
}
if (distancia > (sonar.ping_cm()) && (sonar.ping_cm()) > 0) {
NewTone(13, 500, 500);
analogWrite(3, 255);
analogWrite(6, 255);
analogWrite(9, 255);
analogWrite(10, 255);
delay(2000);
analogWrite(3, 0);
analogWrite(6, 0);
analogWrite(9, 0);
analogWrite(10, 0);
}
}
Nenhum comentário:
Postar um comentário