Arduino is an open source platform composed of a microcontroller, and a development environment. It is a one stop shop for realizing those electronics projects that have been kicking around in the back of your mind for the past few years. You know, the theremin alarm clock that requires you to play the theme to tetris to turn it off...
Evan Farrar's Lightning talk about Arduino inspired me to buy one. And it took me awhile to get around to playing with the thing, but I finally did, and it is fantastic! For my first little project I undertook to reproduce this light mixing project. It was a little too easy, so in order to make it more interesting I decided to implement the state of the 3 lights as a Ripple Carry Adder. And to control it all with a button. It wasn't too hard, although I do have some questions about the Arduino programming language. Their documentation doesn't give any examples of how to use arrays as parameters to functions. They say the language is based on C, but when I try a C style pointer reference I get a funky error. Also when I try to assign one array the value of another array I get errors. My code works around these problems, but if anyone can show me some examples of how to do this correctly in the Arduino language I would appreciate it. Here is my first arduino code:
int pinStates[] = {HIGH,LOW,LOW};
int fullAddResult[2]; //ugh, global since i can't seem to pass arrays around
int halfAddResult[2];
int greenPin = 12;
int redPin = 11;
int bluePin = 10;
int inPin = 2;
int inPinVal = 0;
int prev;
boolean autoPilot = false;
void setup(){
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(inPin, INPUT);
prev = digitalRead(inPin);
}
//current, as in electrical current
int currentVal(boolean a){
if (a == true){
return HIGH;
}
else{
return LOW;
}
}
boolean boolVal(int a){
if (a == HIGH){
return true;
}
else{
return false;
}
}
int nand(int a, int b){
return currentVal(!(boolVal(a) && boolVal(b)));
}
int exor(int a, int b){
int ab;
int aab;
int bab;
ab = nand(a, b);
aab = nand(a, ab);
bab = nand(ab, b);
return nand(aab, bab);
}
// method implements a half adder
void halfAdd(int a, int b){
halfAddResult[0] = currentVal(boolVal(a) && boolVal(b));
halfAddResult[1] = exor(a, b);
}
// implements a full adder
void fullAdd(int a, int b, int c){
int ab[2];
int abc[2];
halfAdd(a, b);
ab[0] = halfAddResult[0];//Lame way of copying array values
ab[1] = halfAddResult[1];
halfAdd(ab[1], c);
abc[0] = halfAddResult[0];//Still Lame
abc[1] = halfAddResult[1];
fullAddResult[0] = currentVal(boolVal(ab[0]) || boolVal(abc[0]));
fullAddResult[1] = abc[1];
}
// implements a ripple carry adder
// MSB is the 0th element of the pinStates array
void increment(){
int temp[2];
halfAdd(pinStates[2], HIGH);
temp[0] = halfAddResult[0];
temp[1] = halfAddResult[1];
fullAdd(pinStates[1], temp[0], LOW);
halfAdd(pinStates[0], fullAddResult[0]);
pinStates[0] = halfAddResult[1];
pinStates[1] = fullAddResult[1];
pinStates[2] = temp[1];
}
void loop(){
inPinVal = digitalRead(inPin);
digitalWrite(redPin, pinStates[0]);
digitalWrite(bluePin, pinStates[1]);
digitalWrite(greenPin, pinStates[2]);
if (pinStates[0] == LOW && pinStates[1] == LOW && pinStates[2] == LOW){
autoPilot = true;
}
if (prev != inPinVal){
increment();
prev = inPinVal;
autoPilot = false;
}
if (autoPilot){
increment();
delay(500);
}
}
One note is that xor seems to be a reserved word (although the compiler doesn't tell you that, it just barfs up some meaningless error) so that is why my xor function is named exor. Obviously I didn't bother with Arduino's bitwise functions. My Arduino code leaves something to be desired and so do my pictures, sorry they are blurry. The code could use some cleanup, but when you flash it over to the microcontroller, it works! Toggling the button causes the light mixer to shuffle through all possible color combinations by doing binary addition on a 3 bit number. Each bit of the number represents a different color and so by running from 0 to 7 you can go from no light to white light with all the colors in between.
No comments:
Post a Comment