Car movement distance control with flame sensor

in the test code of post 92
car moves in presence of flame.
and stops in the absence.

in the 100 post code
the sensor reads constant 0
flame or no flame,
results in car never moving.

When you test the flame sensors alone, what was the result?

[edit] Mis-interpreting flame sensors will effect commands for motor movement... I see EN1 turning on and off about ten times a second. You need to get the flame sensors working before using them in the full sketch.

You are still in the mode of dumping everything in one bucket and hoping it works. You will only solve this step by step.

I used your simulation. The flame sensors wired right will make your car stop ("StopCar") and not drive forward from the start. Also, after detecting flames, the code is looking for a HIGH from the flame sensors to stop signaling a fire... but that signal must be present for one second before being recognized.

Again, start from learning how to read the output of the flame sensors... you know you have a "right" sensor and no "left" sensor? Also, learn how to get an analog value from the forward flame sensor.

i thought from post 91 you meant to write a code for forward movement with the flame sensors.

so you mean separate test code for movement and separate for flame sensor read?

Yes. Get to know each device on its own. I did this (wrote single-device code) with a popular "robot car" that was giving beginners trouble due to a different (better) motor driver board (TB6612FNG) than the power-eating L298N, so "normal" internet motor code was not working... Here is what I wrote... Not showing "how to program YOUR robot" but I am showing that I divided steps into the smallest useful code.

i am ignoring right now the main sketch and focusing on why i get constant 0
on the analog flame sensor.

thats the minzmize sketch

#define FlamePinForawrdAnlog 16 


void setup() {
 
  //setting flame sensor
  //pinMode(FlamePinForawrd, INPUT);
  pinMode(FlamePinForawrdAnlog, INPUT);

  // initaite serial monitor 
  Serial.begin(9600);

}

void loop() {

    delay(100);
    //int FlameCheckForawrd = digitalRead(FlamePinForawrd);
    int FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);

    Serial.println("flame sensor read");
    Serial.println(FlameCheckForawrdAnalog);
    delay(100);

   
}

same result

0
flame sensor read
0
flame sensor read
0
flame sensor read
0
flame sensor read
0
flame sensor read
0
flame sensor read
0
flame sensor read
0
flame sensor read
0

this time i tried 5v on the sensor
this article claim , analog requires 5v

but no change

i did
post 28 posted the log of only the analog output
you even replied

It appears your ESP32 Pin 16 is DIO (digital, 1 or 0) rather than ADC (analog, 0 to 4095).

DIO test:

  1. Run this sketch.
  2. Use a jumper wire on PIN 16.
  3. Touch the jumper wire to GND... you should read 0
  4. Touch the jumper wire to 3v3.... you should read 1
void setup() {
  Serial.begin(115200);
  pinMode(16, INPUT);
}

void loop() {
  Serial.print(digitalRead(16)); // DIO pin will read 0 (GND) or 1 (3v3)
  delay(100);
}

ADC test:

  1. Run this sketch.
  2. Use a jumper wire on PIN 16.
  3. Touch the jumper wire to GND... does it read 0?
  4. Touch the jumper wire to 3v3.... does it read 4095?
void setup() {
  Serial.begin(115200);
  pinMode(16, INPUT);
}

void loop() {
  Serial.print(analogRead(16)); // ADC pin will read 0 (GND) or 4095 (3V3)
  delay(100);
}

I do not know what this ("I did") is a reference to, because my response to #28 (a few posts later) that you can't get ">5000" out of 4096 bits is accurate.

[edit]
I can not find in this thread the board you are using, other than the ESP32 sim. Would you post (again?) the board you are using (so I can read the pinout - or you post the pinout). It seems to be dependent on the module manufacturer... I see ESP32 WROOM pinouts with Pin 16 as DIO/UART or ADC

[psedit]
For example, this is a simulation of ESP32-C3 with DIO or ADC on one pin:

byte dora = 0; // 0 = DIO
// byte dora = 1; // 1 = ADC

#define pin 2

void setup() {
  Serial.begin(115200);
  pinMode(pin, INPUT);
}

void loop() {
  if (dora)
    adc();
  else
    dio();
  delay(100);
}

void dio() {
  Serial.print(digitalRead(pin));
}

void adc() {
  Serial.println(analogRead(pin));
}

Click for the code...

diagram.json for WOKWI
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "board-esp32-c3-devkitm-1", "id": "esp", "top": -0.3, "left": -3.78, "attrs": {} },
    { "type": "wokwi-slide-switch", "id": "sw1", "top": -24.4, "left": -35.3, "attrs": {} }
  ],
  "connections": [
    [ "esp:TX", "$serialMonitor:RX", "", [] ],
    [ "esp:RX", "$serialMonitor:TX", "", [] ],
    [ "esp:GND.1", "sw1:3", "black", [ "v-0.3", "h-13.38" ] ],
    [ "esp:3V3.2", "sw1:1", "green", [ "h-28.8", "v-28.8" ] ],
    [ "esp:2", "sw1:2", "green", [ "h0" ] ]
  ],
  "dependencies": {}
}

