Arduino pins - Signal is incorrectly recognized

I tried to build a signal listener. For this I wrote this code first:

  Serial.begin(9600);
  pinMode(10, OUTPUT);
  pinMode(12, INPUT);
}

void loop() {
  Serial.println(digitalRead(12));
  if(digitalRead(12) == HIGH) {
    digitalWrite(10, HIGH);
  }
}

Now the LED always started after a short time. In the serial monitor there was always 1 and 0. However, often 1. Why is that? Is my Funduino Uno broken? Or are pins linked somehow?
I connected a 100 ohm resistor to PIN 10 and then an LED. Then to the ground.

What can I do so that if I have not yet connected a switch etc. the LED does not just go on? (After that I tried it with a switch and resistor. That didn't work either.)

You have to show us your circuit connections.

Also, where is the code to turn the LED off? :wink:

You seem to have pin 12 not connected to anything or your anything ground is not connected to the Arduino ground.
Paul

Was something edited away? I see no diagram.

Ahh, you have your crystal ball back...
Mine is still getting its Christmas service.

I was expecting a new one for Christmas, but I just got socks....

Tom.... :grinning: :+1: :australia:

Where can I draw a circuit diagram? I've now attached a photo. At first I had 5V and a switch on PIN 12. It didn't work out that way (same problem as described above, only with one switch). So I tested it without anything.

Imgur

Pencil or pen and paper come to mind.
Paul

Oh that's right. Somehow I just couldn't figure it out xD. Afterwards I will draw a circuit diagram. I'm just not at home anymore.

So, knowing that a floating input pin (without pullup enabled) could be high or low

if(digitalRead(12) == HIGH) {
digitalWrite(10, HIGH);

What's going to flip the LED state? As written, once HIGH the output pins stays in that logic level.

The LED can be wired to be On when the Output is LOW (called sinking) or the LED can be On when the Output is HIGH (called sourcing.) in either case, your software must inspect the input pin and then set the output pin: so

if (input condition) {
  do the true;
  }else{
do the false;
  }

See reply #2.

@louie_1 you need to read this
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Is there some kind of f'n game going on where points are awarded for being the first to answer something obvious? Like you are so proud of your karma points ... no one cares, especially me. I'm in the forum to assist the Op, not to collect karma... far too old for games.

My statement was at least followed by the pseudo-code to assist the Op to know what to study in the Reference section. Many self-starters know if(condition) but have not learned the if () then {}else{} construct.

Ray

No, points are awarded to OP's for reading replies and answering them.

I don't worry about karma... I thought I lost all that and my post count too with the forum upgrade but I wasn't really paying much attention to that before, either.

Yes, agreed.
But Newby coders are often overwhelmed; you answer your way, I will answer mine. I do not get into the forum as often as the more enthusiastic folk like you, but I do try. In the end analysis, we both wish to move the Op over whatever hump they are experiencing.

The hump of not even looking at the built in examples that even come with tutorials?
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button

Also, the "see reply #" trope is fairly common here, please take it at face value.

OP never came back to say, "what did you mean" or "I don't get it"...

I have been around since 2012; I have a tough skin. My long absence was primarily because of members treating newbies (and often senior members) like poopoo. I also moderated the stm32duino forum for a time and assisted with the transition of moving to the new STM owned and operated forum.

Face value == :slightly_smiling_face:

Well, I didn't treat anyone like doodoo in this thread at least. So I don't see the relevance.

OP never came back to say, "what did you mean" or "I don't get it"...

IMHO, you are "pressuring" the Op based on your own expectation. Op is likely off visiting family & friends and not fully engaged. The Forum system will notify you when the Op reappears and hopefully will have what you expected.

I have taught adult computer classes and developed courses and the classroom is a difficult place to be, but there are rewards. Online training is much more formal than a forum; I think of the forum as two people who start talking but get on separate trains a few minutes later. Can I transfer enough information and provide some guidance to make a difference. I do not like to ping-pong in a forum; sometimes I'm spot-on, sometimes not so much. Sometimes the student gets an A++ and sometimes they get an F. Ops have a responsibility to respond, but individual initiative should not be on a timetable IMO.

Ray

Oh, wait... here is OP!