Hi, everyone.
I'm trying to make a 3 binary counter that counts from 1-7. Every time it goes to 8 it starts again. Using 3 leds for the counting and 1 led for the heartbeat. For some reason i cant manage to make the button part work at all. Should count every time the button is pushed and light the leds accordingly.
const int ledhb = 10;
const int led1 = 11;
const int led2 = 12;
const int led3 = 13;
const int btnPin = 2;
boolean hbLedState = LOW;
long hbInterval = 0;
long startTime = 0;
long hbCount = 0;
int btnState;
int prevBtnState = LOW;
int led1State = LOW;
int led2State = LOW;
int led3State = LOW;
void setup() {
// Laitetaan pinMode ledille ja nappulaalle
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (ledhb, OUTPUT);
pinMode (btnPin, INPUT);
digitalWrite (ledhb, hbLedState); //HB
startTime = millis();
digitalWrite (led1,led1State);
digitalWrite (led2,led2State);
digitalWrite (led3,led3State);
Serial.begin(9600); //nopeus millä tietoa lähetetään
}
//void setup loppuu tähän
void loop() {
hbCount++;
if (hbInterval == 0 and millis()-startTime >= 100){
hbInterval = hbCount*5;
}// if (hbInterval == 0 and millis()-startTime >=100)
if (hbInterval > 0 and hbCount > hbInterval){
hbLedState = !hbLedState;
hbCount = 0;
digitalWrite (ledhb, hbLedState);
}// if (hbCount > hbInterval)
//nollataan ledit
// Luetaan nappulan tila
btnState = digitalRead(btnPin);
if (btnState and !prevBtnState){
led1State = !led1State;
led2State = !led2State;
led3State = !led3State;
{
int count = 0; count < 8; ++count; {
if(led3, ((count >> 2) % 2) == 1)
{digitalWrite(led3,HIGH);}
else
{ digitalWrite(led3, LOW);}
if (led2, ((count >> 1) % 2) == 1)
{digitalWrite(led2,HIGH);}
else
{ digitalWrite(led2, LOW);}
if (led1, ((count >> 0) % 2) == 1)
{ digitalWrite(led1,HIGH); }
else
{ digitalWrite(led1,LOW);}
delay(2000);
}}
prevBtnState = btnState;
}}