this is the code I am starting with. I have connected a switch ti what i thought to pin a3. The switch is wiredfor 5v to the pin and when the switch is tripped the voltage goes to 0v., using a 10000o resistor in the 5v line and a 1000 o. resistor in the wire to the pin.the led 13 does not light when the switch is operated. What did do wrong. In my plan there is to be two switches as bump switches that will stop the robot and reverse and turn it. Is there a better way than this ? Charlie jint buttonState=0;
int led=13;
void setup()
{
pinMode(3,INPUT);
Serial.begin(9600);
delay(25);
}
void loop()
{
buttonState=digitalRead(3);
if (buttonState==HIGH)
digitalWrite(led, HIGH);
else
digitalWrite(led,LOW);
}
if/else
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 50
- Joined: Tue Nov 29, 2011 11:59 pm
if/else
i am trying to set up a code for my motor controlled (adafruit's ) to respond to two switches I am trying the if/else setup.
- tessellated_dreams
- Posts: 17
- Joined: Sat Oct 22, 2011 9:04 pm
Re: if/else
Does this mean that you have it connected to Analog Pin 3? If so, if you want to do a digitalRead you need to address the pin as A3 in your code, not 3.CharlieJ wrote:I have connected a switch ti what i thought to pin a3.
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
Re: if/else
your code uses digital pin three. i don't know if by a3 you mean analog pin 3, but if you do the correct argument to pinMode and digitalRead is 17.
all pins on the arduino have pullups which can be enables by writing a HIGH value to the pin after setting its mode to INPUT. if you use the internal pullup all you have to do is connect one side of the switch to the input pin and the other side to ground, then when the switch is closed it will give a reading of LOW and when open it will give a reading of HIGH. this is consistent with the wiring you describe, but your code turns the led on when the input pin is HIGH.
so make sure your switch is connected to the correct pin and change your if statement to test for a LOW value.
all pins on the arduino have pullups which can be enables by writing a HIGH value to the pin after setting its mode to INPUT. if you use the internal pullup all you have to do is connect one side of the switch to the input pin and the other side to ground, then when the switch is closed it will give a reading of LOW and when open it will give a reading of HIGH. this is consistent with the wiring you describe, but your code turns the led on when the input pin is HIGH.
so make sure your switch is connected to the correct pin and change your if statement to test for a LOW value.
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
Re: if/else
guess it's time for me to read the reference, again. there are parts i haven't looked at since version 11 or so. fortunately A3 seems to be defined as 17.acolourfullife wrote: If so, if you want to do a digitalRead you need to address the pin as A3 in your code, not 3.
- baldengineer
- Posts: 127
- Joined: Sun Sep 26, 2010 11:49 pm
Re: if/else
You set pin3 to INPUT (which isn't really necessary since pins are INPUT by default) but you never set pin13 (led) to OUTPUT.
-
- Posts: 50
- Joined: Tue Nov 29, 2011 11:59 pm
Re: if/else
I was scanning down in the tutorials in controls ( Ithink) and found a section on if/else that I did not know was there. I think I will work thru the tutorials and it will help me to understand this . I guess I never looked in that area. Thanks Charlie J
Please be positive and constructive with your questions and comments.