admin管理员组

文章数量:1131174

my circuit

// C++ code
//
void setup()
{
  pinMode(8, INPUT);
  pinMode(2, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop()
{
  if (digitalRead(8) == HIGH) {
    digitalWrite(2, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    //delay(1000); // Wait for 1000 millisecond(s)
    //digitalWrite(6, LOW);
    //digitalWrite(2, LOW);
    //delay(1000); // Wait for 1000 millisecond(s)
  }
}

I created the code above for a school project and i set pin 8 as input to check for voltage but they just turns on as soon as i run the simulation and i don't know why. i also linked an image of my circuit because there may be some thing wrong with that.

i haven't tried much because this is my first time using c++ but i want pins 2, 3, and 6 to turn on if pin 8 is reading as HIGH but they just turn on as soon as i run the simulation

my circuit

// C++ code
//
void setup()
{
  pinMode(8, INPUT);
  pinMode(2, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop()
{
  if (digitalRead(8) == HIGH) {
    digitalWrite(2, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    //delay(1000); // Wait for 1000 millisecond(s)
    //digitalWrite(6, LOW);
    //digitalWrite(2, LOW);
    //delay(1000); // Wait for 1000 millisecond(s)
  }
}

I created the code above for a school project and i set pin 8 as input to check for voltage but they just turns on as soon as i run the simulation and i don't know why. i also linked an image of my circuit because there may be some thing wrong with that.

i haven't tried much because this is my first time using c++ but i want pins 2, 3, and 6 to turn on if pin 8 is reading as HIGH but they just turn on as soon as i run the simulation

Share Improve this question edited Jan 8 at 4:18 inayaat pardhan kassam asked Jan 8 at 4:16 inayaat pardhan kassaminayaat pardhan kassam 112 bronze badges New contributor inayaat pardhan kassam is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 8
  • 3 I'm not an electrical engineer, but to my untrained eye, pin 8 looks hard-wired to a battery. So why would there not be voltage on it right away? – Igor Tandetnik Commented Jan 8 at 4:21
  • 1 Is that a switch between the cell and pin 8? Or is it connected directly? What does your multimeter say about the state of pin 8 on power up and afterward (there may be electrical noise)? It's probably a good idea to add an else so the output pins go low when 8 goes low. Then you may need debounce :-) – paxdiablo Commented Jan 8 at 4:27
  • What do you expect your circuit to do when you run the simulation? – LakshyaK2011 Commented Jan 8 at 4:35
  • 1 Hmm, I'm not sure about the Arduino but you may wan to make sure those GND pins are actually electrically connected. Or connect the negative terminals of the 9v battery and cell. All sorts of weirdness can happen if you combine voltages from two different sources without normalising ground. I remember this happening on train systems when each car had distinct power sources and we were had shared data lines using different voltage levels for GND. Much hilarity and boomage :-) – paxdiablo Commented Jan 8 at 4:36
  • 2 Actually, the common ground may not be an issue here, it looks like the TIP120 is isolating the two sections (9v and 3.3V). Still, worth looking into just in case, I don't have enough detail on that transistor to comment. May also be worth asking over at electronics.stackexchange.com on the electrical aspects of the question. – paxdiablo Commented Jan 8 at 4:43
 |  Show 3 more comments

1 Answer 1

Reset to default -1

The actual problem is with your code.
if you see, in your if statement inside the loop, it is:

if (digitalRead(8) == HIGH) {
    digitalWrite(2, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    //delay(1000); // Wait for 1000 millisecond(s)
    //digitalWrite(6, LOW);
    //digitalWrite(2, LOW);
    //delay(1000); // Wait for 1000 millisecond(s)
  }

The line of code that turns off the pin 6 and 2 are commented out, (// added before the code line) as it makes it so that that statement would be ignored,
and also, these lines:

digitalWrite(2, HIGH);
digitalWrite(6, HIGH);
digitalWrite(3, HIGH);

I guess are meant to be:

digitalWrite(2, LOW);
digitalWrite(6, LOW);
digitalWrite(3, LOW);

as you want to turn off these pins, setting it to low will turn them off.

本文标签: my if statement is not working in C idk what to doStack Overflow