around the 28 post i did a test code to read what values i am working with.
it was about the same as in the one i 107.
didnt post the code just the value range.

did the test
for the digital results

1111111111111111111000011111111111111111111111111111111111111100000000000000000000000000000

while 3.3v goes '0' for for beats then return to '1'.
when GND '0'.

for analog test

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111011111111111111111111111111111111111111111111111101111111000000000000000000000000000000

its zero when GND
whenin 16 pin it goes '1' but by the end still drop to zero while connected.
not sure what it means

but i am guessing you are right it seems to be digital.
the datasheet i posted probably not the right one.

this the esp i am using
https://www.amazon.com/dp/B09KLS2YB3?ref=ppx_yo2ov_dt_b_fed_asin_title
i think that the right datasheet for it


indeed shows 16 to be not for analog

the pic

also the way the yellow jumper is set.
means it supplying 3.3v or 5v?

Something is intermittent with your button, wiring, connections or power supply.
If you are using 5vdc, do your IO pins accept 5vdc? If the ADC is waiting for 5vdc and you give it 3V3, the "1" or "0" could easily fluctuate. I though ESP32 were 3v3 only.

Try an analog/ADC pin to replace "pin 16" and test again.

Your board can not get an analogRead() out of a digital pin.

yeah i will try another pin according to datasheet 110

i think you right, its 3.3v, but i been told the expansion board allows for 3.3v and 5v depends where the yellow jumper is. pic in post 110.
could be i misunderstood.

updated
that the latest code

// Setting Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Settin Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

// define pins for flame sensor
//#define FlamePinForawrd 17
#define FlamePinForawrdAnlog 4 
#define FlamePinRight 15
//#define FlamePinLeft 

// pin 13 that connects to the relay to control the pump
#define RELAY_PIN   12 

//pins for siren led and buzzer
//siren
#define LedBlue 2
#define LedRed 21 
#define Buzzer 19



void setup() {
  //modding motor A
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT); 

  //modding Motor B
  pinMode(enable2Pin, OUTPUT);
  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);

  //setting flame sensor
  //pinMode(FlamePinForawrd, INPUT);
  pinMode(FlamePinForawrdAnlog, INPUT);
  pinMode(FlamePinRight, INPUT);
  //pinMode(FlamePinLeft, INPUT);


  //setting siren for right
  pinMode(LedBlue, OUTPUT);  //defined port for right leds
  pinMode(LedRed, OUTPUT);
  pinMode(Buzzer, OUTPUT); // port front buzzer

  // initaite serial monitor 
  Serial.begin(9600);

}

void loop() {
   
    //int FlameCheckForawrd = digitalRead(FlamePinForawrd);
    int FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
    int FlameCheckRight = digitalRead(FlamePinRight);
    //int FlameCheckLeft = digitalRead(FlamePinLeft);

    ///sensorTest();

    Serial.println(FlameCheckForawrdAnalog);

    if (FlameCheckForawrdAnalog < 2500) 
    {
      Serial.println("flame dected a head");
      //FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
      Serial.println(FlameCheckForawrdAnalog);
      delay(200);
      CAR_moveForwardWifi();
      // front fire indicated by both siren on
      Serial.println("sirens on");
      sirenOn(LedBlue, LedRed, Buzzer);
      delay(200);
      Serial.println("pump on");
      digitalWrite(RELAY_PIN, HIGH);}
      
      
    
    else if(FlameCheckRight == LOW)
    {
      Serial.println("flame dected on the right");
      Serial.println("turning right");
      CAR_turnRightWifi();
      Serial.println("siren on");
      sirenOn(LedBlue,LedRed, Buzzer);
      //updated front sensor value
      //FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
      //Serial.println(FlameCheckForawrdAnalog);

      if (FlameCheckForawrdAnalog < 2500) 
        {
          FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
          Serial.println(FlameCheckForawrdAnalog);
          Serial.println("sirens off");
          sirenoff(LedBlue, LedRed, Buzzer);
          StopCar();
        }
      delay(200);
        
    }    
    //else if (FlameCheckLeft == LOW)
     //{
      //Serial.println("flame dected on the left");
      //while(FlameCheckForawrd != LOW)
        //{
         //FlameCheckForawrd = digitalRead(FlamePinForawrd);
         //Serial.println("turning left");
         //CAR_turnLeftWifi();
         //delay(200);
        //} 
     //}     
      
    else
     {Serial.println("pump off");
      digitalWrite(RELAY_PIN, LOW);
      Serial.println("sirens off");
      sirenoff(LedBlue, LedRed, Buzzer);
      StopCar();} 
     
    
    
//delay(300);//change this va

}
     
     
// stop car
void StopCar(){
  Serial.println("StopCar");
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(700);
}

