Arduino Forum

Using Arduino => Programming Questions => Topic started by: andyreww on Aug 02, 2020, 12:12 am

Title: Selector Switch and Blicking LED Code Help
Post by: andyreww on Aug 02, 2020, 12:12 am
Hi Everyone,

I'm hoping someone can help with this code - its my first Arduino project and I'm stuck having spent a while trying to get this working - here's the code:


Code: [Select]
// Declare Variables

int grnLEDPin=12; // Grn Pin Variable
int grnONTime=50; // Grn On Time
int grnOFFTime=100; // Grn Off Time
int flashes = 0;
int counter = 0;
int voltage1 = 0;
int voltage2 = 0;

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(300); 
pinMode(grnLEDPin, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
voltage1 = int(voltage + 0.5);
// This section of code is just for troubleshooting - it will be deleted
String strVoltage1 = " : Voltage #1";
String strCombo1 = voltage1 + strVoltage1;
Serial.println(strCombo1);
String strVoltage2 = " : Voltage #2";
String strCombo2 = voltage2 + strVoltage2;
Serial.println(strCombo2);
String strFlashes = " : Flashes";
String strCombo3 = flashes + strFlashes;
Serial.println(strCombo3);
String strCounter = " : Counter";
String strCombo4 = counter + strCounter;
Serial.println(strCombo4);
// End of the code to delete
// if statements to flash LED based upon switch position
  if (voltage1!=voltage2) {
    switch (voltage1) {
      case 0:
        flashes = 0;
        counter = 0;
        break;
      case 1:
        flashes = 1;
        counter = 0;
        break;
      case 3:
        flashes = 2;
        counter = 0;
        break;
      case 4:
        flashes = 3;
        counter = 0;
        break;
      case 5:
        flashes = 4;
        counter = 0;
        break; }
      if (counter < flashes); {
          digitalWrite(grnLEDPin, HIGH);
          delay(grnONTime);
          digitalWrite(grnLEDPin, LOW);
          delay(grnOFFTime);
          ++counter;}   
      voltage2 = voltage1;
  }
}


I'm trying to accomplish the following:

I have a selector switch which is an analog input (using resistors) to the Arduino Nano. When I change the position of the selector switch I would like the LED to flash a number of times depending on the position of the switch. To accomplish this I detect a change using voltage1 and voltage2 which have been converted to int. Based upon voltage 1's value I then set 2 variables (flashes and counter). I then use these  in an if statement to flash the LED a number of times corresponding to the position of the selector switch. I created some lengthy code to monitor what's going on and I see voltage1 change, I then see a difference between voltage1 and voltage2, flashes then changes however; the LED only ever flashes once and the counter variable only increments to 1 - I have a hunch that the location of the if statement code that flashes the LED may be the issue.

Can anyone spot what the problem??

Many Thanks in Advance
Title: Re: Selector Switch and Blicking LED Code Help
Post by: pcbbc on Aug 02, 2020, 12:38 am
Yes, your flashing code is inside the if which checks voltage1!=voltage2.
Next time through loop you you think that will be true or false if the voltage is the same?

Edit: +Karma for using code tags on a first post.
Title: Re: Selector Switch and Blicking LED Code Help
Post by: vaj4088 on Aug 02, 2020, 12:49 am
+1 for using code tags the first time out.

-1 for this bit of code (comment does not match the code):

Code: [Select]

// initialize serial communication at 9600 bits per second:
Serial.begin(300);


voltage1 is an int, so the int function is redundant in this bit of code:

Code: [Select]

voltage1 = int(voltage + 0.5);


In this code,

Code: [Select]

      if (counter < flashes); {
          digitalWrite(grnLEDPin, HIGH);
          delay(grnONTime);
          digitalWrite(grnLEDPin, LOW);
          delay(grnOFFTime);
          ++counter;}   


the first semicolon in the "if" statement ends the "if" statement and is a common failing.  This should look like

Code: [Select]

      if (counter < flashes) {
          digitalWrite(grnLEDPin, HIGH);
          delay(grnONTime);
          digitalWrite(grnLEDPin, LOW);
          delay(grnOFFTime);
          ++counter;
      }   


Moving the right squiggly bracket "}" is unnecessary but is more clear to anyone reading the program... including you, six months from now.

By the way, there is a good chance that electrical noise is going to make the voltage change even when it has not.  Consider, for example, 1.49 volts and 1.50 volts.  In the first case, voltage1 will be 1.  In the second case, voltage1 will be 2.  Should this be different enough to cause the LED to flash?
The statement
if (voltage1!=voltage2)
should probably allow some "wiggle room", perhaps (untested)
if (abs(voltage1-voltage2)>1)
or something like that.


Good Luck!
Title: Re: Selector Switch and Blicking LED Code Help
Post by: andyreww on Aug 02, 2020, 02:33 am
Thanks for the help guys!
I implemented the advice you gave and the code now works!
Title: Re: Selector Switch and Blicking LED Code Help
Post by: vaj4088 on Aug 02, 2020, 05:40 am
I am happy for you that you got it working.  It would be nice if you would post what worked.