0

enter image description here

Hello guys. I'm working on a little lightshow that has 3 extra feature's when i press the button. depending on the variable counter the lights should do something and it should message you that you found a feature. But my project does'nt work. it compiles fine on the tinkercad site but not everything works. The lights itself work but the button feature's dont. I don't know if my code is wrong or the wiring since its able to compile.

I expected that if i press the countbutton it counts up and when it reaches 6 it does the code in the if statement. However when i press the count up and or count down button according to the variable it needs to reach it doesn't do anything.

the code:

int time = 250;
int ledpins[] = {3, 6, 7, 4, 2, 5};
int amount = 6;
int countup = 11;
int countdown = 12;
int counter;
int timepressed = 4000;
float timerpressed = 0;


void setup()
{
  for (int pin = 0; pin < aantal; pin++)
    {pinMode(ledpins[pin], OUTPUT);}
   //alle leds worden door middel van de loop gedefinieerd als output
   //pin is eerst 0 dat is element 3 in de array. de counter heeft einde
   //nog niet en gaat verder tot de conditie bereikt is
   pinMode(countdown, INPUT_PULLUP);
  pinMode(countup, INPUT);
   Serial.begin(9600);
}

void loop()
{
  bool plus = digitalRead(countup);
  bool minus = digitalRead(countdown);
  delay(20);

  if (plus == LOW){
    counter++;
   if (counter > 10) counter = 0;
  }

  if (minus == LOW){
    counter--;
  }

//Easter egg nummer 1. 2 lights on.
if (counter == -2){
    digitalWrite(ledpins[1], HIGH);
    digitalWrite(ledpins[4], HIGH);
    Serial.print("Gefeliciteerd je hebt (1/3) easter egg gevonden!");
}
//Easter egg nummer 2. all lights off.
else if (counter == 6){
  digitalWrite(ledpins[0], LOW);
  digitalWrite(ledpins[1], LOW);
  digitalWrite(ledpins[2], LOW);
  digitalWrite(ledpins[3], LOW);
  digitalWrite(ledpins[4], LOW);
  digitalWrite(ledpins[5], LOW);
  Serial.print("Gefeliciteerd je hebt (2/3) easter egg gevonden!");
}
//Easter egg nummer 3. blink alternately after keeping the button pressed for 4 seconds.
else if (timerpressed >= timepressed){
  digitalWrite(ledpins[0], HIGH);
  digitalWrite(ledpins[1], HIGH);
  digitalWrite(ledpins[4], HIGH);
  digitalWrite(ledpins[5], HIGH);
  Serial.print("Gefeliciteerd je hebt (3/3) easter egg gevonden!");
 }

Okay an update on this post is a was able to make a couple of fixes. My code was keep getting stuck in the array loop for the leds. I've made it that it now only goes trough it once. Another thing i fixed was the wiring of the buttons since this wasnt correct. The only feature that isnt working right now is the keep the button pressed feature. enter image description here

int tijd = 250;
int ledpins[] = {3, 6, 7, 4, 2, 5};
int aantal = 6;
int knopplus = 11;
int knopmin = 12;
int teller;
int indruktijd = 4000;
float timerdruktijd = 0;
int pin = 0;
int pin1 = 0;

void setup()
{
  for (int pin = 0; pin < aantal; pin++)
    {pinMode(ledpins[pin], OUTPUT);}
   //alle leds worden door middel van de loop gedefinieerd als output
   //pin is eerst 0 dat is element 3 in de array. de counter heeft einde
   //nog niet en gaat verder tot de conditie bereikt is
   pinMode(knopmin, INPUT_PULLUP);
  pinMode(knopplus, INPUT);
   Serial.begin(9600);
}

void loop()
{
  bool plus = digitalRead(knopplus);
  bool minus = digitalRead(knopmin);
  delay(20);

  if (plus == LOW){
    teller++;
   if (teller > 10) teller = 0;
  }

  if (minus == LOW){
    teller--;
  }

//Easter egg nummer 1. twee lichten aan.
if (teller == -2){
    digitalWrite(ledpins[1], HIGH);
    digitalWrite(ledpins[4], HIGH);
    Serial.print("Gefeliciteerd je hebt (1/3) easter egg gevonden!");
}
//Easter egg nummer 2. alle lichten uit.
else if (teller == 6){
  digitalWrite(ledpins[0], LOW);
  digitalWrite(ledpins[1], LOW);
  digitalWrite(ledpins[2], LOW);
  digitalWrite(ledpins[3], LOW);
  digitalWrite(ledpins[4], LOW);
  digitalWrite(ledpins[5], LOW);
  Serial.print("Gefeliciteerd je hebt (2/3) easter egg gevonden!");
}
//Easter egg nummer 3. knipper om en om na 4 seconden lang de -knop ingedrukt te houden
else if (timerdruktijd >= indruktijd){
  digitalWrite(ledpins[0], HIGH);
  digitalWrite(ledpins[1], HIGH);
  digitalWrite(ledpins[4], HIGH);
  digitalWrite(ledpins[5], HIGH);
  Serial.print("Gefeliciteerd je hebt (3/3) easter egg gevonden!");
 }

  for (pin; pin < 6; pin++)
  {digitalWrite(ledpins[pin], HIGH);
   delay(tijd);
   digitalWrite(ledpins[pin], LOW);}
  //loop voor volgorde heen. 

  for (pin1; pin1 < 6; pin1++)
  {digitalWrite(ledpins[pin], HIGH);
   delay(tijd);
   digitalWrite(ledpins[pin], LOW);}
  //loop zelfde volgorde terug
}
  • 1
    "It doesn't work" is a poor problem description. We have to search your code for the possible problem and that get's way easier, when you give us a good description of the problem. So please answer the questions: What is actually happening depending on your actions? What did you expect to happen instead? – chrisl Nov 14 at 9:13
  • I agree with chrisl; however one problem I see is that you need to debounce the buttons, see arduino.cc/en/Tutorial/Debounce … also try to write your comments and variable names in English, not Dutch (I understand since I'm Dutch too, but most people will not). – Michel Keijzers Nov 14 at 9:15
  • thank you guys for the comments. I changed it according to you guys feedback. – Dashcarwash Nov 14 at 9:25
  • Can you also give us a sample of the Serial output (if there is any)? – chrisl Nov 14 at 9:42
  • Why do you have one button with internal pullup but the other not? – chrisl Nov 14 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.