Pages: [1]   Go Down
Author Topic: Some Help Please  (Read 319 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4809
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

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

Pages: [1]   Go Up
Jump to: