This shows you the differences between the selected revision and the current version of the page.
| — | other:dog_source 2009/01/26 19:13 current | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | <code c++> | ||
| + | /* Robot Dog - backs away from nasty chemicals | ||
| + | * | ||
| + | * Tarim | ||
| + | * | ||
| + | * January 2009 | ||
| + | * | ||
| + | */ | ||
| + | |||
| + | // Chemical sensor thresholds | ||
| + | int UpperThresh = 185; | ||
| + | int LowerThresh = 185; | ||
| + | |||
| + | // Amount of time (milleseconds) that doggie will walk or stand still | ||
| + | int WalkSlice = 2000; | ||
| + | int StaySlice = 1000; | ||
| + | |||
| + | // Arduino pins | ||
| + | int LedPin = 13; // Doggie heartbeat | ||
| + | int ForwardPin = 12; // H-bridge motor input 1 | ||
| + | int BackwardPin = 11; // H-bridge motor input 2 | ||
| + | int SpeedPin = 9; // H-bridge motor enable | ||
| + | int ChemPin = 2; // Chemical sensor analog input | ||
| + | |||
| + | |||
| + | // Arduino initialisation | ||
| + | void setup() { | ||
| + | pinMode(LedPin, OUTPUT); | ||
| + | pinMode(ForwardPin, OUTPUT); | ||
| + | pinMode(BackwardPin, OUTPUT); | ||
| + | pinMode(SpeedPin, OUTPUT); | ||
| + | pinMode(ChemPin, INPUT); | ||
| + | Serial.begin(9600); | ||
| + | } | ||
| + | |||
| + | |||
| + | // Print a value | ||
| + | void Print(char* str, int val) { | ||
| + | Serial.print(str); | ||
| + | Serial.println(val); | ||
| + | } | ||
| + | |||
| + | // Walk routine can go forwards, backwards or stay | ||
| + | #define FORWARD 255 | ||
| + | #define BACKWARD (-255) | ||
| + | #define STAY 0 | ||
| + | |||
| + | void Walk(int speed) { | ||
| + | if(speed > 0) { // Forward | ||
| + | digitalWrite(ForwardPin, HIGH); | ||
| + | digitalWrite(BackwardPin, LOW); | ||
| + | analogWrite(SpeedPin, speed); | ||
| + | |||
| + | } else if(speed < 0) { // Backward | ||
| + | digitalWrite(ForwardPin, LOW); | ||
| + | digitalWrite(BackwardPin, HIGH); | ||
| + | analogWrite(SpeedPin, -speed); | ||
| + | |||
| + | } else { // Stay | ||
| + | digitalWrite(ForwardPin, HIGH); | ||
| + | digitalWrite(BackwardPin, HIGH); | ||
| + | analogWrite(SpeedPin, 0); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | /* TimeSlice splits time since startup according to ratio of on:off | ||
| + | * Start with off as Arduino known to reset when battery is low | ||
| + | * | ||
| + | * | Time -> | ||
| + | * | off | on | off | on | off | on | off | ... | ||
| + | * | ||
| + | */ | ||
| + | |||
| + | bool TimeSlice(unsigned long on, unsigned long off) { | ||
| + | unsigned long m; | ||
| + | m = millis(); // time lasts 9 hours from startup | ||
| + | Print("T", m); | ||
| + | m %= off + on; // modulo (off + on) | ||
| + | // Print("/", m); | ||
| + | return m > off; // off when Arduino starts up | ||
| + | } | ||
| + | |||
| + | // Pulse the LED val times in ratio on:off | ||
| + | void pulseWrite(int pin, int val, int on, int off) { | ||
| + | while(--val >= 0) { | ||
| + | digitalWrite(pin, HIGH); | ||
| + | delay(on); | ||
| + | digitalWrite(pin, LOW); | ||
| + | delay(off); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // "Heartbeat" is really a representation of value of chemical sensor | ||
| + | void numberWrite(int pin, int val) { | ||
| + | int tmp; | ||
| + | // 100s as half second pulses | ||
| + | pulseWrite(pin, tmp = val / 100, 500, 500); | ||
| + | delay(500); | ||
| + | // 10s as quarter second pulses | ||
| + | pulseWrite(pin, (val - tmp * 100) / 10, 250, 250); | ||
| + | delay(750); | ||
| + | // don't bother with units | ||
| + | } | ||
| + | |||
| + | // Arduino main loop | ||
| + | void loop() { | ||
| + | int val; | ||
| + | bool walkies; | ||
| + | |||
| + | // sensor value | ||
| + | val = analogRead(ChemPin); | ||
| + | Print("C", val); | ||
| + | |||
| + | // behaviour | ||
| + | if(val > UpperThresh) { // back away from nasty chemicals | ||
| + | Walk(BACKWARD); | ||
| + | |||
| + | } else if(val < LowerThresh) { // either walk or stay | ||
| + | walkies = TimeSlice(WalkSlice, StaySlice); | ||
| + | Print("W", walkies); | ||
| + | Walk(walkies ? FORWARD : STAY); | ||
| + | } | ||
| + | |||
| + | numberWrite(LedPin, val); | ||
| + | } | ||
| + | </code> | ||