Pages: 1 2 [3]   Go Down
Author Topic: [SOLVED]Checking multiple variables for "states" and then lighting an led  (Read 403 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50367
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm going to try to add it some comments, then see what i can do about making these outputs...
Perhaps the reverse order would make more sense. On the other hand, making switch pins OUTPUT doesn't make sense. Perhaps making the pins OUTPUT and the array name more meaningful would.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm,  I figured why i was doing the easy think (making comments) you'd have a suggestion for the hard thing (figuring out how to make arrays an output)


ok, I *think* i've made the button array pins an output.  At least, the LEDs are bright now. Thanks for pointing that out.


the LED's still blink fast though sometimes. They do it when i switch from one button to another, or if i give it some time (say like 30 seconds or so). If I press the button multiple times, it eventually slows down again.


* LEDbox3.ino (11.67 KB - downloaded 2 times.)
« Last Edit: September 17, 2014, 06:13:07 pm by Qdeathstar » Logged

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

Quote
ok, I *think* i've made the button array pins an output.  At least, the LEDs are bright now.
I don't understand why the LEDs are connected to pins defined in the buttonPin array. Why is the name NOT ledPin?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the LEDs are integrated with the buttons.

Later on, I am going to have actual LED strips that are seperate from the LED light on the buttons.  The LED light on the button are used to let the USER know what state the LED strips are in.

Also, i changed it from "buttonPin" to "buttonLEDpin"

Give me a few minutes and i will whip out my ipad take a picture of my project, with the leds.



Hopefully this kinda helps?

« Last Edit: September 17, 2014, 06:44:31 pm by Qdeathstar » Logged

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

Quote
Also, i changed it from "buttonPin" to "buttonLEDpin"
The LED portion of the device is connected to one pin, and the switch portion is connected to a different pin, right?

So, the fact that the LED and switch are in the same physical container is immaterial, isn't it? The name is better, but it is still confusing.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Right, but if i made it just "led pin" you might loose that it was related to the buttons, i thought.
Logged

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

Right, but if i made it just "led pin" you might loose that it was related to the buttons, i thought.
So, how about pinForLedAtSwitch?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.  I made the change to the variable, but the LED's are still blinking fast (after a while or when one button is pressed after another was pressed).


* LEDbox3.ino (11.71 KB - downloaded 1 times.)
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will restate the problem for anyone (Robin) who comes along later.  Paul solved the dimming problem, but:

The lights blink slowly only some of the time. If I let arduino run for a while, then press a button in blinks really fast. Likewise, if as soon as the arduino is reset, if I press "button 1"  it will blink slow as expected, but if I then press "button 2" button button two will blink fast.

I declared my arrays like this:
Code:
byte buttonLEDArray[] = {
  0, 0, 0, 0};

byte buttonLED1 = 0;
byte buttonLED2 = 1;
byte buttonLED3 = 2;
byte buttonLED4 = 3;

//the actual pin locations for the LED lights
byte pinForLedAtSwitch[] = {31, 33, 35, 37};
  
  
//turn off the LED lights initially
byte buttonLEDstate[] = {
  LOW, LOW, LOW, LOW};

//timers for the blinking of the lights when buttonLEDArray values are 2 or 3
unsigned long ledPrevMillis[] = {
  1000,1000,1000,1000};
unsigned long ledInterval[] = {
  1000,1000,1000,1000};

and this takes care of the blinking

Code:
void blinker(){

  for (byte n = 0; n < 4; n++) {
    digitalWrite(pinForLedAtSwitch[n], buttonLEDstate[n]);
    //if the Check button function changed the buttonLEDArray values to 2 or 3 (blinking states), go ahead and blink, as long as time has passed.
    if (((buttonLEDArray[n] == 2) || (buttonLEDArray[n] == 3)) && ((millis() - ledPrevMillis[n]) >= ledInterval[n])){
      //Serial.print(ledPrevMillis[3]);
      ledPrevMillis[n] += ledInterval[n];
      //  Serial.println(ledPrevMillis[3]);
      buttonLEDstate[n] = ! buttonLEDstate[n];
    }
    //if the button is indicated off, turn it off.
    if (buttonLEDArray[n] == 0) {
      digitalWrite(pinForLedAtSwitch[n], LOW);
      Serial.print("Hello1");
    }
    //if the button is indicated on, turn it on.
    if (buttonLEDArray[n] == 1) {
      digitalWrite(pinForLedAtSwitch[n], HIGH);
      Serial.print("Hello2");
    }
  }
}

* LEDbox3.ino (11.71 KB - downloaded 1 times.)
Logged

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

Code:
byte pinForLedAtSwitch[] = {31, 33, 35, 37};
This is a great name. It's easy to see that this is an array, of pin numbers, and that the pins correspond to LEDs that are associated with switches.

Code:
byte buttonLEDArray[] = {  0, 0, 0, 0};
This, however, is not a good name. It's obvious from the context (and the []) that this is an array. We really don't need Array as part of the name. But, what does buttonLED mean? Is that supposed to be the state of the LED? The color? The name does not suggest anything.

The reason that this is important is that it makes deciphering this code:
Code:
    if (((buttonLEDArray[n] == 2) || (buttonLEDArray[n] == 3)) && ((millis() - ledPrevMillis[n]) >= ledInterval[n])){
impossible.

One thing that would help is to shorten this line, considerably, AND use a switch statement in the for loop, not a bunch of if statements.

Another thing, already mentioned, is a meaningful name for the array. I'd guess that the name should be something like ledAction.

Code:
switch(ledAction[n])
{
   case 0: // steady on
      // Do something
      break;
   case 1: // off
      // Do something else
      break;
   case 2: // blinking
   case 3: // blinking
      // do the blinky thing, IF ITS TIME
      break;
}

This separates the decision regarding what to do from the decision regarding whether it is time to do whatever needs to be done. And the comments (if I've guessed at the meaning of the names, etc. correctly) make it clear what the actions are.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just an update, that so far everything works the way i want. The switch case made things a lot easier to understand, Paul. As did changing the variable names (i spent less time scrolling to the top of the sketch)

Still a lot of work to do though before my project is done.

Thanks.

* LEDbox3.ino (10.48 KB - downloaded 0 times.)
« Last Edit: September 19, 2014, 07:28:00 pm by Qdeathstar » Logged

Pages: 1 2 [3]   Go Up
Jump to: