Please Can you help me?. i have some code that i have wirtten to control a remote controlled car which i have added lights and a buzzer to.
the code goes as so.
Then, you should look at the BlinkWithoutDelay example to see how it makes the LED flash without using delay, and make your buzzer buzz (if needed) without using delay.
#include <[color=#CC6600]Servo[/color].h>
[color=#CC6600]Servo[/color] steer;
[color=#CC6600]int[/color] back = 10;
[color=#CC6600]int[/color] forward = 9;
[color=#CC6600]int[/color] buzzer = 3;
[color=#CC6600]int[/color] roofled1 = 2;
[color=#CC6600]int[/color] roofled2 = 4;
[color=#CC6600]int[/color] frontled1 = 5;
[color=#CC6600]int[/color] frontled2 = 6;
[color=#CC6600]int[/color] backled1 = 7;
[color=#CC6600]int[/color] backled2 = 8;
[color=#CC6600]int[/color] incomingByte;
[color=#CC6600]boolean[/color] buzzerNeedsToBuzz = [color=#CC6600]false[/color];
[color=#CC6600]int[/color] buzState = [color=#006699]LOW[/color];
[color=#CC6600]long[/color] previousMillis = 0; [color=#7E7E7E]// will store last time LED was updated[/color]
[color=#CC6600]long[/color] interval = 1000; [color=#7E7E7E]// interval at which to blink (milliseconds)[/color]
[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
[color=#CC6600]pinMode[/color](back, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](forward, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](buzzer, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](roofled1, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](roofled2, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](frontled1, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](frontled2, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](backled1, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](backled2, [color=#006699]OUTPUT[/color]);
steer.[color=#CC6600]attach[/color](11);
[color=#7E7E7E]// start sending data at 9600 baud rate[/color]
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
}
[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]() {
[color=#7E7E7E]// check that there's something in the serial buffer[/color]
[color=#CC6600]if[/color] ([color=#CC6600][b]Serial[/b][/color].[color=#CC6600]available[/color]() > 0) {
[color=#7E7E7E]// read the byte and store it in our variable [/color]
[color=#7E7E7E]// the byte sent is actually an ascii value[/color]
incomingByte = [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]read[/color]();
[color=#7E7E7E]// note the upper casing of each letter![/color]
[color=#7E7E7E]// each letter turns a motor different way.[/color]
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'Q'[/color]) {
[color=#CC6600]digitalWrite[/color](forward, [color=#006699]LOW[/color]);
[color=#CC6600]digitalWrite[/color](back, [color=#006699]LOW[/color]);
[color=#CC6600]digitalWrite[/color](buzzer, [color=#006699]LOW[/color]);
[color=#CC6600]digitalWrite[/color](roofled1, [color=#006699]LOW[/color]);
[color=#CC6600]digitalWrite[/color](roofled2, [color=#006699]LOW[/color]);
[color=#CC6600]digitalWrite[/color](frontled1, [color=#006699]LOW[/color]);
[color=#CC6600]digitalWrite[/color](frontled2, [color=#006699]LOW[/color]);
[color=#CC6600]digitalWrite[/color](backled1, [color=#006699]LOW[/color]);
[color=#CC6600]digitalWrite[/color](backled2, [color=#006699]LOW[/color]);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'W'[/color]) {
[color=#CC6600]digitalWrite[/color](forward, [color=#006699]HIGH[/color]);
[color=#CC6600]digitalWrite[/color](back, [color=#006699]LOW[/color]);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'S'[/color]) {
[color=#CC6600]digitalWrite[/color](forward, [color=#006699]LOW[/color]);
[color=#CC6600]digitalWrite[/color](back, [color=#006699]HIGH[/color]);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'A'[/color]) {
steer.[color=#CC6600]write[/color](100);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'D'[/color]) {
steer.[color=#CC6600]write[/color](50);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'B'[/color]) {
[color=#CC6600]digitalWrite[/color](buzzer, [color=#006699]HIGH[/color]);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'Y'[/color]) {
[color=#CC6600]digitalWrite[/color](roofled1, [color=#006699]HIGH[/color]);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'U'[/color]) {
[color=#CC6600]digitalWrite[/color](roofled2, [color=#006699]HIGH[/color]);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'H'[/color]) {
[color=#CC6600]digitalWrite[/color](frontled1, [color=#006699]HIGH[/color]);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'J'[/color]) {
[color=#CC6600]digitalWrite[/color](frontled2, [color=#006699]HIGH[/color]);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'N'[/color]) {
[color=#CC6600]digitalWrite[/color](backled1, [color=#006699]HIGH[/color]);
}
[color=#CC6600]if[/color] (incomingByte == [color=#006699]'M'[/color]) {
[color=#CC6600]digitalWrite[/color](backled2, [color=#006699]HIGH[/color]);
}
[color=#CC6600]if[/color](incomingByte == [color=#006699]'Z'[/color])
{
buzzerNeedsToBuzz = [color=#CC6600]true[/color];
}
[color=#CC6600]if[/color] ([color=#CC6600]millis[/color]() - previousMillis > interval) {
previousMillis = [color=#CC6600]millis[/color]();
[color=#CC6600]if[/color] (buzState == [color=#006699]LOW[/color])
buzState = [color=#006699]HIGH[/color];
[color=#CC6600]else[/color]
buzState = [color=#006699]LOW[/color];
[color=#CC6600]digitalWrite[/color](buzzer, buzState);
}
}
}
the problem i still have is it is not going on and off and if i try to turn a light on or the motor the buzzer stops :-[
so close but no cigar, any other ideas? :-/
Couple of things:
Can you please use the Code button, and not the Quote button when posting code?
Your repeated "if" might look better as a "switch/case"
You've posted your code using the button labelled "Quote" (look like a sheet of paper with a right-pointing arrow), you should use the button labelled "Code" (looks like a "#" sign).
In reply#3, you had added code to buzz the buzzer. That code needs to be added in place of the comment in reply #11. The code in reply #12 already has the if serial available block. Before or after that block, put the if buzzer needs to buzz block.