All my professional life had only been about software and I never really had any exposure to electronics. Arduino was in my radar but I thought I would just keep it in the box like I did with another large electronics kit I got more than a decade ago. But then, I saw the “$9 ARDUINO Compatible STARTER KIT” and so went and pledged. After a couple of months, the kit arrived but it was sitting in the shelf. Part of the reason is that I changed jobs and it’s been very busy. This crowd sourced project has been very well executed. So, as soon as the same owner started another project that supplements the first, I pledged for it and as part of that decision I thought I would at least give the board that’s already with me a try.
So I set up the Arduino IDE (which seems to still require Java 1.6 so had to down load that) and tried out a few samples. Coming from software background the difficult part for me is following the schematics for the hardware and debugging if something went wrong. Soon I wanted to write something of my own. So I modified the ButtonStateChange example and converted into what I call a Guessing Game. In the ButtonStateChange everytime the button is pressed 4*x times, it glows. Instead, I modified it to glow after every n times where n is between 2 and 4 inclusive. I am sure no adult would probably consider it as a game. However, my 4 yr old son and I had a lot of fun with it. We took turns and one of us would guess and the other would press it to see if it LED glowed as per the guess number of pushes or not. If it did, the one guessing would win.
Here is my little program if you want to make your “beginner tutorial” a bit more fun.
/*
* Guessing game
* Guess a number between 2 to 4. Press the button those many times.
* If the LED glows on the exact count, guessing person wins
* Otherwise, the person pressing the button wins.
* Circuit: same as http://arduino.cc/en/Tutorial/ButtonStateChange
*/// this constant won’t change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int rcount = random(1,5);
boolean pushed = false;void setup() {
// initialize the button pin as an input
pinMode(buttonPin, INPUT);
// initialize the LED as an output
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
// initialize serial communication
Serial.begin(9600);
}void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
pushed = false;
// if the button is pushed, increment the counter
if (buttonState == HIGH) {
buttonPushCounter++;
pushed = true;
}
}
else {
return;
}
lastButtonState = buttonState;
if(!pushed) return;
if (buttonPushCounter == rcount) {
buttonPushCounter = 0;
digitalWrite(ledPin, HIGH);
if(pushed)
rcount = random(2,5);
} else {
digitalWrite(ledPin, LOW);
}
}
WordPress seems to be messing up with the code formatting. But the code is very small for someone to read and understand it.