Hi!!
I hope some of you can help me.
I have been programming with arduino for about a month now and have built a sketch that meets most of my requirements.
I used the arduino IDE to program the sketch on to my Attiny45.
However the sketch uses delay(), i want replace delay()s with millis()s.
Here is the sketch i built using delay()
const int switchPin =2; // "arduino Switch" connected to digital pin 2
const int switchPin1 =3; // "faceplate Switch" connected to digital pin 3
const int ledPin = 0; // Led pins
const int ledPin2 = 1;
const int ledPin3 = 4;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed/debounced status
int buttonState = 0; // variable for reading if button state is changed
int lastButtonState = 0; // previous state of the button
int buttonPushCounter = 0; // counter for the number of button presses
int i = 0;
void setup() // run once, when the sketch starts
{
pinMode(switchPin, INPUT);
pinMode(switchPin1, INPUT);
buttonState = digitalRead(switchPin);
buttonState = digitalRead(switchPin1);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
GTCCR |= 0b01010000; //setup pwm on pin 4
GTCCR &= 0b11011111;
}
void loop(){ // run over and over again
val = (digitalRead(switchPin) || digitalRead(switchPin1)); // read input from switchPin1&2 and store it in val
delay(20); // 20 milliseconds is a good amount of time
val2 = (digitalRead(switchPin) || digitalRead(switchPin1)); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings! Form of button debouncing
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
buttonPushCounter++; // adds +1 to int buttonPushCounter
if (buttonPushCounter == 1) {
blinkon(); //call void blinkon
}
else if(buttonPushCounter == 2){
turntobattlemode(); //call void turntobattlemode
}
else {
pushbuttonoff(); //call void pushbuttonoff
}
}
}
buttonState = val; // save the new state in our variable
}
}
//------------------------------------------------------------------------------------------------------------------
void blinkon(){
// blinking routine
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(00);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(00);
// fading in
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { // sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
OCR1B = fadeValue; // same as analogWrite..
delay(30);
}
}
//------------------------------------------------------------------------------------------------------------------
void turntobattlemode(){
digitalWrite(ledPin, HIGH);
// fading blue out
for (int i = 255; i > 0; i--){ //descrease i with 1
OCR1B = i; // same as analogWrite..
delay(2);
}
OCR1B = LOW;
// fading red in
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { // sets the value (range from 0 to 255):
analogWrite(ledPin2, fadeValue);
delay(20);
}
}
//------------------------------------------------------------------------------------------------------------------
void pushbuttonoff(){
for (int i = 255; i > 0; i--){ //descrease i with 1
analogWrite(ledPin, i);
analogWrite(ledPin2, i);
delay(5);
}
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
buttonPushCounter = 0;
}
//------------------------------------------------------------------------------------------------------------------
Every time i push a button i have to wait for the piece of code to end before i can press it again and toggle the next piece of code.
What i want to do is push the button, toggle the piece of code but not have to wait until the piece of code is finishes before i can press te button again to toggle the next piece of code.
As i understand it this can be done by using the blink/fade without delay principles. So the sketch is not interrupted and can look for another buttonpress.
Now, I think i understand the principles of blinking or fading without delay and how to run a loop a set number of times.
I have googled and tried more sketches than i can remember however i cant seem to combine the two to make a led blink twice and then fade in.
I hope you guys understand, and can help me.
Hope im posting this in the appropriate section and sorry if my English is incorrect, im from the Netherlands.
Greets