Go Down

Topic: Trouble programming an ATtiny to control LEDs (Read 524 times) previous topic - next topic

musclenerdzllc

Thanks for the info!  I bread-boarded it all out and the trigger works now, but there's a problem with the code.  The action doesn't stick.  I can press the button and the LED's come on as they are supposed to with the choice sequence.  But when I let go of the button they all come on again 100%, which isn't right either.  If anything they should all turn off.  What am I doing wrong?

slipstick

Please post the current version of the code that you're using.

Steve

musclenerdzllc

Here's the code I am trying:



/ here are the pin number assignments

int blue = 2;

int green = 1;

int red = 0;

int button = 4; // the pin the button is on.

int choice = 0; // mode selection


void setup() {

  // initialize the pwm pins as outputs.

  pinMode(red, OUTPUT);

  pinMode(green, OUTPUT);

  pinMode(blue, OUTPUT);

  pinMode(button, INPUT);

  analogWrite(red, 255);

  analogWrite(green, 255);

  analogWrite(blue, 255);


}

void loop() {

// read the pushbutton to see if it's been pushed
int val;
val = digitalRead(button);

// SELECT CHOICE
 
  if (val == HIGH) {
    choice = choice + 1;

    analogWrite(blue, 255);
    analogWrite(green, 255);
    analogWrite(red, 255);
    delay(500);

    // indicate mode transition with blue flashes
    for (int i = 0; i < 5; i++) {
      analogWrite(blue, 125);
      delay(20);
      analogWrite(blue, 255);
      delay(40);
    }

    delay(500);

    // change this if you want more modes

    if (choice == 5) {
      choice = 0;
    }
  }

// for debounce

  while (val == HIGH) {
    delay(100);
    val = digitalRead(button);
  }

  //////////////////////////////


  // OFF

  if (choice == 0) {
    analogWrite(red, 0);
    analogWrite(green, 0);
    analogWrite(blue, 0);

  }

  // RED

  if (choice == 1) {
    analogWrite(red, 255);
    analogWrite(green, 0);
    analogWrite(blue, 0);

  }

  // GREEN
  if (choice == 2) {
    analogWrite(red, 0);
    analogWrite(green, 255);
    analogWrite(blue, 0);
  }

  // BLUE

  if (choice == 3) {
    analogWrite(red, 0);
    analogWrite(green, 0);
    analogWrite(blue, 255);
  }

  // AMBER
  if (choice == 4) {
    analogWrite(red, 255);
    analogWrite(green, 255);
    analogWrite(blue, 0);
  }

}

Grumpy_Mike

#18
Jun 22, 2018, 05:31 pm Last Edit: Jun 22, 2018, 05:32 pm by Grumpy_Mike
Quote
What am I doing wrong?
Probably you are looking at the current state of the push buttons. From your description it sounds like you want to trigger off the last change in state of the push buttons and not the current state. Look at the state change example in the examples section of the IDE.

Basically you need to set a variable based on the last pushed button and then do the lights based on this variable.

Edit - you posted the code as I was typing this and yes that is what you have done.

By the way read the how to use this forum sticky post to find out how to post code correctly.

Go Up