Pages: 1 [2]   Go Down
Author Topic: Arduino/Relay control, digitalWrite problems  (Read 1215 times)
0 Members and 1 Guest are viewing this topic.
Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can find a good example of using a variable to keep a state in the blink without delay sketch.
Logged

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

Ha, well this is embarrassing. When I attempted to upload my updated code, i forgot that my Bluetooth receiver was plugged in, and on. That was stopping the code from being uploaded to the Arduino. Thanks to everyone who gave input and suggestions to help my naivety. So here is my present code...
Code:
int pinA = 4;
int pinB = 5;
int pinC = 8;
int pinD = 7;
byte command = 0;
const int rHigh = 800;
const int rLow = 200;
int randomPin;
dud
void setup()
{               
  pinMode(pinA, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0)
  {
  command = Serial.read();
  switch (command)
    {
     case 1:
      if (digitalRead(pinA) == LOW)
      {
      digitalWrite(pinA, HIGH);
      }
      else
      {
      digitalWrite(pinA, LOW);
      }
      break;
     case 2:
      if (digitalRead(pinB) == LOW)
      {
      digitalWrite(pinB, HIGH);
      }
      else
      {
      digitalWrite(pinB, LOW);
      }
      break;
     case 3:
      if (digitalRead(pinC) == LOW)
      {
      digitalWrite(pinC, HIGH);
      }
      else
      {
      digitalWrite(pinC, LOW);
      }
      break;
     case 4:
      if (digitalRead(pinD) == LOW)
      {
      digitalWrite(pinD, HIGH);
      }
      else
      {
      digitalWrite(pinD, LOW);
      }
      break;
     case 5:
      do
        {
        randomPin = random(pinA, pinD + 1);
        digitalWrite(randomPin, HIGH);
        delay(random(rLow, rHigh));
        digitalWrite(randomPin, LOW);
        }
        while (command == 5);
      break;
     case 6:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      break;
     case 7:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, LOW);
      break;
    }
  }
}

Now the problem I am facing is two-fold. The 'random' portion of the code (case 5) is unstoppable unless I press the reset button. How would i make it so sending a 6 or a 7 would exit the random flashing by turning all relays on or off, but until then, it would stay in a loop? And lastly, for some odd reason case 3 doesn't let me turn off the relay when i send a 3 again, does anyone see a coding error i missed?
Logged

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

Quote
The 'random' portion of the code (case 5) is unstoppable unless I press the reset button.
Because you haven't put anything in the while loop to break out of it. You've been told repeatedly now that you need to do that.

Quote
And lastly, for some odd reason case 3 doesn't let me turn off the relay when i send a 3 again, does anyone see a coding error i missed?
You need to be pressing the key when the 3 arrives. That hardly seems likely.

Reading the state of the switches only when there is serial data to read, and it is some specific value is wrong. You need to read the state of the switches (all of them) independently of whether there is serial data, or what that value is.

Second, you really need to change the value in command only when there is serial data, but execute the switch statement on EVERY pass through loop.

Third, you need to read up on the difference between while and do/while. Where you have the do/while loop should be a while loop IF there needs to be one at all. I'm not convinced that there should be one there at all.
Logged

Pages: 1 [2]   Go Up
Jump to: