I am working on an rc car project and I have hooked up a ultrasonic sensor to my arduino with a buzzer and esc. What I need is if the TXValue is < 1400 (PWM) then the buzzer should start playing. The issue im getting is that the motor keeps running until the if() statment stops. Here is my code:
/*
Ruban Clare
Rc car project with arduino, Flysky FS-i6x, Flysky Reciver Module, ect.
*/
// Define Input Connections
#include <Servo.h> //Servo librarey to support ESC
Servo ESC; //ESC as Servo
#include "pitches.h"
int melody1[] = {
NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5,
NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_C6,
NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5, NOTE_AS5, NOTE_C6,
NOTE_AS5, NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6,
NOTE_GS5, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_DS5,
NOTE_F5, NOTE_FS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_DS5, NOTE_FS5, NOTE_F5
};
int noteDurations[] = {
4,8,4,8,8,8, 4,8,8,4,8,8, 4,8,4,8,8,8, 4,8,8,2, 4,8,4,8,8,8, 4,8,8,4,8,8, 4,8,8,4,8,8, 4,8,2
};
#define ONE_FOOT (12 * 2.54)
#define FOUR_FEET (ONE_FOOT * 3)
#define SIX_FEET (ONE_FOOT * 5)
#define CH1 2 //****************************done***********************************
#define TX 3 //CH2****************************done***********************************
#define CH7 4 //****************************done***********************************
#define CH8 5 //****************************done***********************************
#define CH9 6 //****************************done***********************************
#define CH10 7 //****************************done***********************************
#define ledRL A2 //****************************done***********************************
#define ledGR A3 //****************************done***********************************
#define HLight A4 //****************************done***********************************
#define CLight A1 //****************************done***********************************
#define Buzzer 12 //****************************done***********************************
// Boolean to represent switch value
bool ch7Value;
bool ch8Value;
bool ch9Value;
bool ch10Value;
// Integers to represent values from sticks and pots
int ch1Value;
int TXValue; //CH2
// Delays
int GRdelay = 250;
int RLdelay = 250;
int BuzzerDelay = 400;
//Ultrasonic Sensor
int trigPin = 8; //****************************done***********************************
int echoPin = 9; //****************************done***********************************
long duration;
int distance;
//FS-i6x STICKS READING
// Read the number of a specified channel and convert to the range provided.
// If the channel is off, return the default value
int readChannel(int channelInput, int minLimit, int maxLimit, int defaultValue){
int ch = pulseIn(channelInput, HIGH, 30000);
if (ch < 100) return defaultValue;
return map(ch, 1000, 2000, minLimit, maxLimit);
}
//FS-i6x SWITCH READING`
// Read the switch channel and return a boolean value
bool readSwitch(byte channelInput, bool defaultValue){
int intDefaultValue = (defaultValue)? 100: 0;
int ch = readChannel(channelInput, 0, 100, intDefaultValue);
return (ch > 50);
}
void setup(){
Serial.begin(2000000);
pinMode(CH1, INPUT);
pinMode(CH7, INPUT);
pinMode(CH8, INPUT);
pinMode(CH10, INPUT);
pinMode(ledRL, OUTPUT);
pinMode(ledGR, OUTPUT);
pinMode(HLight,OUTPUT);
pinMode(CLight,OUTPUT);
pinMode(TX, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
ESC.attach(10); //DigitalPin 10
}
void loop() {
//ESC CONTROL
TXValue = pulseIn(TX, HIGH);
ch10Value = readSwitch(CH10, false);
digitalWrite(trigPin, LOW);
delayMicroseconds(3);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
if (ch10Value == 0){
distance= duration*0.034/2;
}
else if(ch10Value == 1){
distance = 1000;
}
if (distance == 0 || distance > SIX_FEET){
// TXValue unchanged
}
else if (distance < FOUR_FEET){
TXValue = constrain(TXValue, 1300, 1500);
}
else if (distance < SIX_FEET){
TXValue = constrain(TXValue, 1400, 1600);
}
Serial.println(TXValue);
ESC.writeMicroseconds(TXValue);
//BACKUP BUZZER CODE
//ch3Value = readChannel(CH3, -100, 100, 0);
//Serial.println(ch3Value);
if(TXValue<1400){
for (int thisNote = 0; thisNote < 43; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(Buzzer, melody1[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.10;
delay(pauseBetweenNotes);
noTone(Buzzer);
}
}
else(TXValue>1480);{
noTone(Buzzer);
}
//CLIGHT CODE
ch9Value = readSwitch(CH9, false);
// Serial.println(ch9Value);
if(ch9Value > 0){
digitalWrite(CLight,HIGH);
}
else{
digitalWrite(CLight,LOW);
}
//HEADLIGHT CODE
ch8Value = readSwitch(CH8, false);
//Serial.println(ch8Value);
if(ch8Value > 0){
digitalWrite(HLight,HIGH);
}
else{
digitalWrite(HLight,LOW);
}
//EMERGANCY PARKING CODE
ch7Value = readSwitch(CH7, false);
//Serial.print(ch7Value);
if(ch7Value > 0){
digitalWrite(ledGR, HIGH);
digitalWrite(ledRL, HIGH);
delay(GRdelay);
digitalWrite(ledGR, LOW);
digitalWrite(ledRL, LOW);
delay(GRdelay) ;
}
// INDECATOR CODE
// Get values for each channel
ch1Value = readChannel(CH1, -100, 100, 0);
// Print to Serial Monitor
//Serial.println(ch1Value);
delay(150);
if (ch1Value > 10) {
//while(ch1Value > 10){
digitalWrite(ledRL, HIGH);
delay(GRdelay);
digitalWrite(ledRL, LOW);
delay(GRdelay);
// }
}
else if (ch1Value < -10) {
digitalWrite(ledGR, HIGH);
delay(RLdelay);
digitalWrite(ledGR, LOW);
delay(RLdelay);
}
//else if(ch1Value == 0) {
// digitalWrite(ledRL, LOW);
//digitalWrite(ledGR, LOW);
//
// }
}
The issue I am getting is:
if(TXValue<1400){
for (int thisNote = 0; thisNote < 43; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(Buzzer, melody1[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.10;
delay(pauseBetweenNotes);
noTone(Buzzer);
}
}
else(TXValue>1480);{
noTone(Buzzer);
}
Its just that if my motor is going lets say 10mph and I decide to go reverse, the if else statment wont stop it just keeps going and my remote value isnt reading until the buzzer is done playing its music.
Edit: I added the wrong code sorry i ment to put the original code (not messed with)