I am using an arduino uno, with a dual h bridge motor controller to control an RC bulldozer, I am using a code from indestructibles, it works very well but I would like to a add reversing alarm .
I think I know what I need to do but have no idea how to write the code !
This is what I think needs to happen:
When the 2 pins for reverse are low/high turn on pin connected to buzzer? But I would also like the buzzer to be 1 sec on 1 sec off.
All I've seen is a 5 line snippet, with no context.
You really should start with a sketch that does nothing but make the thing make the kind of noise you want. Might even want to go simpler - make the thing make ANY kind of noise. At least that way, you'll know that the device is connected correctly, and can make (obnoxious) noises.
The code i have converts the signal from a RC receiver in to signals a dual H bridge motor driver can use, control 2 DC motors on a radio controlled bulldozer i tried to post the whole code up it was to big.
In a nut shell I am trying to insert the basic pin 13 LED blink sketch in to it, but only have it start when both motors are running in reverse.
First thing, is to follow PaulS's advice from reply #5. Write a simple sketch which does not use delay(), and runs the buzzer.
Look carefully at your code. You will see that the switch case for gDirection code only runs when the gDirection or gGear is changed, so you just can't put the buzzer function into the case DIRECTION_REVERSE as the code will not loop through that case.
Here's one way to work with that situation.
Set up a global boolean variable soundBackUpBuzzer and initialize it to false. Set it equal true in the DIRECTION_REVERSE case. Set it equal false, in all the other cases.
Then add a section to your code
if(soundBackUpBuzzer == true)
{
// run the non blocking code you wrote to sound the buzzer
}
Ok,
I am now using a different motor driver and sketch DRV8835 to be exact, managed to edit a sketch found on line to convert the rx out put to the correct signal for the driver. Which i am very happy with by the way.
I have almost got a reversing buzzer working! I can switch on the led at pin 13 when in reverse, but also when turning left.
I have tried various combinations in the last 2 digitalwrite lines in sketch below which either have the LED on all the time or not on at all!
Some assistance would be greatly appreciated.
int thrPin = 2;
int ailPin = 3;
int leftPWMPin = 9;
int leftDirPin = 7;
int rightPWMPin = 10;
int rightDirPin = 8;
int buzzerPin= 13;
unsigned long thrLen, ailLen;
float thrPos, ailPos, rightVal, leftVal;
float clamp = 0.25;
int minLen = 1004;
int maxLen = 1976;
void setup()
{
pinMode(thrPin, INPUT);
pinMode(ailPin, INPUT);
pinMode(leftDirPin, OUTPUT);
pinMode(rightDirPin, OUTPUT);
pinMode(leftPWMPin, OUTPUT);
pinMode(rightPWMPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Hello world!");
}
void loop()
{
thrLen = pulseIn(thrPin, HIGH);
ailLen = pulseIn(ailPin, HIGH);
thrPos = ((float)thrLen-minLen)/(maxLen-minLen)*2 - 1;
ailPos = ((float)ailLen-minLen)/(maxLen-minLen)*2 - 1;
/*
Serial.println("throttle, aileron:");
Serial.println(thrLen);
Serial.println(ailLen);
*/
leftVal = min(max(thrPos - ailPos, -1), 1);
rightVal = min(max(thrPos + ailPos, -1), 1);
if (leftVal < clamp and leftVal > -clamp) leftVal = 0;
if (rightVal < clamp and rightVal > -clamp) rightVal = 0;
Serial.println("left, right");
Serial.println(leftVal);
Serial.println(rightVal);
digitalWrite(leftDirPin, leftVal < 0 ? HIGH : LOW);
analogWrite(leftPWMPin, abs(leftVal) * 255);
digitalWrite(rightDirPin, rightVal < 0 ? HIGH : LOW);
analogWrite(rightPWMPin, abs(rightVal) * 255);
digitalWrite(buzzerPin, leftVal < 0 ? HIGH :LOW );
digitalWrite(buzzerPin, rightVal < 0 ? HIGH :LOW);
delay(100);
}
digitalWrite(buzzerPin, leftVal < 0 ? HIGH :LOW );
digitalWrite(buzzerPin, rightVal < 0 ? HIGH :LOW);
The ternary operator is neat but you can use the assumption that HIGH is also the same as true. So when the comparison is true (HIGH) then you used the ternary to return HIGH.
Surely you want the reverse buzzer to be on when both left and right are negative, like, at the same time?