Digital Input on ATtiny not working

Hi All,

I have programmed quite a bit but am running into an issue I have never had before with an ATtiny chip.
I am using arduino to write to the chip using a Sparkfun AVR, I was using the sparkfun library and was having the issue switch to adafruits library, and am now on the SpenceKonde/ATTinyCore library.

What I am running into is that the digital read does not appear to be working. I have simplified the code to just turn the LEDs off if the button is pressed. I also used analogWrite on one and digital on the other to see if I had something weird there.

This is the code:

const int redLED = 0;
const int greenLED = 1;
const int button = 2;
const int relayRelease = 4;

int count = 0;
int greenInt = 168;
int redInt = 107;

void setup()
{
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(button, INPUT);
pinMode(relayRelease, OUTPUT);
}

void loop()
{
analogWrite(redLED, redInt);
analogWrite(greenLED, greenInt);
delay(1000);

while (digitalRead(button == HIGH))
{
analogWrite(greenLED, 0);
digitalWrite(redLED, LOW);
delay(1000);
}
}

When I press the button nothing happens, I can measure 5v at the pin ( measured after the button) so I know it is high. As a second test I changed while (digitalRead(button == HIGH)) to while (digitalRead(button == LOW)) still nothing happens.

I at one point added a flash sequence after the while loop to make sure the code was reading through. This worked but the button would still not affect the LEDs.

Is there something I am missing? To my knowledge all the pins on an ATtiny can function as digital I/O.

Thanks

My guess is that you've got the pin numbering wrong? Which ATTiny chip are you using? My core supports over a dozen different parts. There are images showing the pin mapping for all of the supported chips in the core documentation - doublecheck the pin numbers you're using against that. The Tiny84/44/24 and tiny87/167 has two options for the pin mapping - make sure you are using the one you think you are if you're using one of those.

Also, be aware that during the 1 second delay(), the button is not being read, so you might have to hold the button down for >1 second to see a response (this is a standard use case for "blink without delay" type techniques).

(deleted)

spycatcher2k:
Oops!

while (digitalRead(button) == HIGH)

/facepalm - how did I miss that!

  while (digitalRead(button == HIGH))
  {
    analogWrite(greenLED, 0);
    digitalWrite(redLED, LOW);
    delay(1000);
  }

Read the (wrong) pin once per second, and expect instantaneous results. Why?

Are you paying for each iteration of the while loop?

Why do you, inside the body of that statement, need to set the pin states over and over?