Help for turn on an LED with a button

please, what is wrong with my code, it doesn't work

int buttonPin=2;
int ledPin=12;
int bout;
void setup()
{
  pinMode(buttonPin,INPUT)
  pinMode(ledPin;OUTPUT)
}
void loop()
  bout=digitalRead(inputPin);
  if (bout==HIGH);
  {
    digitalWrite(ledPin,HIGH);
}
  else
  {
    digitalWrite(ledPin,LOW);
  }

I want that while the button is pressed, the light is on and that when the button is not pressed, the light is not on, I am in 2.0.2

here's code that works

int buttonPin=2;
int ledPin=12;
int bout;

void setup ()
{
    pinMode (buttonPin, INPUT);
    pinMode (ledPin,    OUTPUT);
}


void loop ()
{
    bout=digitalRead (buttonPin);
    if (bout==HIGH)
    {
        digitalWrite (ledPin,HIGH);
    }

    else {
        digitalWrite (ledPin,LOW);
    }
}

here are the differnces

_Others diff $F $T
4c4,5
< void setup()
---
>
> void setup ()
6,7c7,8
<   pinMode(buttonPin,INPUT)
<   pinMode(ledPin;OUTPUT)
---
>     pinMode (buttonPin, INPUT);
>     pinMode (ledPin,    OUTPUT);
9,15c10,15
< void loop()
<   bout=digitalRead(inputPin);
<   if (bout==HIGH);
<   {
<     digitalWrite(ledPin,HIGH);
< }
<   else
---
>
>
> void loop ()
> {
>     bout=digitalRead (buttonPin);
>     if (bout==HIGH)
17c17
<     digitalWrite(ledPin,LOW);
---
>         digitalWrite (ledPin,HIGH);
18a19,24
>
>     else {
>         digitalWrite (ledPin,LOW);
>     }
> }
>
1 Like

it doesn't work, I get the error : redefinition of 'void setup()'. It may be because of my version, 2.0.2, or it's because I have an arduino nano.

The original code doesn't compile. The way one works through that is to check the first error in compilation, fix what's causing that (often a mistake on the line before the reported error), then iterate (with IDE autoformatting to check for sane nesting).

The #1 code gives these errors in Wokwi:

UnoScope.ino: In function 'void setup()':
UnoScope.ino:20:3: error: expected ';' before 'pinMode'
   pinMode(ledPin; OUTPUT)
   ^~~~~~~
UnoScope.ino:20:25: error: expected ';' before ')' token
   pinMode(ledPin; OUTPUT)
                         ^
UnoScope.ino: At global scope:
UnoScope.ino:23:1: error: expected initializer before 'bout'
 bout = digitalRead(inputPin);
 ^~~~
UnoScope.ino:24:1: error: expected unqualified-id before 'if'
 if (bout == HIGH);
 ^~
UnoScope.ino:25:1: error: expected unqualified-id before '{' token
 {
 ^
UnoScope.ino:28:1: error: expected unqualified-id before 'else'
 else
 ^~~~

Error during build: exit status 1

the first error is that this line does not have a semicolon on the end of it:

@gcjr worked through those to make it compile, (worked for me) and printed a 'diff' detailing all the changes.

there are 2 separate listings. try the listing before

Most of your code is good, but small mistakes cause programs to stop. This looks like an image-to-text was used on screen-shots of the code. There are three extra semi-colons (;), two missing semi-colons, a missing open brace, a missing close brace, and a word replacement issue. Then it works (press the button to light the LED). Keep correcting the errors, one at a time.

NO, it will work on a NANO, and you should upgrade the IDE to 2.3.2.

1 Like

Do you have a 10k pulldown resistor connected from pin 2 to GND?

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