Hi,
I am using the Debounce example from the Arduino IDE. I have run the code on the ATiny85 and it works great. When I run the code without commenting out the section of code within setup(), it stops toggling the LED. The commented out section within setup() is to run pwm in fast mode.
I've tried changing the input pins for the push button but still no change
/*
ATtiny85
-------u-------
X - A0 - (D 5) --| 1 PB5 VCC 8 |-- +5V
| |
X A3 - (D 3) --| 2 PB3 PB2 7 |-- (D 2) - A1 --> X
| |
PB SW I/P A2 - (D 4) --| 3 PB4 PB1 6 |-- (D 1) - PWM --> To MOSFET
| |
Gnd ---| 4 GND PB0 5 |-- (D 0) - PWM --> LED O/P
-----------------
*/
// normal delay() won't work anymore because we are changing Timer1 behavior
// Adds delay_ms and delay_us functions
#include <util/delay.h>
// Clock at 8mHz
#define F_CPU 8000000 //F_CPU 8000000 (8Mhz). This is used by delay.h library
const int PWMPin = 1; // Only works with Pin 1(PB1)
const int push_button = 4; // Push button input to trigger HIT & HOLD.
const int ledPin = 0; // The number of the LED pin
unsigned long hitDelay = 250;//250mS hit delay.
//variables will change:
int ledState = HIGH; //the current state of the output pin
int buttonState; //the current reading from the input pin
int lastButtonState = LOW; //the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup()
{
pinMode(push_button, INPUT);
pinMode(ledPin, OUTPUT);
//set initial LED state
digitalWrite(ledPin, ledState);
pinMode(PWMPin, OUTPUT);
/*
//Registers used to set FAST PWM in non inverting mode.
TCCR0A = 1 << COM0B1 | 0 << COM0B0 | 1 << WGM01 | 1 << WGM00;
TCCR0B = 1 << WGM02;
//PWM frequency = clock speed / (prescaler X OCR0A)
//8 000 000 / 64 = 125 kHz
//64 - pre-scaler
//125 kHz / 127 = ~1000 Hz
//127 - OCR0A
// disable interrupts to allow register changes
TIMSK = 0;
// set TOP to generate ~1 kHz
OCR0A = 127;// 63=2Khz, 90=1.4Khz 127=1Khz,181=700hz, 255=500hz Timer counts up to these values then drops to zero again (bottom).
// 50 % duty cycle to start with if required ?? ?
OCR0B = 31;//31
// start timer at 125 kHz (8 MHz / 64)
TCCR0B = 1 << WGM02 | 0 << CS02 | 1 << CS01 | 1 << CS00; //CS02,CS01,& CS00 Select the clock. (clk/64)
*/
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(push_button);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}