Trouble programming an ATtiny to control LEDs

Greetings All,

I am trying to program an ATTiny85 with code to change between 3 different LEDs with the push of a button. I think I have the circuit design correct because the LEDs flash when I apply power. However when I press the button, nothing is happening. This is my first time coding, so I'm sure I have something wrong in there. Would someone take a look and see if you can spot the breakdown?

Kano.txt (1.77 KB)

How the OP should have pasted the code:

// 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);
  }

}

musclenerdzllc:
I think I have the circuit design correct because the LEDs flash when I apply power.

But how do we know? Because this says nothing about, for example, the button :wink: And I do suspect an error there :wink:

So sorry! Here's an image of the schematic.

Schematic.JPG

That's not how you wire a switch. How do you expect that PB4 will ever go HIGH with no connections to anything except ground.

Steve

So it should go to Vcc instead of ground? Like this?

Schematic2.JPG

No. Now it can never be anything but high. Have a look at https://www.arduino.cc/en/Tutorial/Button and see if that gives you any ideas.

Steve

Thanks! I can eliminate the the pull-up resistor by enabling the internal pull-up, correct?

Okay, I think I got it now.

musclenerdzllc:
Thanks! I can eliminate the the pull-up resistor by enabling the internal pull-up, correct?

Indeed. Just connect the switch between GND and the pin and enable internal pull ups ( pinMode(pin, INPUT_PULLUP) )

Also, not sure if it just isn't in the schematic, but you should pull the reset switch up to Vcc, preferably with a resistor in the 10k range.

Shuzz:
Also, not sure if it just isn't in the schematic, but you should pull the reset switch up to Vcc, preferably with a resistor in the 10k range.

That is what pinMode(pin, INPUT_PULLUP ) does only it uses the built in internal pull up resistors.

Read all about it here:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Should have a bypass cap (0.1uF ceramic) between VCC & GND, close to the pins on the acting.

Grumpy_Mike:
That is what pinMode(pin, INPUT_PULLUP ) does only it uses the built in internal pull up resistors.

Read all about it here:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

I was talking about the Reset (NRES, Pin1).
I do think that it is still recommended to pull that up to Vcc using an external resistor (or a direct wire to Vcc if you don't want to use ISP). Am I mistaken?

Shuzz:
I was talking about the Reset (NRES, Pin1).
I do think that it is still recommended to pull that up to Vcc using an external resistor (or a direct wire to Vcc if you don't want to use ISP).

Yes, you are correct.

I think Mike quoted post #10 in error when he meant to quote post #6.

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?

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

Steve

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);
}

}

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.