Friday, March 15, 2013

Gizduino lesson 2 on_off switch

Most electronic devices has an on/off switch; you can do the same with your Gizduino. Implementing a on/off switch to your circuit with a Gizduino is possible with the use of the digital pins.

You will need the following:

 













Schematic Diagram:









Connect positve end of LED to digital pin 13 and negative end to the 100 ohm resistor. Then, connect the resistor to the ground pin. Connect the 5V power pin to one end of the pushbutton switch. On the other end of the pushbutton switch, connect it to digital pin 2; also, connect the same end to the ground pin with a 100 ohm resistor in between. 



Sketch, Compile, then Upload:

Open the button sketch found in the examples menu in the Arduino IDE.

File > Examples > Digital > Button

/*
Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 7.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave
modified 17 Jun 2009
by Tom Igoe

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Try the program you have uploaded to the Gizduino. Every time you press the pushbutton the LED will turn on, when you release the button it will turn off.

Code Overview:
 
buttonState = digitalRead(buttonPin);

The digitalRead function determines the state of a pin, whether there is voltage passing to the pin or not. When there is voltage passing to the pin, it is read as HIGH. When there is no voltage in the pin it is read as LOW. You should specify which pin is to be read; in the example, “butonPin” was used, which was set to be equal to digital pin 2. It will be easier to name the code for use later on; in this case, the code was named as “buttonState”. Instead of writing “digitalRead(buttonpin)” every time you will use this function, you can just write “buttonState”.

  if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);}
 
This is an example of a control structure. Control structures are used for controlling the logical flow of your sketch. This example is an if else type of control structure. This structure forces the Gizduino to make a decision on what to do next. The if statement should be followed by a “question” contained in the parentheses after the statement. If the expression is true, codes within the clause will be followed. The “questions” can be specified by using comparison operators, which will be posted later in the entry.


The structure states that when the buttonstate is HIGH the ledPin should turn on; otherwise, the ledPin should be off. When you press the button voltage should be able to enter the input digital pin. When voltage passes through the input digital pin the Gizduino will digitally read it as HIGH. Based on the program, when the digital read is HIGH, ledPin will turn on. On the other hand, when the pushbutton is not pressed there is no voltage that enters the digital pin, this is read as LOW by the Gizduino. Else means the previous condition was not met, the Gizduino will now follow the instructions under the else statement. Referring to the program, if the digital read is not HIGH the ledPin should be off.

Comparison Operators
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to


Notice that the program has its flaws. You constantly need to press down the pushbutton to keep the LED turned on. What you need is a alternating action switch program.

Edit Your Sketch: 

Replace the previous sketch with the program that I have written. Copy and paste the sketch below to your Arduino IDE, then compile to be sure it is free from error, then upload the sketch to your Gizduino.

const int ledPin = 13;
const int buttonPin = 2;

int val = 0;
int old_val = 0;
int state = 0;

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  val = digitalRead(buttonPin);

  if((val == HIGH) && (old_val == LOW))
  {
    state = 1 - state;
    delay(50);
  }
  old_val = val;

  if(state == 1)
  {
    digitalWrite(ledPin, HIGH);
  }
  if(state != 1)
  {
    digitalWrite(ledPin, LOW);
  }
}
 
With some modifications to the first sketch, you are now able to  turn the LED on or off with a single push of the button.
 
 
Modified Codes:
 
 
 if((val == HIGH) && (old_val == LOW))
  {
    state = 1 - state;
    delay(50);
  }

This part of the program distinguishes if the button was pressed and makes adjustments with the variables. The first line of the code states the conditions to be met before proceeding the next instructions; the condition is that the button was pressed for an instant. When the the button is pressed the variable “state” will change, either it will be 1 or 0. In the clause, state = 1 – state means that the new state is equal to 1 minus the prevoius state. For example, the value of “state” is currently 1; when you press the button, the equation will state = 1 – 1, the new value of “state” will be 0. The ”state” variable will be useful later on the sketch. The delay function is used for debouncing. Debouncing is needed because the Gizduino reads data very fast, pressing the buuton even for a split second without debouncing could send dozens of data and you would end up not having the desired action of the Gizduino. A delay of 30 – 100 microseconds would be enough to debounce your program.

 
if(state == 1)
  {
    digitalWrite(ledPin, HIGH);
  }
  if(state != 1)
  {
    digitalWrite(ledPin, LOW);
  }
 
With every press of the button, the “state” variable changes ftom a  value of 1 to 0 or 0 to 1. When the value of the “state” changes there  is a corresponding change in the mode of the LED. If the value of the  “state”variable is 1 , the LED turns on; on the other hand, when the  value of “state” is 0, the LED will turn off.

No comments:

Post a Comment