/**
Led Blinking :
Inspirado por el ejemplo de Arduino "blinkwithoutdelay".
Utiliza directamente los registros para modificar el estado de los LEDS.
El tiempo de refresco de modifica con el potentiometro.
Es possible de cambiar entre 2 modos con el button.
*/
int mode=1;
const int potentiometerPin=0;
const int buttonPin=8;
unsigned long previousMillis = 0; // will store last time LED was updated
long interval = 200; // interval at which to blink (milliseconds)
int oldButtonState=0;
void setup() {
pinMode(buttonPin, INPUT); //init button
DDRD=DDRD|0xFC; //Port B as output : the LEDS are connected from pin 3 to 7 of PORTB
//initialize the leds
PORTD=(PORTD&0x03)|0x04;
}
void loop()
{
int reading; // to save the ADC value
int buttonState;
int buttonFlag=0;
unsigned long currentMillis;
//button handling
buttonState = digitalRead(buttonPin);
if(buttonState!=oldButtonState)
buttonFlag=1;
//button flag handling;
if(buttonFlag==1)
{
buttonFlag=0; //reset the flag
//Change the mode of blinking depending on the buttonState
if(buttonState==0)
{
mode=0;
PORTD=(PORTD&0x03)|0xA8;
}
else
{
mode=1;
PORTD=(PORTD&0x03)|0x04;
}
}
oldButtonState=buttonState;
interval=analogRead(potentiometerPin);
//interval=200;
currentMillis = millis();
//change the led state if enough time has gone.
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
switch(mode)
{
case 0 :PORTD=PORTD^0x0FC; //Invert the leds
break;
case 1 : if((PORTD&0xFD)==0x80)
{
PORTD=(PORTD&0x03)|0x04;
}
else
{
PORTD=(PORTD&0xFC)<<1;
}
break;
}
}
}