Arduino If () question

Discussion in 'Programmer's Corner' started by CoachKalk, Jun 19, 2012.

  1. CoachKalk

    Thread Starter Member

    Sep 20, 2011
    139
    2
    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).
    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.
    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
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,356
    I am not an arduino programmer but I will give this a shot.

    The statement:
    Code ( (Unknown Language)):
    1.  
    2. [COLOR=#CC6600]const[/COLOR] [COLOR=#CC6600]int[/COLOR] laser1 = 22;
    3.  
    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:
    Code ( (Unknown Language)):
    1.  
    2. [COLOR=#CC6600]if[/COLOR] (pbeasy == [COLOR=#006699]LOW[/COLOR])
    3.  
    is invalid because pbeasy is int while LOW is boolean.
     
  3. vpoko

    Member

    Jan 5, 2012
    258
    47
    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 (Text):
    1.  
    2. [COLOR=#CC6600]if[/COLOR] (DigitalRead(pbeasy) == [COLOR=#006699]LOW[/COLOR])
    3. ...
    4.  
    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.
     
  4. vpoko

    Member

    Jan 5, 2012
    258
    47
    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.
     
  5. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,769
    969
  6. CoachKalk

    Thread Starter Member

    Sep 20, 2011
    139
    2
    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![​IMG]

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

    Thanks for pointing that out!
     
Loading...