THIS BLOG HAS MOVED. PLEASE UPDATE ANY BOOKMARKS TO GO DIRECTLY TO WWW.RAISINGGEEKS.COM

Sunday, January 17, 2010

The day after Christmas I headed right back to the Arduino with a burst of both enthusiasm and confidence. It had to be a good day - after all Radio Shack was open!

I got one of the analog gauges and returned to the link that got me excited about Arduino originally - "Arduino Analog Gauge"

A few minutes later, I was controlling the "Leak Rate" analog gauge from the Control Panel.



I then moved to how to control the lamps in the lighted switches, the piezo siren and the strobe. I found an online example showing the use of TIP-120 transistors. After a quick trip to Radio Shack, I had some TIP-120s. A few minutes later, I could turn the button lamps on / off and even dim them using PWM.

I was hooked. All this time I wanted to increase my electronics knowledge, but I'd never played with microcontrollers. Suddenly I realized that the microcontroller does all the logic and I just needed to build basic connection circuits to switches, sensors and output devices. Suddenly all my plans for world domination were possible! Ok, no real plans for world domination, but so many of my ideas from the past were now feasible.

I started thinking of how to demonstrate all this to my son. I built the left side access panel with the 3 lit buttons, and the strobe. I built a breadboard circuit (another trip to Radio Shack to get a standalone breadboard) with 4 TIP-120s (3 button lamps and the strobe), and used two trim pots to "calibrate" the gauges. The three buttons and 10k pot on the Lab were used as inputs.



The code read the input pot and used that to determine the rate used to flash the buttons AND the value displayed on the Leak Rate gauge. If the value went above the midpoint of the gauge, the green button would light and the strobe would activate. Each of the input buttons was given a value and the values of pushed buttons were summed and displayed on the Engine Degrees gauge. It made no sense, but it gave me a way to debug that I was reading the switches properly  and it would show my son the concept of inputs + logic = outputs. About this same time I was getting really annoyed that I couldn't easily debug my code - then I found the serial output commands and the serial monitor in the IDE. What an AWESOME feature.

Here is some of my early Arduino code for the above prototype


#define POT_DELAY 0
#define GAUGE_LR 3
#define GAUGE_ED 5

#define LAMP_RED 6 //PWM pin
#define LAMP_YELLOW 7 //NOT PWM
#define LAMP_GREEN 8 //NOT PWM

#define STROBE 10

#define BUTTON_RED 2
#define BUTTON_YELLOW 4
#define BUTTON_GREEN 12



int potValue = 0; // value returned from the potentiometer


void setup() {
// set the transistor pin as output:
pinMode(POT_DELAY, INPUT);
pinMode(GAUGE_LR, OUTPUT);
pinMode(GAUGE_ED, OUTPUT);
pinMode(LAMP_RED, OUTPUT);
pinMode(LAMP_YELLOW, OUTPUT);
pinMode(LAMP_GREEN, OUTPUT);
pinMode(STROBE, OUTPUT);
pinMode(BUTTON_RED, INPUT);
pinMode(BUTTON_YELLOW, INPUT);
pinMode(BUTTON_GREEN, INPUT);

Serial.begin(9600); // set up Serial library at 9600 bps

Serial.println("Hello world!"); // prints hello with ending line break

switchOff();

}

void switchOff()
{
analogWrite (GAUGE_ED,0);
analogWrite (GAUGE_LR,0);
analogWrite (LAMP_GREEN,0); //switch to digital?
analogWrite (LAMP_YELLOW,0); //switch to digital?
analogWrite (LAMP_RED,0);
analogWrite (STROBE,0); //digital?

}

void loop() {
// read the potentiometer, convert it to 0 - 255:
potValue = analogRead(POT_DELAY);
int leakRate = 255-(potValue/4);

int buttonRedValue = digitalRead(BUTTON_RED)*32;
int buttonYellowValue = digitalRead(BUTTON_YELLOW)*64;
int buttonGreenValue = digitalRead(BUTTON_GREEN)*128;


analogWrite(GAUGE_LR, 255-(potValue/4));

analogWrite(GAUGE_ED, buttonRedValue+buttonYellowValue+buttonGreenValue);
analogWrite(LAMP_RED, 255);
analogWrite(LAMP_YELLOW, 0);
if (leakRate > 128)
{
analogWrite(LAMP_GREEN, 255);
analogWrite(STROBE, 255);

}
else
{
analogWrite(LAMP_GREEN, 0);
analogWrite(STROBE, 0);
}

delay (potValue);


analogWrite(LAMP_RED, 0);
analogWrite(LAMP_YELLOW, 255);
//analogWrite(LAMP_GREEN, 255);
delay(potValue);
Serial.print(potValue);
Serial.print(":");
Serial.println(buttonRedValue+buttonYellowValue+buttonGreenValue);


}

No comments: