Here is the complete code I use (with the proper indentation!). I tried to make it simpler....Now the real thing:
Main:
int motorPin1 = 6;
int motorPin2 = 9;
int motorPin3 = 11;
int motorPin4 = 13;
int photoAV = 3;
int photoGA = 4;
int photoDR = 2;
int AV,GA,DR;
int incomingByte,;
int led10 = 10;
int led12 = 12;
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
pinMode(led10, OUTPUT);
pinMode(led12, OUTPUT);
}
void moteur(int mot1, boolean mot2, int mot3, boolean mot4, int duree);
void moteuravance(int mot1, boolean mot2, int mot3, boolean mot4);
void lumiere();
void loop() {
lumiere();
}
and then "lumiere" tab:
void lumiere()
{
AV = analogRead(photoAV);
GA = analogRead(photoGA);
DR = analogRead(photoDR);
if(AV > GA && AV > DR){
Serial.print('H');
delay(100);
digitalWrite(led10, LOW);
digitalWrite(led12, LOW);
moteur(0, false, 0, false, 500);
}
else{
digitalWrite(led10, HIGH);
digitalWrite(led12, LOW);
Serial.print('L');
delay(100);
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn motors in one direction:
if (incomingByte == 'H') {
moteuravance(255, false, 0, true);
}
else if (incomingByte == 'L') {
moteuravance(0, true, 255, false);
}
delay(100);
}
}
}
and "moteurs" tab:
void moteuravance(int mot1, boolean mot2, int mot3, boolean mot4)
{
analogWrite(motorPin1, mot1);
digitalWrite(motorPin2, mot2);
analogWrite(motorPin3, mot3);
digitalWrite(motorPin4, mot4);
}
void moteur(int mot1, boolean mot2, int mot3, boolean mot4, int duree)
{
analogWrite(motorPin1, mot1);
digitalWrite(motorPin2, mot2);
analogWrite(motorPin3, mot3);
digitalWrite(motorPin4, mot4);
delay(duree);
}
Can you help me to understand why this code doesn't respond properly right a way and some delay is required for Fio1 to receive the "H" and "L" from Fio2?