Arduino If () question

Thread Starter

CoachKalk

Joined Sep 20, 2011
141
First, if the admins think this is too closely related to my "general" question in the other section, please take appropriate corrective actions.:eek:

I have a few specific programming questions so I figured I would post here for some help.

I am using a 2n2222 transistor to switch a laser circuit on/off. The first sketch I have posted worked just like I thought it would. From previous suggestions, I know the delay function is not ideal, but I wanted an obvious indicator if I could get the desired response.

I have laser1 wired to the 2n2222 base (through a resistor).
//Laser Maze Program
//Assign laser units to pins
const int laser1 = 22;

void setup()
{
pinMode(laser1, OUTPUT);
}

void loop()
{
digitalWrite(laser1, HIGH);
delay(5000);
digitalWrite(laser1, LOW);
delay(5000);

}
Feeling quite confident at this point, I then attempted to use a PB to turn the laser on and off. I am still getting my head around when an input pin is high/low, but I figured I could get the laser to turn off and on based on if I held the button down. My thinking was the laser would either be on at start up and turn off when I held the button down or it would be the other way around.

Nope! With the following sketch, the laser was always on - regardless of PB condition. I will say I was measuring the voltage at the pbeasy pin and it was measuring correctly (0V or 5V) when the PB was pressed/released.

Here is the sketch.
//Laser Maze Program
//Assign laser units to pins
const int laser1 = 22;
const int pbeasy = 51;

void setup()
{
pinMode(laser1, OUTPUT);
pinMode(pbeasy, INPUT);
}

void loop()
{
if (pbeasy == LOW)
digitalWrite(laser1, HIGH);
else
digitalWrite(laser1, LOW);

}
So, any obvious mistakes on why laser1 wouldn't play nice with pbeasy? Small disclaimer: I am about 1 week into learning how to program so go easy and talk slow.

Thanks
 

MrChips

Joined Oct 2, 2009
35,095
I am not an arduino programmer but I will give this a shot.

The statement:
Rich (BB code):
const int laser1 = 22;
makes laser1 a constant value and will not change. Remove the const.

The words LOW and HIGH are boolean values. Usually these are defined as zero and no-zero respectively.

Hence the statement:
Rich (BB code):
if (pbeasy == LOW)
is invalid because pbeasy is int while LOW is boolean.
 

vpoko

Joined Jan 5, 2012
267
I'm assuming "pbeasy = 51" represents pin 51, which is an input pin connected to your push-button. Your current code is not checking whether pin 51 is currently HIGH or LOW, it's actually checking whether "51 == LOW", which it isn't.

You need to do use the DigitalRead() function to read the current status of the pin:

Code:
[COLOR=#CC6600]if[/COLOR] (DigitalRead(pbeasy) == [COLOR=#006699]LOW[/COLOR])
...
Note that you will have some bounce when you push and release the button (the laser may very quickly flash on/off before settling into the correct state). See the Arduino tutorial on debouncing switches if you want to eliminate it.
 

vpoko

Joined Jan 5, 2012
267
Rich (BB code):
const int laser1 = 22;
makes laser1 a constant value and will not change. Remove the const.
This is actually OK, 22 represents the pin# that the laser is connected to, which doesn't change.

Also, unlike more modern languages, C does let you mix booleans and integers. 0 is false while any other value is true.
 

Thread Starter

CoachKalk

Joined Sep 20, 2011
141
I'm assuming "pbeasy = 51" represents pin 51, which is an input pin connected to your push-button. Your current code is not checking whether pin 51 is currently HIGH or LOW, it's actually checking whether "51 == LOW", which it isn't.

You need to do use the DigitalRead() function to read the current status of the pin:

Code:
[COLOR=#CC6600]if[/COLOR] (DigitalRead(pbeasy) == [COLOR=#006699]LOW[/COLOR])
...
Note that you will have some bounce when you push and release the button (the laser may very quickly flash on/off before settling into the correct state). See the Arduino tutorial on debouncing switches if you want to eliminate it.
Now I feel like an idiot. If you saw how many notes I have taken and pages I have printed (even some actual lines I hand wrote on paper), where I had the digitalRead portion correct. Then, when I go to actually write the sketch, I do it wrong!


Maybe this public embarrassment will burn this into my brain! I can only hope - but not promising.

Thanks for pointing that out!
 
Top