Something is not working

So as seen in the picture, i just want the led to be off when the switch is of. And blink when it is on. I feel like I have tried everything. Pin 2 is working as an output, but I cannot seem to understand the input function.
(Could only include one picture as a new user)

EDITED by J-M-L:

with properly formatted (bad) code and not the crappy image for future reference on how to post.

int test  = 13;
int test2 = 7;
int readpin = 2;

void setup() {
  // put your setup code here, to run once:
  pinMode (test, OUTPUT) ;
  pinMode (test2, OUTPUT) ;
  pinMode (readpin, INPUT) ;
}

void loop() {
  if (digitalRead(readpin == HIGH)) {
    digitalWrite(test, HIGH);
    digitalWrite (test2, HIGH);
    delay(100);
    digitalWrite(test, LOW);
    digitalWrite(test2, LOW);
    delay(100);
  }

  if (digitalRead (readpin == LOW)) {
    digitalWrite(test2, LOW);
    digitalWrite(test, LOW) ;
  }
}

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

Try to connect the switch to ground and use the internal pull up resistor.

pinMode(readpin, INPUT_PULLUP);

Then the input goes low whenever the switch is closed.

if (not digitalRead(readpin)) {
  // Do something.
}

If you use an external pull down resistor, you can leave out the not in the previous snippet.

Oh, and please read this section of the forum guidelines regarding posting code.

Please, clearly mention:
1. At which DPin, the Switch is connected?

2. What logic voltage (0V for LOW or 5V for HIGH) do you want to provide to MCU/UNO when the Switchh is pressed?

2. At which DPin, the LED is connected?

digitalRead(readpin == HIGH)

is WRONG.

if(digitalRead(readpin) == HIGH)

is how it should be.

and use an else instead of checking the pin again

if(digitalRead(readpin) == HIGH) {
  ...
} else { // if it's not HIGH then it's LOW
  ...
}

you might have to deal with bouncing and hopefully you have an external pull down resistor for your switch? (you could use the builtin pull-up otherwise)


side note: It's beyond me why people would choose to post an image of some text instead of the text itself... :thinking: :roll_eyes: :tired_face: :poop:

Missed that, thanks

Your topic has been moved to a more suitable location on the forum as this has nothing to do with Avrdude, stk500 or Bootloader.

In future, please follow @LarryD's advice when posting code.

Pictures of code and error messages are useless.

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