fancart.pde
// Fan cart driver
// Eric Ayars
// 6/16/10
#define MOTORDRIVE 10
#define MOTORINDICATOR 13
#define SELECTSWITCH 11
#define REEDSWITCH 12
#define POWER1 7
#define POWER2 8
#define POWER3 9
#define DEBOUNCE 100
byte PowerSetting = 2; // Ranges from 0 to 3
byte PowerLevel[] = {0, 130, 200, 255};
int MotorState = 0;
byte LastInputState = HIGH;
byte LastReedState = HIGH;
boolean ButtonPressed() {
boolean state;
byte CurrentState = digitalRead(SELECTSWITCH);
if ((LastInputState == HIGH) && (CurrentState == LOW)) {
// button has just been pressed
state = true;
delay(DEBOUNCE);
} else {
state = false;
}
LastInputState = CurrentState;
return state;
}
boolean MagnetSensed() {
boolean state;
byte CurrentState = digitalRead(REEDSWITCH);
if ((LastReedState == HIGH) && (digitalRead(REEDSWITCH) == LOW)) {
// Cart has just passed a magnet
state = true;
delay(DEBOUNCE);
} else {
state = false;
}
LastReedState = CurrentState;
return state;
}
void setup() {
// Set appropriate pins to output
pinMode(MOTORINDICATOR, OUTPUT);
pinMode(POWER1, OUTPUT);
pinMode(POWER2, OUTPUT);
pinMode(POWER3, OUTPUT);
// Turn off motor, just in case
analogWrite(MOTORDRIVE, 0);
digitalWrite(MOTORINDICATOR, LOW);
// Set input pins
pinMode(SELECTSWITCH, INPUT);
pinMode(REEDSWITCH, INPUT);
}
void loop() {
// Check status of "user interface"
if (ButtonPressed()) {
PowerSetting += 1;
if (PowerSetting > 3) {
PowerSetting = 0;
}
}
// Check status of reed switch
if (MagnetSensed()) {
// Toggle between 0 and 1
MotorState = -MotorState + 1;
}
// Turn on appropriate lights
if (PowerSetting > 0) {
digitalWrite(POWER1, LOW);
} else {
digitalWrite(POWER1, HIGH);
}
if (PowerSetting > 1) {
digitalWrite(POWER2, LOW);
} else {
digitalWrite(POWER2, HIGH);
}
if (PowerSetting > 2) {
digitalWrite(POWER3, LOW);
} else {
digitalWrite(POWER3, HIGH);
}
// Set motor state
if (MotorState) {
analogWrite(MOTORDRIVE, PowerLevel[PowerSetting]);
digitalWrite(MOTORINDICATOR, HIGH);
} else {
analogWrite(MOTORDRIVE, 0);
digitalWrite(MOTORINDICATOR, LOW);
}
}
Generated by GNU enscript 1.6.4.