Monday, November 14, 2011

Sparkfun Inventor's Kit Circuit-07: A Push-Button World

With what they describe as an "underwhelming" example the Inventor's Kit manual brings us into what may be the most important part of creating an autonomous vehicle: sensing.  In the first 6 tutorials everything was about output -- getting the robot to "do something".  Trouble is that this tethers the human to the mechanical being, and why people with control fantasies may be satisfied with robots that do exactly and only what they tell them to when they tell them to, the rest of us are fascinated by the idea of giving our co-creations some semblance of "free will".

To be autonomous, a robot needs to be "aware" of its environment.  It needs to be able to hear and to see, to touch and taste and even smell. Watching and listening will come later, but this tutorial prepares the robot to "feel" via one of the simplest of "touch sensors" - the push button.

The set up is fairly simple (2 pushbuttons, a red LED, a 330 Ohm resistor and a 10 KOhm resistor), and for the first sketch only one of the pushbuttons is used.  As the book states, "Underwhelmed? No worries, these circuits are all super stripped down to make playing with the component easy, but once you throw them together the sky is the limit."

Here is the first code:


/*
  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 <http://www.0j0.org>
 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);
  }
}


You download it. You press the push button. The LED goes off. You release the button. It goes back on.  Bravo. So what?
But then you start modding.

Here is the modded code to use both pushbuttons, one to turn the LED on, one to turn it off (I commented out the original code, but it is still there:)

/*
  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 <http://www.0j0.org>
 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
int inputPin1 = 3;  //button 1
int inputPin2 = 2;   //button 2

// 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);    
 pinMode(inputPin1,INPUT); //make button 1 an input
 pinMode(inputPin2,INPUT); //make button 2 an 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);
 // }
//}

if(digitalRead(inputPin1)==LOW) {
  digitalWrite(ledPin, LOW); //turn LED OFF
} else if (digitalRead(inputPin2)==LOW) {
  digitalWrite(ledPin, HIGH); //turn LED ON
}
 }


The next hack is "fading up and down". This means using the push buttons to control an analog signal.  We start by changing the wire connecting the LED to pin 9 from pin 13 and change it in the code to, so it reads int ledPin = 9;
Then the loop() code is changed to:
int value = 0;
void loop() {
if (digitalRead(inputPin1) == LOW) { value--; }
else if (digitalRead(inputPin2) == LOW) { value++;}
value = constrain(value, 0, 255);
analogWrite(ledPin, value);
delay(10);
}


For doing the fade up and fade down, this is the code (I had to type it in, but it's done, so why should you?!):


// 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 =  9;      // the number of the LED pin
int inputPin1 = 3;  //button 1
int inputPin2 = 2;   //button 2

// 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);    
 pinMode(inputPin1,INPUT); //make button 1 an input
 pinMode(inputPin2,INPUT); //make button 2 an input
}
int value = 0;
void loop() {
if (digitalRead(inputPin1) == LOW) { value--; }
else if (digitalRead(inputPin2) == LOW) { value++;}
value = constrain(value, 0, 255);
analogWrite(ledPin, value);
delay(10);
}


And they say "if you would like the LED to fade faster or slower, there is only one line of code that needs changing:
delay(10); can be made into delay(5); to go twice as fast, and delay(20); to go twice as slow, and everything inbetween.

Enjoy;  now that we are into sensing I can sense things are going to get a lot more fun!

No comments:

Post a Comment