Hello,
Having built Arduino car with police siren. my problem is .Both codes work individually,
When joined together motors work but siren doesn't. There is something missing although code loads ok.
As a infant in coding can anyone help.
Al
Pensioner
The hardware layout and your code might help.
Tim.
As a new boy in coding and (91 years old, I use Arduino to keep me active, I try to code myself. But mostly I copy and paste.
Note.
Will forward code. COMPONENTS ARE= ESP32 -L298-BLUETOOTH.
I'm 74, so I understand. I've been working with computers since 1973 and programming since 1983.
Can you post the two sketches that work and the one that doesn't?
char t,f,d;
void setup() {
pinMode(13,OUTPUT); //left motors forward
pinMode(12,OUTPUT); //left motors reverse
pinMode(11,OUTPUT); //right motors forward
pinMode(10,OUTPUT); //right motors reverse
pinMode(9,OUTPUT); //siren on or off
Serial.begin(9600);
}
void siren(){
for(f=635;f<=912;f++)
tone(9, f);
delay(d);
if (digitalRead(2)==HIGH)
f=912;
if (digitalRead(3)==HIGH)
d=0;
else
d=7;
for(f=911;f>=634;f--)
tone(9, f);
delay(d);
if (digitalRead(2)==HIGH)
f=634;
if (digitalRead(3)==HIGH)
d=0;
else
d=10;
while (digitalRead(2)==HIGH)
{
noTone(9);
tone(9, 250);
delay(5);
noTone(9);
delay(5);
}
}
void loop() {
if(Serial.available()){
t = Serial.read();
Serial.println(t);
}
if(t == 'F'){ //move forward(all motors rotate in forward direction)
digitalWrite(13,HIGH);
digitalWrite(11,HIGH);
}
else if(t == 'B'){ //move reverse (all motors rotate in reverse direction)
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
}
else if(t == 'L'){ //turn right (left side motors rotate in forward direction, right side motors doesn't rotate)
digitalWrite(11,HIGH);
}
else if(t == 'R'){ //turn left (right side motors rotate in forward direction, left side motors doesn't rotate)
digitalWrite(13,HIGH);
}
else if(t == 'S'){ //STOP (all motors stop)
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
}
delay(100);
}
How is this supposed to break out of this while loop? If D2 is HIGH, this kills your program.
Edit: Never mind. The coffee is still kicking in.
The direction code seems a bit weird. You are setting the direction without stopping the opposite direction. Seems if you want to turn, you must stop the wheel (set the pin LOW) before setting the wheel HIGH.
To turn right, stop the right wheel (LOW) and start the left wheel (HIGH).
To turn left, stop the left wheel (LOW) and start the right wheel (HIGH).
Your code is difficult to read as posted.
In the IDE, click on Edit, then Copy for Forum, then paste you code(s) here
I was just reading about ESP32, motors and tone(). Seems that ESP32 uses LEDC (LED control) which, in turn, uses timers that must be configured (through an API?)... causing motors with tone() to malfunction.
https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/ledc.html