Hello
Übernoob warning!
I've got a Centipede shield;
http://docs.macetech.com/doku.php/centipede_shield
But damn, I can't seem to figure out how to connect regular tact pushbuttons to it.
Do I need pull down resistors, like the standard arduino pushbutton scetch, or can I use the "internal 100k" and the .pinPullup function?
I.e. do I need to multiply something like this for each button: http://www.arduino.cc/en/Tutorial/Button
Could someone (Mr Macetech maybe...) please explain with a simple scetch how I connect a button and how to read it's state, and maybe add to the documentation.
Thanks
F
This is how you change the basic button example to work with the button on shield pin 98 and the LED on shield pin 99.
#include <Wire.h>
#include <Centipede.h>
Centipede CS; // create Centipede object
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 98; // the number of the pushbutton pin (was 2)
const int ledPin = 99; // the number of the LED pin (was 13)
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
CS.pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
CS.pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = CS.digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
CS.digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
CS.digitalWrite(ledPin, LOW);
}
}
Ok, great, thanks!
So the schematics, will I need a resistor for each button like in the example?
Just thought if the internal 100k pull up resistor was to use for stuff like buttons to make my life easier...
Cheers!
Enabling the pull-up resistor on a shield input pin is close to the way you do it on an Arduino pin but you use CS.pinPullup(pin,HIGH) instead of digitalWrite(pin, HIGH).
void setup() {
// initialize the LED pin as an output
CS.pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input and enable the pull-up:
CS.pinMode(buttonPin, INPUT);
CS.pinPullup(buttonPin, HIGH);
}
Allright, got it working it seems! The following code will light up the on-board led (13) when Centipede pin 10 is connected to ground.
#include <Wire.h>
#include <Centipede.h>
Centipede CS;
const int buttonPin = 10;
int buttonState = 0;
void setup() {
Wire.begin();
CS.initialize();
pinMode(13, OUTPUT);
CS.pinMode(buttonPin, INPUT);
CS.pinPullup(buttonPin, HIGH);
}
void loop() {
buttonState = CS.digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
}