Button-led lights code in project

I am making a project with countdown and piezo, that sings a song. But there is a part with button and led light, which doesnt work. The button is supposed to turn on led light when pressed, but it doesnt work for an unknown reason. I marked the parts with button.

#define segA 9
#define segB 7
#define segC 6
#define segD 4
#define segE 5
#define segF 8
#define segG 11
#define CA1 2
#define CA2 3
#include "pitches.h"
int tempo = 140;
int melodie[] = {
  NOTE_G4, 4, NOTE_G4, 8, NOTE_A4, -4, NOTE_G4, -4, NOTE_C5, -4, NOTE_B4, -2, 
  NOTE_G4, 4, NOTE_G4, 8, NOTE_A4, -4, NOTE_G4, -4, NOTE_D5, -4, NOTE_C5, -2,
  NOTE_G4, 4, NOTE_G4, 8, NOTE_G5, -4, NOTE_E5, -4, NOTE_C5, -4, NOTE_B4, -4, NOTE_A4, -4, NOTE_F5, 4, NOTE_E5, 8, NOTE_D5, -4, NOTE_C5,-2,  
};

int Noty = sizeof(melodie) / sizeof(melodie[0]) / 2;
int CelaNota = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;

const int segs[7] = {segA, segB, segC, segD, segE, segF, segG};
const byte digits[10] = {0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010, 0b0000010,
0b1111000, 0b0000000, 0b0010000};
const byte letters[4] = {0b0001001,0b0001000, 0b0001100, 0b0011001};

const int buttonPin = 14;
const int ledPin =  13;   
int buttonState = 0;  

void setup() {
  pinMode(ledPin, OUTPUT);      //button problem
  pinMode(buttonPin, INPUT);    //button problem
  
  for(int a = 0; a<=6; a++){
    pinMode(segs[a], OUTPUT);
  }
  pinMode(CA1, OUTPUT);
  pinMode(CA2, OUTPUT);
  
  digitalWrite(12, HIGH);
}

void loop() {
    buttonState = digitalRead(buttonPin);  //button problem
  if (buttonState == HIGH) {  
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    digitalWrite(ledPin, LOW);
  }  //button problem

  zobrazLeve(digits[1]);
  delay(1000);
  for(int digitL = 0; digitL >= 0; digitL--){
    for(int digitR = 9; digitR >= 0; digitR--){
      unsigned long startTime = millis();      
      for(unsigned long trvani=0; trvani < 1000; trvani = millis() - startTime){
        zobrazLeve(digits[digitL]);
        delay(5);
        zobrazPrave(digits[digitR]);
        delay(5);
      }
    }
  }

    for (int Nota = 0; Nota < Noty * 2; Nota = Nota + 2) {
    divider = melodie[Nota + 1];
    if (divider > 0) {
      noteDuration = (CelaNota) / divider;
    } else if (divider < 0) {
      noteDuration = (CelaNota) / abs(divider);
      noteDuration *= 1.5; 
    }
    tone(12, melodie[Nota], noteDuration * 0.9);
    delay(noteDuration);
    noTone(12);
  }

    delay(1000);
    zobrazLeve(letters[0]);
    delay(1000);
    zobrazPrave(letters[1]);
    delay(1000);
    zobrazLeve(letters[2]);
    delay(1000);
    zobrazPrave(letters[2]);
    delay(1000);
    zobrazPrave(letters[3]);
    delay(1000);
    zobrazLeve(digits[1]);
    delay(1000);
    zobrazPrave(digits[8]);
    delay(1000);
}

void zobrazLeve(byte cislice){
  digitalWrite(CA1, LOW);
  digitalWrite(CA2, HIGH);
  zapisCislici(cislice);
}

void zobrazPrave(byte cislice){
  digitalWrite(CA1, HIGH);
  digitalWrite(CA2, LOW);
  zapisCislici(cislice);
}
                     
void zapisCislici(byte cislice){
  for(int i = 0; i < 7; i++){
    int bit = bitRead(cislice, i);
    digitalWrite(segs[i], bit);
  }
}

@cleo321

Your topic was Moved to it's current location / section as it is more suitable and NOT covid related.

Please take a few moments to Learn and Use The Forum

It will help you get the very best out of the forum in the future.

  • Your OS and version can be valuable information, please include it along with extra security you are using. Antivirus etc.

  • Always list the version of the IDE you are using and the board version if applicable.

  • Use quote or add error messages as an attachment NOT a picture.

  • How to insert an image into your post. ( Thanks @sterretje )

  • Add your sketch where applicable but please use CODE TAGS https://drive.google.com/file/d/1vF0OE4HozgyW_3oFENjem8DCrbrNoZ88/view?usp=share_link

  • Add a SCHEMATIC were needed even if it is hand drawn

  • Add working links to any specific hardware as needed (NOT links to similar items)

  • Remember that the people trying to help cannot see your problem so give as much information as you can

COMMON ISSUES

  • Ensure you have FULLY inserted the USB cables.

  • Check you have a COMMON GROUND where required. ( Thanks @Perry)

  • Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.

  • Try other computers where possible.

  • Try other USB leads where possible.

  • You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI

  • There may be a problem with the board check or remove your wiring first.

  • Remove any items connected to pins 0 and 1.

COMPUTER RELATED

  • Close any other serial programs before opening the IDE.

  • Ensure you turn off any additional security / antivirus just to test.

  • There may be a problem with the PC try RESTARTING it.

  • You may be selecting the wrong COM port.

  • Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.

  • Clear your browsers CACHE.

  • Close the IDE before using any other serial programs.

  • Preferably install IDE’s as ADMINISTRATOR or your OS equivalent

ARDUINO SPECIFIC BOARDS

  • CH340/341 based clones do not report useful information to the “get board info” button.

  • NANO (Old Types) some require you to use the OLD BOOTLOADER option.

  • NANO (ALL Types) See the specific sections lower in the forum.

  • NANO (NEW Types) Install your board CORE’s.

  • Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default “AVRISP mkII”.

  • Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.

CREATE editor install locations.

  • On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini

  • On Linux ~/ArduinoCreateAgent-1.1/config.ini

  • On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1

Performing the above actions may help resolve your problem without further help.

Language problem ?

Try a language closer to your native language:

Thanks to all those who helped and added to this list.

What Arduino board are you using?

How is the button switch wired? Is there a pulldown resistor on the switch input?

Where is pin 14 on your board? It is the same as A0 on Uno, Nano.

It takes a very very long time for your loop() to execute because of all of the delays. The switch will be read each iteration of loop(), but it takes about a half of a minute to execute loop(). Hold the switch down and wait and you should see it change after a while ( a bit over 30 seconds).

As long as you are using the, blocking, delay() function the program will never be responsive. These tutorials show how to write non blocking code.
Several things at a time.
Beginner's guide to millis().
Blink without delay().

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.