Loop Help

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.

#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] frontr = 2;    
[color=#CC6600]int[/color] frontl = 4;    
[color=#CC6600]int[/color] rearl = 5;    
[color=#CC6600]int[/color] rearr = 6;    
[color=#CC6600]int[/color] topl = 7;    
[color=#CC6600]int[/color] topr = 8;    
[color=#CC6600]int[/color] incomingByte;
[color=#CC6600]int[/color] minPulse     =  600;  [color=#7E7E7E]// minimum servo position  [/color]
[color=#CC6600]int[/color] maxPulse     =  2400; [color=#7E7E7E]// maximum servo position  [/color]
[color=#CC6600]int[/color] turnRate     =  100;
[color=#CC6600]int[/color] centerServo; 
[color=#CC6600]int[/color] pulseWidth; 




[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](frontr, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](frontl, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](rearl, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](rearr, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](topl, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](topr, [color=#006699]OUTPUT[/color]);
  steer.[color=#CC6600]attach[/color](11);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
  centerServo = maxPulse - ((maxPulse - minPulse)/2);  


}

[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=#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](frontr, [color=#006699]LOW[/color]);  
      [color=#CC6600]digitalWrite[/color](frontl, [color=#006699]LOW[/color]);  
      [color=#CC6600]digitalWrite[/color](rearl, [color=#006699]LOW[/color]);  
      [color=#CC6600]digitalWrite[/color](rearr, [color=#006699]LOW[/color]);  
      [color=#CC6600]digitalWrite[/color](topl, [color=#006699]LOW[/color]);  
      [color=#CC6600]digitalWrite[/color](topr, [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](frontr, [color=#006699]HIGH[/color]);  
    } 
    [color=#CC6600]if[/color] (incomingByte == [color=#006699]'U'[/color]) {
      [color=#CC6600]digitalWrite[/color](frontl, [color=#006699]HIGH[/color]);  
    } 
    [color=#CC6600]if[/color] (incomingByte == [color=#006699]'H'[/color]) {
      [color=#CC6600]digitalWrite[/color](rearl, [color=#006699]HIGH[/color]);  
    } 
    [color=#CC6600]if[/color] (incomingByte == [color=#006699]'J'[/color]) {
      [color=#CC6600]digitalWrite[/color](rearr, [color=#006699]HIGH[/color]);  
    } 
    [color=#CC6600]if[/color] (incomingByte == [color=#006699]'N'[/color]) { 
      [color=#CC6600]digitalWrite[/color](topl, [color=#006699]HIGH[/color]);  
    } 
    [color=#CC6600]if[/color] (incomingByte == [color=#006699]'M'[/color]) { 
      [color=#CC6600]digitalWrite[/color](topr, [color=#006699]HIGH[/color]);  
    }
    [color=#CC6600]if[/color] (incomingByte == [color=#006699]'X'[/color]) {    
      [color=#CC6600]digitalWrite[/color](topl, [color=#006699]LOW[/color]);   
      [color=#CC6600]digitalWrite[/color](topr, [color=#006699]HIGH[/color]);  
      [color=#CC6600]delay[/color](200);
      [color=#CC6600]digitalWrite[/color](topl, [color=#006699]HIGH[/color]); 
      [color=#CC6600]digitalWrite[/color](topr, [color=#006699]LOW[/color]);   
      [color=#CC6600]delay[/color](200);

    }

    [color=#CC6600]if[/color] (incomingByte == [color=#006699]'Z'[/color]) {


      [color=#CC6600]digitalWrite[/color](buzzer, [color=#006699]LOW[/color]);   
      [color=#CC6600]delay[/color](200);
      [color=#CC6600]digitalWrite[/color](buzzer, [color=#006699]HIGH[/color]); 
      [color=#CC6600]delay[/color](200);

    }
  }
}



[/quote]

at the bit where it goes

[quote]
    [color=#CC6600]if[/color] (incomingByte == [color=#006699]'Z'[/color]) {


      [color=#CC6600]digitalWrite[/color](buzzer, [color=#006699]LOW[/color]);   
      [color=#CC6600]delay[/color](200);
      [color=#CC6600]digitalWrite[/color](buzzer, [color=#006699]HIGH[/color]); 
      [color=#CC6600]delay[/color](200);

    }

i want to have the buzzer turn on and off like a car alarm until i press Q to stop it. can somone tell me how to do this?
thanks
James

a bit more info

i need to have this running but also beable to drive the car with the wasd keys, i would also like to have the lights going

You need to separate the actual buzzer operation from the need to have the buzzer buzz:

Before setup:

boolean buzzerNeedsToBuzz = false;

In loop:

if(incomingByte == 'Z')
{
   buzzerNeedsToBuzz = true;
}

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.

Thankyou

This is my new Sketch

#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? :-/

Well, for one thing, you set buzzerNeedsToBuzz, but then you go ahead and buzz the buzzer whether it needs to be buzzed or not.

Second, you only buzz the buzzer if there is serial data to read. That seems a strange thing to do...

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"

sorry i dont understand :-/

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).

im relitivly new to all this,

awol i am using the copy for forum menu in the arduino and it adds the quots automatically,
what is a switch case, and how would i use it?

i am using the copy for forum menu in the arduino and it adds the quots automatically

You're allowed to delete them and use the correct "code" instead.

ok thanks
pauls
what did you mean by this?

Well, for one thing, you set buzzerNeedsToBuzz, but then you go ahead and buzz the buzzer whether it needs to be buzzed or not.

Second, you only buzz the buzzer if there is serial data to read. That seems a strange thing to do...

how would you recommend doing it?

void loop()
{
   if(Serial.available() > 0)
   {
      // collect the serial data
   }

   if(buzzerNeedsToBuzz)
   {
      // Do the buzzer stuff
   }
}

I am sorry if i seam stupid its just that i am very new to programming and am trying to teach my self!

I have re-Written the code to include switch cases which simplifies it a bit.

here is the new Code

#include <Servo.h> 
Servo steer;
int back = 10;
int forward = 9;                             
int buzzer = 3;    
int roofled1 = 2;    
int roofled2 = 4;    
int frontled1 = 5;    
int frontled2 = 6;    
int backled1 = 7;    
int backled2 = 8;    
int incomingByte;
boolean buzzerNeedsToBuzz = false; 
int buzState = LOW;             
long previousMillis = 0; 
long interval = 1000; 

void setup() {

  pinMode(back, OUTPUT);
  pinMode(forward, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(roofled1, OUTPUT);
  pinMode(roofled2, OUTPUT);
  pinMode(frontled1, OUTPUT);
  pinMode(frontled2, OUTPUT);
  pinMode(backled1, OUTPUT);
  pinMode(backled2, OUTPUT);
  steer.attach(11);

  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    switch (incomingByte) {
    case 'W':
      digitalWrite(forward, HIGH);   
      digitalWrite(back, LOW);  
      break;
    case 'S':
      digitalWrite(forward, LOW);   
      digitalWrite(back, HIGH);  
      break;
    case 'A':
      steer.write(40);   

      break;
    case 'D':
      steer.write(50);
      break;
    case 'B':
      digitalWrite(buzzer, HIGH);  
      break;
    case 'Y':
      digitalWrite(roofled1, HIGH);  
      break;
    case 'U':
      digitalWrite(roofled2, HIGH);  
      break; 
    case 'H':
      digitalWrite(frontled1, HIGH);  
      break; 
    case 'J':
      digitalWrite(frontled2, HIGH);  
      break; 
    case 'N': 
      digitalWrite(backled1, HIGH);  
      break;
    case 'M': 
      digitalWrite(backled2, HIGH);  
      break;
    default:
      digitalWrite(forward, LOW);   
      digitalWrite(back, LOW);  
      digitalWrite(buzzer, LOW);  
      digitalWrite(roofled1, LOW);  
      digitalWrite(roofled2, LOW);  
      digitalWrite(frontled1, LOW);  
      digitalWrite(frontled2, LOW);  
      digitalWrite(backled1, LOW);  
      digitalWrite(backled2, LOW);
    }
  }
}

can you tell me how to incorprait the code in your prevois post into my new code please.

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.

is this right?

#include <Servo.h>
Servo steer;
int back = 10;
int forward = 9;                            
int buzzer = 3;    
int roofled1 = 2;    
int roofled2 = 4;    
int frontled1 = 5;    
int frontled2 = 6;    
int backled1 = 7;    
int backled2 = 8;    
int incomingByte;
boolean buzzerNeedsToBuzz = false;


void setup() {

  pinMode(back, OUTPUT);
  pinMode(forward, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(roofled1, OUTPUT);
  pinMode(roofled2, OUTPUT);
  pinMode(frontled1, OUTPUT);
  pinMode(frontled2, OUTPUT);
  pinMode(backled1, OUTPUT);
  pinMode(backled2, OUTPUT);
  steer.attach(11);

  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    switch (incomingByte) {
    case 'W':
      digitalWrite(forward, HIGH);  
      digitalWrite(back, LOW);  
      break;
    case 'S':
      digitalWrite(forward, LOW);  
      digitalWrite(back, HIGH);  
      break;
    case 'A':
      steer.write(40);  

      break;
    case 'D':
      steer.write(50);
      break;
    case 'B':
      digitalWrite(buzzer, HIGH);  
      break;
    case 'Y':
      digitalWrite(roofled1, HIGH);  
      break;
    case 'U':
      digitalWrite(roofled2, HIGH);  
      break;
    case 'H':
      digitalWrite(frontled1, HIGH);  
      break;
    case 'J':
      digitalWrite(frontled2, HIGH);  
      break;
    case 'N':
      digitalWrite(backled1, HIGH);  
      break;
    case 'M':
      digitalWrite(backled2, HIGH);  
      break;
    default:
      digitalWrite(forward, LOW);  
      digitalWrite(back, LOW);  
      digitalWrite(buzzer, LOW);  
      digitalWrite(roofled1, LOW);  
      digitalWrite(roofled2, LOW);  
      digitalWrite(frontled1, LOW);  
      digitalWrite(frontled2, LOW);  
      digitalWrite(backled1, LOW);  
      digitalWrite(backled2, LOW);
    }
  }
  if(buzzerNeedsToBuzz)
   {
      // Do the buzzer stuff
   }
}

That's the right place for the block. Now, just copy the code you had in reply #3, and put it in place of the comment.

thankyou that works fine, the question now s how to stop it? and get two leds alternating like a police car lights :slight_smile:

Well, you have a buzzer on command. Define a buzzer off command. Or, make the buzzer command a toggle.

For a toggle:

buzzerNeedsToBuzz = !buzzerNeedsToBuzz;

instead of:

buzzerNeedsToBuzz = true;

and now how to get two led's alternating like on a police car :wink:

dont worry done it
thanks very much for all your help ;D :Dhttp://www.arduino.cc/yabbfiles/Smilies/thumbsup.gif