Go Down

Topic: if loop (Read 3164 times) previous topic - next topic

JAndersM

I think a better approach is set the state of the pin with the serial input and have the main loop react to the status. Otherwise it is hard to get the strobing to work properly.

Eg:

state = 0 // 0=Low, 1 = Strobe, 2=High

loop() {

//Set state from input
if (Serial.available()) {
char ser = Serial.read();
 
if(ser == 'a') state=2;
else if (ser == 'b') state=1;
else state=0;
}

// Do the output according to the state
switch (state) {
case 0:
// Set low
case 1:
//Set opposite to previous
case 2:
//Set high
}
delay(1000);
}

UKHeliBob

Quote
removed #define a HIGH; as I'm noticing it's not needed
and it was written wrongly anyway.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

cattledog

Quote
I'm just not sure how to code it properly.  I would also like to be able to add programming to control other keys to make it blink at different rates based on the key typed into the serial monitor.
In your current logic, you are always winding up at
Code: [Select]
else{
    digitalWrite (light, LOW);
    delay (1000);

and turning the light off when there is no serial input. Is that what you want?

I would recommend changing to switch / case statements http://arduino.cc/en/Reference/SwitchCase
rather than layered ifs and elseifs. Based on the serial input, you will switch to a different blink pattern.

You should take a look at the "blink without delay" in the digital examples section of the IDE, and learn how to use this timer approach in your coding.

Use this project as an opportunity to get away from using delay and to learn switch/case as a way to handle the logic.

welchsc

AndersM and luisilva it works perfectly thanks!! If I wanted to add another key stroke which syntax should I use (another else if statement)?  Also I have decoded signals from my remote control.  I would like to use them to turn on, off, and blink the light.  Do I need to use the string variable to read the codes in?  Again not sure of how the syntax would be written.

UKHeliBob

Quote
and turning the light off when there is no serial input.
Actually the code to turn off the LED is inside the check to see whether serial data is available, so any character, or LF/CR will turn off the LED and confuse things even further
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

welchsc

I'm confused...  I want to be able to add an additional character so that when I type it in the serial monitor it will cause something else to take place (i.e. blink faster, etc.).  Sorry for the confusion.

AWOL

#21
Dec 28, 2014, 10:15 pm Last Edit: Dec 28, 2014, 10:16 pm by AWOL
Your default action if you don't recognise an 'a' or a 'b' is to turn off, so if you send a carriage return or line feed, that's what'll happen.

Please use code tags when posting code.

JAndersM

AndersM and luisilva it works perfectly thanks!! If I wanted to add another key stroke which syntax should I use (another else if statement)?  Also I have decoded signals from my remote control.  I would like to use them to turn on, off, and blink the light.  Do I need to use the string variable to read the codes in?  Again not sure of how the syntax would be written.
Some notes:
As cattledog wrote, have a look at Blink without delay. The delay causes everything to halt including checking for serial input!

You, could have a variable strobetime (in millis.) and change that with a key stroke as one case in your switch-case statement that sets the state to make it blink at different frequencies..

Note that you can have characters, but not strings, as switch variable. (See AWOLs reply here) So you can write a switch-case block for setting the states based on the keys.

JAndersM

#23
Dec 29, 2014, 01:13 pm Last Edit: Dec 29, 2014, 01:16 pm by JAndersM
For my own education I made an example where you can change blinking (on time, off time) typing different chars. (Use no appended line ending in Serial Monitor)

Code: [Select]
const int ledPin=13;

unsigned long currentMillis;
unsigned long previousMillis;

//Start with LED off
unsigned long timeOn = 0;
unsigned long timeOff = 4294967295;
char currentcase = 'a';

void setup() {
  Serial.begin(9600);
  previousMillis = millis();
  pinMode(ledPin, OUTPUT);
}

void loop() {
  //New case input check
  if (Serial.available() > 0) {
    currentcase = Serial.read();
    switch (currentcase) {
      case 'a':              // Off
        timeOn = 0;
        timeOff = 4294967295;
        break;
      case 'b':              // On
        timeOn = 4294967295;
        timeOff = 0;
        break;
      case 'c':             // Iso
        timeOn = 500;
        timeOff = 500;
        break; 
     case 'd':             // Long on short off
        timeOn = 500;
        timeOff = 250;
        break;     
    }
    Serial.println(currentcase);
  }

  // Check pin state and change if time is up
  currentMillis= millis();
  switch(digitalRead(ledPin)) {
    case LOW :
      if (currentMillis-previousMillis >= timeOff) {
        digitalWrite(ledPin, HIGH);
        previousMillis = currentMillis;
      }
      break;
    case HIGH :
      if (currentMillis-previousMillis >= timeOn) {
        digitalWrite(ledPin, LOW);
        previousMillis = currentMillis;
      }
      break;
  }
}


More experienced probably can improve the code.

welchsc

Thanks everyone!  I will give it a try.

Go Up