Yes, this is another of these "Help me with my code" posts. I understand that they are annoying but I have to say I'm seriously stuck on this one. All the code works except for the latest addition which is a timer (sort of). So basically, the robot turns right wile comparing Ir readings. When it detects a decrease in the IR level, it turns left until it achieves relatively the biggest IR readings (meaning that the IR source is right in front as there is more radiation detected when facing it). These steps have a lot of calibration processes which I don't think anyone wants to understand so I'll just skip the explanation for them. So... after finding the fire the robot drives forward and puts it out. Now the latest addition is a millis()-based code that doesn't let the robot look for the fire for more than 8 seconds (the robot does a full turn after 8 seconds, so the robot has to move somewhere else cuz there's no flames around
). As I mentioned before, everything works except for this timing part of the code. The robot seems to just ignore it and spins around indefinetely. If you want me to clarify my code I will be happy to do so. Thanks so much for your time. Here's the code:
//Arduino pin assignment
int pingPin = 9; //Ping US sensor
int Ain1 = 11; //H-Bridge1 output1
int Ain2 = 12; //H-Bridhe1 output2
int Bin1 = 7; //H-Bridge2 output1
int Bin2 = 8; //H-Bridge2 output2
int Pwma = 6; //Motor1 speed
int Pwmb = 5; //Motor2 speed
int Stby = 10; //Power saving function
int IRRIGHT = 1; //IR phototransistor
int IRLEFT = 2; //IR phototransistor
int Smoke = 0; //Smoke detector
int Beep = 13; //Beeper
int Weather = 3; DHT11
int Fan = 2; Brushless DC Motor (actually relay)
float FirstIR;
float SecondIR;
int BiggestIR = 0 ;
float Distance_Obstacle;
int SmallestIR = 1023;
bool TimeSet = 0;
unsigned long TimeReference = 0;
unsigned long CurrentTime = 0;
unsigned long TimeDifference = 0;
void setup()
{
//Dual H-Bridge setup
pinMode(Ain1, OUTPUT);
pinMode(Ain2, OUTPUT);
pinMode(Bin1, OUTPUT);
pinMode(Bin2, OUTPUT);
pinMode(Pwma, OUTPUT);
pinMode(Pwmb, OUTPUT);
pinMode(Stby, OUTPUT);
digitalWrite(Stby, HIGH);
analogWrite(Pwma, 255);
analogWrite(Pwmb, 255);
pinMode(Beep, OUTPUT);
pinMode(Fan, OUTPUT);
digitalWrite(Fan, LOW);
int BiggestIR = 0;
SmallestIR = CheckIR();
SmallestIR ++;
}
void loop()
{
if (TimeSet == 0)
{
TimeReference = millis();
TimeSet = 1;
}
analogWrite(Pwma, 100);
analogWrite(Pwmb, 100);
Right();
float FirstIR = CheckIR ();
delay(70);
float SecondIR = CheckIR ();
if (SecondIR > BiggestIR)
{
BiggestIR = SecondIR;
}
if (FirstIR > BiggestIR)
{
BiggestIR = FirstIR;
}
if (FirstIR < SmallestIR)
{
SmallestIR = FirstIR;
}
if (SecondIR < SmallestIR)
{
SmallestIR = FirstIR;
}
if (FirstIR > (SecondIR + 2) && FirstIR > 10 && FirstIR > ((SmallestIR + BiggestIR)/2) + 2)
{
do
{
Left();
SecondIR = CheckIR();
}
while (SecondIR < BiggestIR - 2);
Stop();
TimeSet = 0;
analogWrite(Pwma, 255);
analogWrite(Pwmb, 255);
Distance_Obstacle = MeasureDistance();
do
{
Forward();
Distance_Obstacle = MeasureDistance();
}
while (Distance_Obstacle >= 12);
Stop();
FirstIR = CheckIR();
if (FirstIR > 20 && FirstIR > BiggestIR && Distance_Obstacle < 13)
{
analogWrite(Beep, 255);
delay(1000);
analogWrite(Beep, 0);
digitalWrite(Fan, HIGH);
analogWrite(Pwma, 100);
analogWrite(Pwmb, 100);
Right();
delay(1000);
Left();
delay(2000);
Right();
delay(2000);
Left();
delay(1000);
Stop();
digitalWrite(Fan, LOW);
BiggestIR = SmallestIR + 2;
}
else
{
Back();
delay(500);
Stop();
Right();
delay(500);
Stop();
BiggestIR = SmallestIR + 2;
}
CurrentTime = millis();
TimeDifference = CurrentTime - TimeReference;
if (TimeDifference > 8000 && TimeSet != 0)
{
//Ralentir
analogWrite(Pwma, 255);
analogWrite(Pwmb, 255);
Distance_Obstacle = MeasureDistance();
do
{
Forward();
Distance_Obstacle = MeasureDistance();
}
while (Distance_Obstacle >= 15);
Stop();
Left();
delay(500);
Stop();
TimeSet = 0;
}
}
}
long MeasureDistance()
{
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = duration /29 /2;
delay(50);
return cm;
}
void Forward ()
{
digitalWrite(Ain1, LOW);
digitalWrite(Ain2, HIGH);
digitalWrite(Bin1, LOW);
digitalWrite(Bin2, HIGH);
}
void Back()
{
digitalWrite(Ain1, HIGH);
digitalWrite(Ain2, LOW);
digitalWrite(Bin1, HIGH);
digitalWrite(Bin2, LOW);
}
void Stop ()
{
digitalWrite(Ain1, LOW);
digitalWrite(Ain2, LOW);
digitalWrite(Bin1, LOW);
digitalWrite(Bin2, LOW);
}
void Right ()
{
digitalWrite(Ain1, HIGH);
digitalWrite(Ain2, LOW);
digitalWrite(Bin1, LOW);
digitalWrite(Bin2, HIGH);
}
void Left ()
{
digitalWrite(Ain1, LOW);
digitalWrite(Ain2, HIGH);
digitalWrite(Bin1, HIGH);
digitalWrite(Bin2, LOW);
}
//Read IR
float CheckIR ()
{
int IRR = analogRead(IRRIGHT);
int IRL = analogRead(IRLEFT);
float FinalValue = (IRR + IRL)/2;
return FinalValue;
}