I'm using Attiny45 with Arduino bootloader and everything is running ok. I tested this little sketch - on attiny it runs ok, the LED goes on as it should when I press button more than 1 sec. But on Arduino Uno the LED blinks from the start all the time and I cannot find the reason why is it so.
I hope you can help me. I'm using arduino IDE 0022 and uploading sketches to the Attiny with Uno as ISP. All other sketches run on Uno ok.
Here's my code:
const int ButtonPin = 0;
const int LedPin = 1;
void setup() {
pinMode(ButtonPin, INPUT);
pinMode(LedPin, OUTPUT);
}
enum {WAIT, SETUP, READY, SHORT, LONG};
int State = WAIT;
void loop(){
if (getButton(ButtonPin) == LONG)
{
digitalWrite(LedPin,HIGH);
delay(1000);
digitalWrite(LedPin,LOW);
State = WAIT;
}
delay(200); // pseudo debounce
}
int getButton(int pin) {
int state = WAIT;
unsigned long timer = millis();
while (digitalRead(pin) == HIGH)
{
if (millis() - timer < 1000)
{
state = SHORT;
}
if (millis() - timer > 1000)
{
state = LONG;
break;
}
}
return state;
}
rednaskellar:
I tried this allready but it did not help.
Your code doesn't reflect that. Nor did you post anything that shows how you've hooked up the momentary switch (what you call a button). How is your external pullup resistor hooked up?
rednaskellar:
As I said I tried different pins but it didn't help.
So you went back to a pin that is likely to give you issues even if you find another issue? When you say the LED blinks, is the blinking consistent, erratic? Is it turning on for 1 second and off for 200 mS as would be the case if ButtonPin was permanently HIGH?