/* Input Pullup Serial This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital input on pin 2 and prints the results to the serial monitor. The circuit: * Momentary switch attached from pin 2 to ground * Built-in LED on pin 13 Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal 20K-ohm resistor is pulled to 5V. This configuration causes the input to read HIGH when the switch is open, and LOW when it is closed. created 14 March 2012 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/InputPullupSerial This example code is in the public domain */ #include //***Last updated to 7 //**CODE CHANGES** //PIN_F5 put to high int val[8]; int detect[8]; int lock[8]; boolean drive; int distance; int high; int pinM1en = 18; int pinM1for = 5; int pinM1rev = 4; //Motor 2 (Right): int pinM2en = 17; int pinM2for = 12; int pinM2rev = 14; int power = PIN_F5; // Calibration Variables: int rate = 500; //rate of movement (mSec/in) int turnrate = 25; //turn rate (mSec/deg) int pwmspd = 225; //speed of motors (0-255) pwm control int beep = 0; void flip(void){ if(high == 1){ high = 0; //Serial.println("Now Low!"); digitalWrite(PIN_C6, LOW); } else{ high = 1; //Serial.println("Now High!"); digitalWrite(PIN_C6, HIGH); } } void setup(){ //start serial connection //***Last updated to 7 high = 1; Timer1.initialize(13); Timer1.attachInterrupt(flip); //Timer1.start(); pinMode(PIN_B0, INPUT); pinMode(PIN_B1, INPUT); pinMode(PIN_B2, INPUT); pinMode(PIN_B3, INPUT); pinMode(PIN_F1, INPUT); pinMode(PIN_F4, INPUT); pinMode(PIN_D1, INPUT); pinMode(PIN_D2, INPUT); pinMode(PIN_C6, OUTPUT); pinMode(PIN_B6, OUTPUT); analogWrite(pinM1for, 0); analogWrite(pinM1rev, 0); analogWrite(pinM2for, 0); analogWrite(pinM2rev, 0); pinMode(pinM1en, OUTPUT); pinMode(pinM2en, OUTPUT); digitalWrite(pinM1en, HIGH); digitalWrite(pinM2en, HIGH); digitalWrite(power,HIGH); pinMode(11, OUTPUT); Serial.begin(9600); drive = false; distance = 1; //**Last updated to 7 for(int i = 0; i < 8; i++){ detect[i] = 0; lock[i] = 0; } } void loop(){ digitalWrite(11, HIGH); if(drive){ mov(1); } else{ analogWrite(PIN_B6,0); analogWrite(pinM1rev, 0); analogWrite(pinM2rev, 0); analogWrite(pinM1for, 0); analogWrite(pinM2for, 0); } //***Last updated to 7 //Timer1.pwm(PIN_C6, 512); val[0] = digitalRead(PIN_B0); val[1] = digitalRead(PIN_B1); val[2] = digitalRead(PIN_B2); val[3] = digitalRead(PIN_B3); val[4] = digitalRead(PIN_F1); val[5] = digitalRead(PIN_F4); val[6] = digitalRead(PIN_D1); val[7] = digitalRead(PIN_D2); int flee = -1; for(int i = 0; i < 8; i++){ if(val[i] == LOW){ detect[i] = 1; } else{ if(detect[i] > 0){ detect[i] = detect[i] - 1; } else{ detect[i] = 0; } } if(detect[i] > 0 && lock[i] != 1){ lock[i] = 1; } else if(detect[i] <= 0){ lock[i] = 0; } } int detections = 0; int base = -1; int factor = -1; for(int i = 0; i < 8; i++){ if(detect[i] > 0){ analogWrite(PIN_B6,128); if(detections == 0){ Serial.print("DETECTION(S): "); base = i; flee = (i + 4) % 8; detections = detections + 1; } Serial.print(i); Serial.print(", "); if((base+8-i) < (i - base)){ factor = i - base+8; } else{ factor = i - base; } flee = round(flee + factor/2); } } if(detections > 0){ flee = flee % 8; switch(flee){ case -1: break; case 0: drive = true; distance = 1; //no turn. Serial.print(" STRAIGHT"); break; case 1: drive = true; distance = 1; //turn 45 degrees turn(45); Serial.print(" STRAIGHT-RIGHT"); break; case 2: drive = true; distance = 1; //turn 90 degrees turn(90); Serial.print(" RIGHT"); break; case 3: drive = true; distance = -1; turn(135); //turn -45 degrees Serial.print(" BACK-RIGHT"); break; case 4: drive = true; distance = -1; turn(180); Serial.print(" REVERSE"); break; case 5: drive = true; distance = -1; turn(-135); Serial.print(" BACK-LEFT"); break; case 6: drive = true; distance = 1; turn(-90); Serial.print(" LEFT"); break; case 7: drive = true; distance = 1; turn(-45); Serial.print(" STRAIGHT-LEFT"); break; default: Serial.print(flee); Serial.print(" ERROR WHAT DID YOU DO??"); } Serial.println(""); } else{ drive = false; } flee = -1; //digitalWrite(PIN_C6, HIGH); //delayMicroseconds(14); //digitalWrite(PIN_C6, LOW); //delayMicroseconds(0); } //Move Distance and Turn Angle Functions: void mov(int dist) { //Move Function (Units of dist = in) if (dist < 0) { movrev(pwmspd, (abs(dist)*rate)); } if (dist > 0) { movfor(pwmspd, (dist*rate)); } } void turn (int angle) { //Turn Function (Units of angle = deg) if (angle < 0) { turnCCW(pwmspd, (abs(angle)*turnrate)); } if (angle > 0) { turnCW(pwmspd, angle*turnrate); } } //Motor Commands: void movfor(int spd,unsigned int dur) { //Motor 1&2 Forward beep = 0; analogWrite(pinM1rev, 0); analogWrite(pinM2rev, 0); analogWrite(pinM1for, spd); analogWrite(pinM2for, spd); while(beep < dur*4/1000){ analogWrite(PIN_B6, 128); beep = beep + 1; delay(250); analogWrite(PIN_B6, 0); beep = beep +1; delay(250); } analogWrite(pinM1for, 0); analogWrite(pinM2for, 0); } void movrev(int spd,unsigned int dur) { //Motor 1&2 Reverse beep = 0; analogWrite(pinM1for, 0); analogWrite(pinM2for, 0); analogWrite(pinM1rev, spd); analogWrite(pinM2rev, spd); while(beep < dur*4/1000){ analogWrite(PIN_B6, 128); beep = beep + 1; delay(250); analogWrite(PIN_B6, 0); beep = beep +1; delay(250); } analogWrite(pinM1rev, 0); analogWrite(pinM2rev, 0); } void turnCW(int spd,unsigned int dur) { //Turn Clockwise beep = 0; analogWrite(pinM1rev, 0); analogWrite(pinM2for, 0); analogWrite(pinM1for, spd); analogWrite(pinM2rev, spd); while(beep < (dur/1000)*4){ analogWrite(PIN_B6, 128); beep = beep + 1; delay(250); analogWrite(PIN_B6, 0); beep = beep + 1; delay(250); } analogWrite(pinM1for, 0); analogWrite(pinM2rev, 0); } void turnCCW(int spd,unsigned int dur) { //Turn CounterClockwise beep = 0; analogWrite(pinM1for, 0); analogWrite(pinM2rev, 0); analogWrite(pinM1rev, spd); analogWrite(pinM2for, spd); while(beep < dur*4/1000){ analogWrite(PIN_B6, 128); beep = beep + 1; delay(250); analogWrite(PIN_B6, 0); beep = beep + 1; delay(250); } analogWrite(pinM1rev, 0); analogWrite(pinM2for, 0); }