so im new on arduino coding, and i made this code, where i input from serial monitor and then use it as a command for digital write. the problem is, sometimes the code work perfectly but then when i input 4, both D3 ad D4 is high and that also happens in D5 and D6. but its inconsistent, and i also print the 'out' value and also which digital pin is high to make it easier.
byte b;
int out;
int D3 = 3;int D4 = 4;int D5 = 5;int D6 = 6;int D7 = 7;int D8 = 8;
void setup() {
Serial.begin(9600);
pinMode(out, OUTPUT);
digitalWrite(D3, LOW);digitalWrite(D4, LOW);digitalWrite(D5, LOW);digitalWrite(D6, LOW);digitalWrite(D7, LOW);digitalWrite(D8, LOW);
}
void loop() {
while (Serial.available() == 0) {}
b = Serial.read() - 48;
out = b;
Serial.print("\n Out is ");
Serial.println(out);
digitalWrite(out, HIGH);
delay(600);
if (digitalRead(D3) == HIGH)
{
Serial.print("D3 HIGH");
}
if (digitalRead(D4) == HIGH)
{
Serial.print("D4 HIGH");}
if (digitalRead(D5) == HIGH)
{
Serial.print("D5 HIGH");
}
if (digitalRead(D6) == HIGH)
{
Serial.print("D6 HIGH");
}
if (digitalRead(D7) == HIGH)
{
Serial.print("D7 HIGH");
}
if (digitalRead(D8) == HIGH)
{
Serial.print("D8 HIGH");
}
digitalWrite(out, LOW);
delay(1000);
}
does anyone know why this happens? or can anyone recommend another method to do what im tryinng to do? thank you guys!!!
1 Answer 1
If I look at the first 5 lines:
byte b;
int out;
int D3 = 3;int D4 = 4;int D5 = 5;int D6 = 6;int D7 = 7;int D8 = 8;
void setup() {
Serial.begin(9600);
pinMode(out, OUTPUT);
You see that pinMode
out, OUTPUT)uses a variable
outwhich is not initialized (it's only declared in
int out`'.
I assume D3
to D8
are all output pins, so you have to replace the last line with:
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
Also, since you are using all pins in the same way, I suggest you use two variables: startPin
and endPin
. Then you can use a for
loop to iterate through all pins. Also
- always initialize variables.
- use '0' instead of 48 when you convert from a byte value to a character (since you don't need to know the ASCII value of '0').
I don't have an Arduino compiler at hand, but it should look something like:
byte b = 0;
int out = 0;
const byte startPin = 3;
const byte endPin = 8;
void setup() {
Serial.begin(9600);
for (byte pin = startPin; pin <= endPin; pin++)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
}
void loop() {
while (Serial.available() == 0) {}
b = Serial.read() - '0';
out = b;
Serial.print("\n Out is ");
Serial.println(out);
digitalWrite(out, HIGH);
delay(600);
...
However, from here you are using the pins as INPUT pins. You cannot change those on the fly, you might change them manually (by using pinMode(pin, INPUT)
), but it does not seem normal to switch input and output pins, so maybe you should explain what you are intending with your application.
-
Hey, thank you so much, your recommendation actually solve the problem so far lol, and the intention of the program is fairly simple, i just need to input number to serial monitor and made the digital pins give out the output according to the numbers, (3456 in serial monitor means pind D3 til pin D6 gives out high value one by one.Mochammad Farid– Mochammad Farid2020年08月04日 08:30:52 +00:00Commented Aug 4, 2020 at 8:30
-
but I still don't understand why you need to read the pins; if you write a HIGH you can assume the pin will be HIGH, no need (or you should) not read them, as they are configured to be output pins, not input pins.Michel Keijzers– Michel Keijzers2020年08月04日 08:33:22 +00:00Commented Aug 4, 2020 at 8:33
-
yeah i dont need to, but i just need to check whether my code is working as intended, and i have limited resources where im at right now due to corona so i stuck in a place where nobody even sell resistor or LEDMochammad Farid– Mochammad Farid2020年08月04日 08:49:55 +00:00Commented Aug 4, 2020 at 8:49
-
I doubt it will work this way, because if you change the pin mode, it will not output the signal anymore. Better use a multimeter to check the pin voltages.Michel Keijzers– Michel Keijzers2020年08月04日 09:04:29 +00:00Commented Aug 4, 2020 at 9:04
-
1At least on AVR,
digitalRead()
on an pin configured asOUTPUT
is perfectly fine.pinMode()
controls the output buffer, the input buffer is always active.Edgar Bonet– Edgar Bonet2020年08月04日 10:55:14 +00:00Commented Aug 4, 2020 at 10:55
Explore related questions
See similar questions with these tags.