gmp1457
December 16, 2022, 2:56am
1
I followed a guide on it and just copied the code, to me I see it as fine but I'm still new ish to Arduino.
The serial monitor read:
103
867
206
771
337
643
441
533
577
406
695
297
803
It read this no matter what position the switch was in. Its a 3 prong on/off/on spdt switch.
int switchpin = A4;
int redLED = 5;
void setup() {
pinMode(switchpin, INPUT);
pinMode(redLED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A4);
Serial.println(sensorValue);
if (analogRead(A4) > 100) {
digitalWrite(redLED, HIGH);
}
else digitalWrite(redLED, LOW);
delay(10);
}
I will also put a photo of the breadboard in the replies, but be wear the wiring is extremely messy because I only have long wires at this moment.
Can someone help me figure out what's wrong because i have no clue.
gmp1457
December 16, 2022, 2:59am
2
The schematic for what i used but i used a different switch, the switch i used was
alto777
December 16, 2022, 3:16am
3
Just noticed your code doesn't use the switch at all…
Check your wiring . This looks like random values coming from a completely unconnected analog input.
Confirm you are using all the components in the circuit you found.
Try a jumper wire where the switch contacts are supposed to be instead of the switch.
Confirm you are using the switch correctly, ohmmeter or a simple circuit.
a7
gmp1457
December 16, 2022, 3:21am
4
Wait im confused, wdym by its not using the switch at all? Sorry im still new ish to arduino
red_car
December 16, 2022, 3:23am
5
For a switch like the one you are using you are better to use digitalRead rather than analogRead. It will return either HIGH or LOW.
alto777
December 16, 2022, 3:24am
6
gmp1457:
int switchpin = A4;
You have a name for the analog input.
But you don't use the name
int sensorValue = analogRead(A4);
so I got confused. Usually once you have a name for a pin, it is referred to by that name, that's the hole point of naming it.
Works the same, reads funny.
a7
alto777
December 16, 2022, 3:26am
7
Yeah, the site uses analofRead () for some reason.
Tropical Engineer, maybe the heat has gotten to them.
a7
gmp1457
December 16, 2022, 3:30am
8
I changed the code to
int switchpin = A4;
int redLED = 5;
void setup() {
pinMode(switchpin, INPUT);
pinMode(redLED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = digitalRead(A4);
Serial.println(sensorValue);
if (digitalRead(A4) > 100) {
digitalWrite(redLED, HIGH);
}
else digitalWrite(redLED, LOW);
delay(100);
}
All i did was change the analogReads to digitalReads and now on the serial monitor theres only 1's and 0's showing up, but they still seem random and non reactive to the switch.
gmp1457
December 16, 2022, 3:31am
9
I changed the code to
int switchpin = A4;
int redLED = 5;
void setup() {
pinMode(switchpin, INPUT);
pinMode(redLED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = digitalRead(A4);
Serial.println(sensorValue);
if (digitalRead(A4) > 100) {
digitalWrite(redLED, HIGH);
}
else digitalWrite(redLED, LOW);
delay(100);
}
All i did was change the analogReads to digitalReads and now on the serial monitor theres only 1's and 0's showing up, but they still seem random and non reactive to the switch. I am very confused lmao
LarryD
December 16, 2022, 3:32am
10
digitalRead(A4) will give you a HIGH (1) or LOW (0)
red_car
December 16, 2022, 3:33am
11
This is not relevant if using digitalRead. Just use...
if (digitalRead(switchPin) == HIGH)
I agree with a777... you likely have a wiring issue if you are getting random results.
BTW... the code on that site looks pretty rubbish... I would be reluctant to use their examples.
alto777
December 16, 2022, 3:37am
13
Or
if (sensorValue == HIGH)…
since it was digitalRead () earlier.
sensorValue is a misleading name, probably leftover form when analogRead () was being used to actually read some analog sensor value...
a7
gmp1457
December 16, 2022, 3:39am
14
It must be a wiring issue then is my guess, but everything seems right and all my parts are new.
LarryD
December 16, 2022, 3:41am
15
Wire your switches as S3 above, look for a LOW when the switch is closed.
LarryD
December 16, 2022, 4:21am
17
Wire switches similar to the way S3 is wired in the schematic.
i.e. one side of your switch goes to the Arduino input pin, the other side of your switch goes to Arduino GND (0 volts).
In setup( ) add a line similar to below for each hardware switch you have:
const byte mySwitch = 3;
. . .
void setup( )
{
. . .
pinMode(mySwitch, INPUT_PULLUP); //turn on internal pull-up resistor on this input pin
. . .
} //END of setup( )
void loop( )
{
. . .
// is this switch pushed ?
if (digitalRead(mySwitch) == LOW)
{
//do something
}
. . .
} //END of loop( )
system
Closed
June 14, 2023, 4:22am
18
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.