Author Topic: Simple switch/button  (Read 10421 times)

makerbotspace

  • Newbie
  • *
  • Posts: 5
Simple switch/button
« on: February 26, 2013, 10:01:52 pm »
So I am new to the Digispark.  I have a simple use for it to start with and looking for free advice.  I want to use a transistor to control power to a project.  That part seems easy drive a pin high or low on the control leg of the transistor.  What I need help with is how to read a button in the Digispark.  It seems any pin but pin 4 should work?  Is that correct?  Is one pin better than another for this application?  I need the code to flip the state of the pin.  Push on, push off.  This seems like an easy task for the Digispark.


Thanks

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Simple switch/button
« Reply #1 on: February 26, 2013, 10:10:38 pm »
Follow this exactly:


http://arduino.cc/en/tutorial/button


Change ledPin to your transistor pin number
and buttonPin to your button pin number


I like to put buttons on pin 5 because it is usually the last to be used for other things.


Once you have that down check this one out:


http://arduino.cc/en/Tutorial/Debounce

Mark

  • Full Member
  • ***
  • Posts: 196
Re: Simple switch/button
« Reply #2 on: February 27, 2013, 04:26:20 am »
makerbotspace
 
There are lots of tutorials on the web for Arduino, and Digispark is one flavour.
 
The tutorial suggested will get you started but doesn't toggle the output.
This compiles but is NOT tested.
Its not the prettiest code, but also isn't complicated. (sorry to all you programmers out there)
 
 
Code: [Select]

 /*
    Button Toggle with debounce..
    Pin connections for Digispark
   
    P0 =
    P1 = On board LED (Rev A)
    P2 = pushbutton (P4 to +5v) use an external pulldown resistor to GND
    P3 =
    P4 =
    P5 = output to transistor (also works for relay shield) ...don't forget the series resistor to the base
 
    M.Beckett Feb 2013
  */
 
      //inputs
      const int buttonPin = 2;    // the number of the pushbutton pin
      const int ledPin =  1;        // the number of the LED pin ver A
      //const int ledPin =  0;     // the number of the LED pin ver B
      const int OutputPin = 5;   // this is the same as the relay shield
         
      //Settings
      boolean OutputState = false;        //sets output to off
      boolean lastButtonState = LOW;    // note the last button read to ensure it is released.
     
  void setup() {
 
     pinMode(ledPin, OUTPUT);   
     pinMode(buttonPin, INPUT);
     pinMode(OutputPin, OUTPUT);
     digitalWrite (OutputPin, LOW);     //turn it OFF
     //digitalWrite(buttonPin, HIGH);   //this sets the internal pullup and isn't really necessary for this
   
  }
 
  void loop()
  {
      //read the button pin and if HIGH AND doesn't equal last time then next line
      if (digitalRead(buttonPin) == HIGH && digitalRead(buttonPin) != lastButtonState)
      {
        delay(50);                             // Wait 50mS for debounce and then check again
        if (digitalRead(buttonPin) == HIGH)    // If it's still HIGH then change the outlet state
        {
          OutletControl();                     // Executes the OutletControl() routine below and then returns
        }
      }
      // ** this needs the button to restore before checking again **
 
  }
  void OutletControl()
  {
         if (OutputState == false)            // If it is OFF (note the double equals in an 'if' statement)
         {
           digitalWrite(ledPin, HIGH);        // turn LED ON
           OutputState = true;                // set the 'flag' for ON
           lastButtonState = HIGH;            // set the button state HIGH so we don't keep toggling it while its held
           digitalWrite (OutputPin, HIGH);    // turn it OFF
         }
         else                                   // It must be already ON       
         {
           digitalWrite(ledPin, LOW);           // turn LED OFF
           OutputState = false;                 // set the 'flag' for OFF
           lastButtonState = LOW;               // set the button state LOW so we don't keep toggling it while its held
           digitalWrite (OutputPin, LOW);       // turn it OFF
         }
         // 'returns' back to the line immediately after it was called.
   
  }

The lastButtonState is needed because there are no delays anywhere, it would switch ON and OFF as long as the button was held.
 
You can also digitalRead an output pin to see if its HIGH or LOW. Useful if more than one process can change it.
 
All mechanical switches have bounce (think of dropping a tennis ball onto a hard surface), and the cheaper switches are around 25mS ...hence the 50mS delay.
If the sketch is time critical there are other ways of doing this without the delay.
 
Mark