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 | Show 3 more comments1 Answer
Reset to default -1The 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
版权声明:本文标题:my if statement is not working in C++ idk what to do - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736760320a1951509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
else
so the output pins go low when 8 goes low. Then you may need debounce :-) – paxdiablo Commented Jan 8 at 4:27