void CAR_moveForwardWifi() {
  //Set speed motor A and B
  analogWrite(enable1Pin,186);
  analogWrite(enable2Pin,200);
  //move forawrd
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
}

void CAR_moveBackwardWifi() {
  //setting speed motor A and B
  analogWrite(enable1Pin,190);
  analogWrite(enable2Pin,200);
  //back movement
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
}

void CAR_turnLeftWifi() {
    //Set speed motor A and B

  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,0);
  //turn left
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, LOW);
}

void CAR_turnRightWifi() {
  //Set speed motor A and B
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  //turn right
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
}

void sensorTest() {
  while (1) {
    for (int i = 0; i < 3; i++) {
      Serial.print(digitalRead(i + 15)); // read "flame" sensors
    }
    Serial.println(); // print the sensor state
    delay(250);
  }
}

//functions for siren 
void sirenOn(int Led1Pin, int Led2Pin,int BuzzerPin)
{
  int i;

  for (int i = 700; i < 800; i++) // duration of the first sound 350 2 millisecond cycles.
    
  {
    tone(BuzzerPin,i);
    delay(15);
    digitalWrite(Led1Pin, HIGH); // turn on the leds
    digitalWrite(Led2Pin, LOW);
    delay(30);
    digitalWrite(Led1Pin, LOW); // blinking leds
    digitalWrite(Led2Pin, HIGH);
    delay(30); 
    delay(15); // wait 1 millisecond and restart from the for statement (100 repetitions)
  }

  for(i=800;i>700;i--){
  tone(BuzzerPin,i);
  delay(15);
  digitalWrite(Led1Pin, HIGH); // turn on the leds
  digitalWrite(Led2Pin, LOW);
  delay(30);
  digitalWrite(Led1Pin, LOW); // blinking leds
  digitalWrite(Led2Pin, HIGH);
  delay(30); 
  delay(15); // wait 1 millisecond and restart from the for statement (100 repetitions)
  }

}

void sirenoff(int Led1Pin, int Led2Pin,int BuzzerPin)
{
    digitalWrite(Led1Pin, LOW); // turn off blinking leds
    digitalWrite(Led2Pin, LOW);

    tone(BuzzerPin,0); // turn off buzzer

}

two problem
for the front sensor piece of code

if (FlameCheckForawrdAnalog < 2500) 
    {
      Serial.println("flame dected a head");
      //FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
      Serial.println(FlameCheckForawrdAnalog);
      delay(200);
      CAR_moveForwardWifi();
      // front fire indicated by both siren on
      Serial.println("sirens on");
      sirenOn(LedBlue, LedRed, Buzzer);
      delay(200);
      Serial.println("pump on");
      digitalWrite(RELAY_PIN, HIGH);}```


pump off
sirens off
StopCar
3979
pump off
sirens off
StopCar
3665
pump off
sirens off
StopCar
2359
pump off
sirens off
StopCar
98
flame dected a head
98
sirens on
pump on
4095
pump off
sirens off

thats the log i get.
what i was going for is car moving forward until the value gets below 2500, then stops.
what happens in practise is ,as soon as the front sensor triggered i turn off the flame,
but it takes around 13 secs before it registered, and to car moves well into the flame.

the second problem is in the right sensor piece of code

 else if(FlameCheckRight == LOW)
    {
      Serial.println("flame dected on the right");
      Serial.println("turning right");
      CAR_turnRightWifi();
      Serial.println("siren on");
      sirenOn(LedBlue,LedRed, Buzzer);
      //updated front sensor value
      //FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
      //Serial.println(FlameCheckForawrdAnalog);

      if (FlameCheckForawrdAnalog < 2500) 
        {
          FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
          Serial.println(FlameCheckForawrdAnalog);
          Serial.println("sirens off");
          sirenoff(LedBlue, LedRed, Buzzer);
          StopCar();
        }
      delay(200);
        
    }    

with log

4095
pump off
sirens off
StopCar
4095
flame dected on the right
turning right
siren on

here the idea was to turn right until front sensor is triggered and then stop.
the problem is similar. as the the right triggered , the right turn never stops, even with flame in front of the front trigger.

i might be misunderstanding how the loop in the ide works.
thats my current understanding.
lets say the first if fulfilled, then the nested if within it fulfilled.
then the code goes back to the beginning of the loop. not the if that comes before the nested one.

is it even possible to control the movement using the analog option?
is it a hardware limitation, being not refined enough the control movement?

should i just abandon the analog control, use only digital with delay function?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.