Salve a tutti, ho costruito un piccolo robot a cui ho montato e collegato 2 photodiode e 2 led IR per evitare gli ostacoli,
dal seriale ho testato i sensori e tutto funziona alla perfezione e senza errori, il problema e che se faccio partire il robot per evitare gli ostacoli non li rileva in modo coretto, nel senso che rileva gli ostacoli solo da uno dei 2 sensore, Sinistra o Destra e non da tutti e due contemporaneamente, di seguito lascio il codice che ho scritto se qualcuno sa dirmi se ho fatto qualche errore grazie.
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Servo.h>
// SERVO RUOTE
Servo servo_ruota_destra;
Servo servo_ruota_sinistra;
const int pin_servo_ruota_destra = 4;
const int pin_servo_ruota_sinistra = 5;
// SENSORE DI PROSSIMITA' IR
int IRpinLeft = A0; // IR photodiode on analog pin A0
int IRpinRight = A1; // IR photodiode on analog pin A0
int IRemitter = 7; // IR emitter LED on digital pin 2
int ambientIR_Left; // variable to store the IR coming from the ambient
int ambientIR_Right; // variable to store the IR coming from the ambient
int obstacleIR_Left; // variable to store the IR coming from the object
int obstacleIR_Right; // variable to store the IR coming from the object
int value_Left[10]; // variable to store the IR values
int value_Right[10]; // variable to store the IR values
int distance_Left; // variable that will tell if there is an obstacle or not
int distance_Right; // variable that will tell if there is an obstacle or not
// LED
const int led = 8;
// LED STATE
const int led_state = 13;
// SET PRESCALE PORTE ANALOGICHE
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
// VARIABILE PER LA FUNZIONE EVITA OSTACOLI
int funzione_evita_ostacoli = 0;
// VARIABILE PER LA FUNZIONE INSEGUI OSTACOLO
int funzione_insegui_ostacolo = 0;
// VARIABILE PER LA FUNZIONE AUTO-LIGHT
int funzione_auto_light = 0;
int stato_led = 0;
// VARIABILE PER LA MODALITA' DI MOVIMENTO
int mod_movimento = 0;
// SETUP
void setup() {
pinMode(led, OUTPUT);
pinMode(led_state, OUTPUT);
pinMode(IRpinLeft, INPUT);
pinMode(IRpinRight, INPUT);
pinMode(IRemitter,OUTPUT); // IR emitter LED on digital pin 2
digitalWrite(IRemitter,LOW);// setup IR LED as off
servo_ruota_destra.attach(pin_servo_ruota_destra);
servo_ruota_sinistra.attach(pin_servo_ruota_sinistra);
servo_ruota_destra.write(90);
servo_ruota_sinistra.write(90);
fermo();
// Setta prescale a 32
// Riduci la durata della lettura analogica da 128 micros (standard) a 32
sbi(ADCSRA,ADPS2);
cbi(ADCSRA,ADPS1);
sbi(ADCSRA,ADPS0);
// Seriale
Serial.begin(115200);
Serial.println("MINIDUINO-ROVER (c) 2013 Davide Gallo");
}
void loop() {
distance_Left = readIR_Left(5); // calling the function that will read the distance and passing the "accuracy" to it
distance_Right = readIR_Right(5); // calling the function that will read the distance and passing the "accuracy" to it
evita_ostacoli();
}
// SENSORE DI PROSSIMITA' IR
int readIR_Left(int times_Left){
for(int x=0;x<times_Left;x++){
digitalWrite(IRemitter,LOW); //turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR_Left = analogRead(IRpinLeft); // storing IR coming from the ambient
digitalWrite(IRemitter,HIGH); //turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR_Left = analogRead(IRpinLeft); // storing IR coming from the obstacle
value_Left[x] = ambientIR_Left-obstacleIR_Left; // calculating changes in IR values and storing it for future average
}
for(int x=0;x<times_Left;x++){ // calculating the average based on the "accuracy"
distance_Left+=value_Left[x];
}
return(distance_Left/times_Left); // return the final value
}
int readIR_Right(int times_Right){
for(int x=0;x<times_Right;x++){
digitalWrite(IRemitter,LOW); //turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR_Right = analogRead(IRpinRight); // storing IR coming from the ambient
digitalWrite(IRemitter,HIGH); //turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR_Right = analogRead(IRpinRight); // storing IR coming from the obstacle
value_Right[x] = ambientIR_Right-obstacleIR_Right; // calculating changes in IR values and storing it for future average
}
for(int x=0;x<times_Right;x++){ // calculating the average based on the "accuracy"
distance_Right+=value_Right[x];
}
return(distance_Right/times_Right); // return the final value
}
// FUNZIONI DI MOVIMENTO
void avanti()
{
servo_ruota_destra.write(0);
servo_ruota_sinistra.write(180);
}
void indietro()
{
servo_ruota_destra.write(180);
servo_ruota_sinistra.write(0);
}
void ruota_a_destra()
{
servo_ruota_destra.write(0);
servo_ruota_sinistra.write(0);
}
void gira_a_destra()
{
servo_ruota_destra.write(0);
servo_ruota_sinistra.write(90);
}
void ruota_a_sinistra()
{
servo_ruota_destra.write(180);
servo_ruota_sinistra.write(180);
}
void gira_a_sinistra()
{
servo_ruota_sinistra.write(180);
servo_ruota_destra.write(90);
}
void fermo()
{
servo_ruota_destra.write(90);
servo_ruota_sinistra.write(90);
}
void evita_ostacoli()
{
if(distance_Left>250) {
ruota_a_destra();
}
if(distance_Right>250) {
ruota_a_sinistra();
}
else {
avanti();
}
}