I require assistance in Coding on the online Tinkercad for a school assignemnt

I require assistance in some coding, my objectives are: make that the button switches the LED off instead of ON when the button is hold. Here is the code, please help me:

int led=13;
int pulsador=10;
int valor=0;
void setup() {
 pinMode(led,OUTPUT);
 pinMode(pulsador,INPUT); 
  
}

void loop() {
  valor=digitalRead(pulsador);  
  digitalWrite(led,valor);
  }

How is your button wired?
What does your code do now?
Have a look at the not operator (!).

Read about pullup pulldown...
You can change your code or your button connection...
But something must be inverted...

Hmm. I've tried switching the digitalread and diigtalwrite but it doesn't work?

Do i need to switch anything? I've tried also switching the inputs for led and pulsador it says it has mistakes?

It's very simple you can also go through Tinkercad inbuilt examples

Also you can Google it you will easily find the answer for that

1 Like

I second that.

Where can I find the not operator?

Why? You stabbing in the dark.

digitalRead() gets the value on the pin. That will be HIGH or LOW.

digitalWrite() copies that value to the LED.

You can alter that value between the two calls. Do it better than this, but watch:

void loop() {
  valor = digitalRead(pulsador);

  if (valor == HIGH) {
    digitalWrite(led, LOW);
  } 
  else if (valor == LOW) {
    digitalWrite(led, HIGH);
  }
}

As I said, there are niftier ways to do this. Find two before breakfast!

a7

1 Like

I'll test this in the code. Ithink it will work.

Thank you, it work!

Come on, you have a school exercise. It is not a guessing competition...

What?

Remember, I said that is a kinda verbose and dumb way to do that!

The not operator, or '!' as suggested by @build_1971 is worth looking into.

and, or and not are used in logical expressions. Consult your favorite C/C+= reference materials.

Also seen as &&, ||, and !.

HTH

a7

I will tell you the how:

Google.

Or try chatGTP...

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