Go Down

Topic: Some Help Please (Read 359 times) previous topic - next topic

dme916_2

Been working on this programme today and have now got a bit of a problem.

I am sending the characters, 'A' and 'D' using XBee modules.  When this data is received at the Arduino board I want to illuminate an LED.  At the moment though when I send the data the LED lights up but does not turn off.

Code below;

int SignalRecieved = 10;
int TrainApproaching = 11;
int Fault = 12;
int BELL = 13;
int myData = 0;

void setup()
{
pinMode(SignalRecieved, OUTPUT);
pinMode(TrainApproaching, OUTPUT);
pinMode(Fault, OUTPUT);
pinMode(BELL, OUTPUT);
Serial.begin(9600);
}

void loop()
{
  if (Serial.available()>0)
  {
    myData = Serial.read();
  }

  if (myData == 'D')
  {
    digitalWrite(SignalRecieved, HIGH);
    digitalWrite(BELL, HIGH);
    delay (1000);
    digitalWrite(BELL,LOW);
    delay (2000);
    digitalWrite (TrainApproaching, LOW);
    digitalWrite (Fault, LOW);
  }
 
  while (myData == 'A')
  {
    digitalWrite (TrainApproaching, HIGH);
    digitalWrite(BELL, HIGH);
    digitalWrite(SignalRecieved, LOW);
    digitalWrite(Fault, LOW);
  }
 
  if (myData != 'A')
  {
    digitalWrite (Fault, HIGH);
    digitalWrite(BELL, HIGH);
  }
 
  if (myData != 'D')
  {
    digitalWrite (Fault, HIGH);
    digitalWrite(BELL, HIGH);
  }   
}

many thanks

PaulS

Code: [Select]
while (myData == 'A')
  {
    digitalWrite (TrainApproaching, HIGH);
    digitalWrite(BELL, HIGH);
    digitalWrite(SignalRecieved, LOW);
    digitalWrite(Fault, LOW);
  }

If you receive an 'A' this while loop will be entered, and will never exit.

What does the sending?

dme916_2

at the moment my computer with in terminal server but evently, a infrared sensor conected to a arduino uno

GoForSmoke

dme, you are so close to having it right

Let the job be a real-time journey with no fixed end. Then loop() is a single turn of the wheel.

The wheel may turn many times between each serial char becomes available.

The code inside loop() should be enough to do anything you need yet for any turn of the wheel only do what is needed right then.

Once you digitalWrite a pin, it stays that way.
Read up on else to go with if:
http://arduino.cc/en/Reference/Else
and read up on switch-case:
http://arduino.cc/en/Reference/SwitchCase
They are short and will help with your future code.

Using delay() stops the wheel for a while. Someone nice here will show you how to use millis() to time events then you can do multiple things at once or you can read this:
http://www.gammon.com.au/forum/?id=11411

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up