Category Archives: Uncategorized

Selector Switch with ARDUINO

A fellow Arduino guy from Texas asked about how to set up a multi-mode selector switch using a single pushbutton and 3 states (not including the boot up state). What follows is this discussion that we had becoming a back and forth collaboration of ideas until I got off work and decided to see if I could make it more robust and simpler at the same time.

Lets talk about the original code…  it essentially worked… with some quirks.

The original code would continuously set all the pins… every loop, even though it didn’t need to…

It also tried to process the loop even if the mode was equal to zero (IE; No button presses have occurred yet and in that case, all LED’s should be off)

Lastly, it got weird when mode was incremented to 4 and set back to 1 and in here is the behavior that I wanted to eliminate.  It would change to mode 1 and 2 and 3 when the key was PRESSED but would only go back to mode 1 when button was released when in mode 3.  This is just not the way to make all the button presses seem similar.  All the other buttons react on the button press, not the release.

So it was SWITCH/CASE to the rescue.  With switch case statements I could handle all the mode switching and deal with mode 1 and any other mode that was not 2 or 3 with the default statement.  This allowed it to have all button mode migration to appear to behave the same.

The code also makes sure that pins do not get changed when they don’t need to be… IE; every loop traversal.

On to the final code example…

/*
 Using a single switch to select between 3 modes
*/
// Schematic: http://www.pwillard.com/files/mode4.jpg
//===============================================================
// Global Variables & Constants
//===============================================================

const int ledPinOne = 2; // LED1 ANODE
const int ledPinTwo = 4; // LED2 ANODE
const int ledPinThree = 7; // LED3 ANODE
const int modePin = 13; // Active HIGH, held low by 4.7K

int mode = 0; // Selector State (Initial state = ALL OFF)
int val = 0; // Pin 13 HIGH/LOW Status
int butState = 0; // Last Button State
int modeState = 0; // Last Mode State
boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled

//===============================================================
// SETUP
//===============================================================
void setup () {
 pinMode(ledPinOne, OUTPUT);
 pinMode(ledPinTwo, OUTPUT);
 pinMode(ledPinThree, OUTPUT);
 pinMode(modePin, INPUT);
 if (debug){
 Serial.begin(9600);
 Serial.print("Initial Mode: ");
 Serial.println(mode);
 Serial.print("Setup Complete\n");
 }
}

//===============================================================
// Main Loop
//===============================================================
void loop() {

 val = digitalRead(modePin);

 // If we see a change in button state, increment mode value
 if (val != butState && val == HIGH){
 mode++;
 }

 butState = val; // Keep track of most recent button state

 // No need to keep setting pins *every* loop
 if (modeState != mode){

 // If no keys have been pressed yet don't execute
 // the switch code below
 // if (mode != 0) {

 switch ( mode ) {
 //case 1 is actually handled below as default

case 2:
 digitalWrite(ledPinOne, LOW);
 digitalWrite(ledPinTwo, HIGH);
 showState();
 break;
 case 3:
 digitalWrite(ledPinTwo, LOW);
 digitalWrite(ledPinThree, HIGH);
 showState();
 break;
 default:
 mode = 1;
 // loop back to 1 by default, seems redundant but
 // it also handles the "mode is > 3" problem
 digitalWrite(ledPinThree, LOW);
 digitalWrite(ledPinOne, HIGH);
 showState();
 break;
 } // end switch
// } // end of "if mode = 0" check
 } // end of ModeState check
 modeState = mode; // Keep track of mode recent mode value
 delay(10); // slow the loop just a bit for debounce
}

//===============================================================
// Subroutine
//===============================================================
void showState() {
 if (debug){
 Serial.print("Mode: ");
 Serial.println(mode);
 }
}

Tagged

TouchPad Hacking

HP did a nice thing for it’s employees recently.  After the unannounced fire sale of HP Touchpad’s at retailers, they offered up a similar sale for employee purchase.  Needless to say, more than a few employees took advantage of the offer.

Now to be fair, I never really intended to use it “as is”.  I was “this close” [hold thumb and index finger really close together] to buying a NOOK Color for hacking purposes and when this opportunity arose… I knew it would be just a matter of time before someone developed a Google Android OS solution to offset the rather limp offerings in the WebOS application market.

It didn’t take long.  Now after the second patch of the Alpha release of CyanogenMod 7 for the HP Touchpad, I can say that this is an excellent Android platform.  I could actually see HP tweaking it a bit and allowing it to become an awesome Android tablet… but that is some wishful thinking on my part.

More info here

 

I’m Back.

Back from the virtual  grave.  Lets so how things go from here…

 

What’s new?

Fry’s Retail Stores now sell arduino variants right off the shelves…   including the mega2560.  This is good news!

 

By the way:

Hosting companies that don’t warn they are closing up shop or even say goodbye are rather cowardly.  No happy feelings towards Johnnie Nelke of the now defunct  “HostingRevolution” right now.  If anyone ever sits down next to this guy at a bar, smack him for me.