/*=============================================
Program : Vertical External Pole Climbing Robot
Author : wickPro-PC\wickPro
Time : 15:56 PM
Date : 10/11/2016
Coded by: Pramod C Wickramasinghe
E-mail : wick.pro@gmail.com
Mobile : +94774585875
=============================================*/
#include <IRremote.h>
int WR1 = 22;
int WF1 = 23;
int WS1 = 4;
int WR2 = 26;
int WF2 = 27;
int WS2 = 6;
int WR3 = 30;
int WF3 = 31;
int WS3 = 8;
int RWF = 28;
int RWR = 29;
int RWS = 7;
int CUT = 5;
int CUT1 = 24;
int CUT2 = 25;
int ALS = A1;
int ALS_THRESH = 1023;
int UDT = A12; // Ultrasonic Sensor DOWN.
int UUT = 34; // Ultrasonic Sensor UP.
int UDE = A13;
int UUE = 36;
long distanceUp = 0;
long distanceDown = 0;
int STATE_START = 0;
int STATE_UP = 1;
int STATE_DOWN = 2;
int STATE_STOP = 3;
int state = 0;
int SW1 = 40; // Switches
int SW2 = 42;
int SW3 = 44;
int ROT = 38;
int ROT_DIRECTION = 1; // 1 or -1
int ROT_DIRECTION_PREV = -1; // 1 or -1
long ROT_DEBTIME = -500;
long ROT_DEBDELAY = 500;
int rLED = 41;
int yLED = 43;
int gLED = 45;
int RECV_PIN = 49;
int rledState = HIGH;
long rpreviousMillis = 0;
long rinterval = 500;
int yledState = HIGH;
long ypreviousMillis = 0;
long yinterval = 500;
int gledState = HIGH;
long gpreviousMillis = 0;
long ginterval = 500;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
pinMode(SW3, INPUT);
pinMode(rLED, OUTPUT);
pinMode(yLED, OUTPUT);
pinMode(gLED, OUTPUT);
pinMode(WS1, OUTPUT);
pinMode(WF1, OUTPUT);
pinMode(WR1, OUTPUT);
pinMode(WS2, OUTPUT);
pinMode(WF2, OUTPUT);
pinMode(WR2, OUTPUT);
pinMode(WS3, OUTPUT);
pinMode(WF3, OUTPUT);
pinMode(WR3, OUTPUT);
pinMode(RWS, OUTPUT);
pinMode(RWF, OUTPUT);
pinMode(RWR, OUTPUT);
pinMode(CUT, OUTPUT);
pinMode(CUT1, OUTPUT);
pinMode(CUT2, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
Serial.begin(9600);
digitalWrite(rLED,HIGH);
Serial.println("Hello World");
}
// -------------------
void loop() {
resetSystem();
while (digitalRead(SW1) == HIGH) {
unsigned long rcurrentMillis = millis();
digitalWrite(gLED, LOW);
digitalWrite(yLED, LOW);
while (digitalRead(ROT)==0) {
if (ROT_DIRECTION==1) {
analogWrite(RWS, 75);
digitalWrite(RWR, LOW);
digitalWrite(RWF, HIGH);
delay(100);
} else {
analogWrite(RWS, 75);
digitalWrite(RWR, HIGH);
digitalWrite(RWF, LOW);
delay(100);
}
if (digitalRead(ROT)==1) {
analogWrite(RWS, 0);
digitalWrite(RWF, LOW);
digitalWrite(RWR, LOW);
break;
}
}
if(rcurrentMillis - rpreviousMillis > rinterval) {
rpreviousMillis = rcurrentMillis;
if (rledState == LOW)
rledState = HIGH;
else
rledState = LOW;
digitalWrite(rLED, rledState);
} else if (digitalRead(SW1) == LOW) {
digitalWrite(rLED, HIGH);
}
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xFF629D: // Serial.println(" FORWARD"); break;
delay(100);
analogWrite(WS1, 200);
digitalWrite(WF1, LOW);
digitalWrite(WR1, HIGH);
analogWrite(WS2, 100);
digitalWrite(WF2, LOW);
digitalWrite(WR2, HIGH);
analogWrite(WS3, 100);
digitalWrite(WF3, LOW);
digitalWrite(WR3, HIGH);
break;
case 0xFFA857: //Serial.println(" REVERSE"); break;
delay(100);
analogWrite(WS1, 70);
digitalWrite(WR1, LOW);
digitalWrite(WF1, HIGH);
analogWrite(WS2, 90);
digitalWrite(WR2, LOW);
digitalWrite(WF2, HIGH);
analogWrite(WS3, 90);
digitalWrite(WR3, LOW);
digitalWrite(WF3, HIGH);
break;
case 0xFF22DD: //Serial.println(" LEFT");
ROT_DIRECTION = 1;
delay(100);
analogWrite(RWS, 75);
digitalWrite(RWR, HIGH);
digitalWrite(RWF, LOW);
break;
case 0xFFC23D: //Serial.println(" RIGHT");
ROT_DIRECTION = -1;
delay(100);
analogWrite(RWS, 75);
digitalWrite(RWR, LOW);
digitalWrite(RWF, HIGH);
break;
case 0xFF02FD: //Serial.println(" -OK-");
delay(3);
analogWrite(WS1, 0);
digitalWrite(WF1, LOW);
digitalWrite(WR1, LOW);
analogWrite(WS2, 0);
digitalWrite(WF2, LOW);
digitalWrite(WR2, LOW);
analogWrite(WS3, 0);
digitalWrite(WF3, LOW);
digitalWrite(WR3, LOW);
analogWrite(RWS, 0);
digitalWrite(RWR, LOW);
digitalWrite(RWF, LOW);
break;
case 0xFF6897: //Serial.println(" 1");
analogWrite(WS1, 100);
digitalWrite(WF1, LOW);
digitalWrite(WR1, HIGH);
delay(400);
analogWrite(WS1, 0);
digitalWrite(WF1, LOW);
digitalWrite(WR1, LOW);
break;
case 0xFF9867: //Serial.println(" 2");
analogWrite(WS2, 100);
digitalWrite(WF2, LOW);
digitalWrite(WR2, HIGH);
delay(400);
analogWrite(WS2, 0);
digitalWrite(WF2, LOW);
digitalWrite(WR2, LOW);
break;
case 0xFFB04F: //Serial.println(" 3");
analogWrite(WS3, 100);
digitalWrite(WF3, LOW);
digitalWrite(WR3, HIGH);
delay(400);
analogWrite(WS3, 0);
digitalWrite(WF3, LOW);
digitalWrite(WR3, LOW);
break;
case 0xFF30CF: //Serial.println(" 4");
analogWrite(WS1, 60);
digitalWrite(WR1, LOW);
digitalWrite(WF1, HIGH);
delay(400);
analogWrite(WS1, 0);
digitalWrite(WF1, LOW);
digitalWrite(WR1, LOW);
break;
case 0xFF18E7: //Serial.println(" 5");
analogWrite(WS2, 60);
digitalWrite(WR2, LOW);
digitalWrite(WF2, HIGH);
delay(400);
analogWrite(WS2, 0);
digitalWrite(WF2, LOW);
digitalWrite(WR2, LOW);
break;
case 0xFF7A85: //Serial.println(" 6");
analogWrite(WS3, 60);
digitalWrite(WR3, LOW);
digitalWrite(WF3, HIGH);
delay(400);
analogWrite(WS3, 0);
digitalWrite(WF3, LOW);
digitalWrite(WR3, LOW);
break;
case 0xFF42BD:// Serial.println(" *");
ROT_DIRECTION = 1;
analogWrite(RWS, 75);
digitalWrite(RWR, HIGH);
digitalWrite(RWF, LOW);
delay(300);
analogWrite(RWS, 0);
digitalWrite(RWR, LOW);
digitalWrite(RWF, LOW);
break;
case 0xFF52AD:// Serial.println(" #");
ROT_DIRECTION = -1;
analogWrite(RWS, 75);
digitalWrite(RWR, LOW);
digitalWrite(RWF, HIGH);
delay(300);
analogWrite(RWS, 0);
digitalWrite(RWR, LOW);
digitalWrite(RWF, LOW);
break;
}
irrecv.resume();
}
} //end-while
resetSystem();
while (digitalRead(SW1) == LOW) {
unsigned long ycurrentMillis = millis();
unsigned long gcurrentMillis = millis();
if (digitalRead(SW2)==HIGH) {
if(ycurrentMillis - ypreviousMillis > yinterval) {
ypreviousMillis = ycurrentMillis;
yledState = (yledState == LOW) ? HIGH : LOW;
digitalWrite(yLED, yledState);
}
} else {
digitalWrite(yLED, HIGH);
}
if (digitalRead(SW3)==HIGH) {
if(gcurrentMillis - gpreviousMillis > ginterval) {
gpreviousMillis = gcurrentMillis;
gledState = (gledState == LOW) ? HIGH : LOW;
digitalWrite(gLED, gledState);
}
} else {
digitalWrite(gLED, HIGH);
}
// -----------------------------------------------------------------------
readDistance();
readRotationStatus();
if (state == STATE_START){
Serial.println("[] STATE_START");
if(goDown() == 1){
Serial.println("[] Going down (start)");
} else {
state = STATE_UP;
startCutters();
}
} else if(state == STATE_UP){
Serial.println("[] STATE_UP");
if(goUp() == 1) {
Serial.println("[] Going up");
if (ROT_DIRECTION != ROT_DIRECTION_PREV) {
ROT_DIRECTION_PREV = ROT_DIRECTION;
ROT_DEBTIME = millis();
if (ROT_DIRECTION == 1) {
analogWrite(RWS, 75);
digitalWrite(RWR, HIGH);
digitalWrite(RWF, LOW);
} else {
analogWrite(RWS, 75);
digitalWrite(RWR, LOW);
digitalWrite(RWF, HIGH);
}
}
if(analogRead(ALS) > ALS_THRESH) {
delay(100);
Serial.println("[] Going back (Freezed)");
goDown();
delay(750);
stopMotors();
delay(100);
goUp();
}
} else {
state = STATE_DOWN;
stopRotate();
stopCutters();
ROT_DIRECTION_PREV = ROT_DIRECTION * -1;
}
} else if(state == STATE_DOWN){
Serial.println("[] STATE_DOWN");
if(goDown() == 1) {
Serial.println("[] Going down");
if (digitalRead(SW3)==HIGH) {
Serial.println("[] Painting");
if (ROT_DIRECTION != ROT_DIRECTION_PREV) {
ROT_DIRECTION_PREV = ROT_DIRECTION;
ROT_DEBTIME = millis();
if (ROT_DIRECTION == 1) {
analogWrite(RWS, 75);
digitalWrite(RWR, HIGH);
digitalWrite(RWF, LOW);
} else {
analogWrite(RWS, 75);
digitalWrite(RWR, LOW);
digitalWrite(RWF, HIGH);
}
}
// Start Painter
analogWrite(9, 254);
digitalWrite(32, LOW);
digitalWrite(33, HIGH);
}
} else {
state = STATE_STOP;
stopRotate();
// Stop Painter
analogWrite(9, 254);
digitalWrite(32, HIGH);
digitalWrite(33, LOW);
}
} else if(state == STATE_STOP){
Serial.println("[] STATE_STOP");
stopMotors();
}
}
}
void resetSystem() {
stopCutters();
stopMotors();
stopRotate();
state = 0;
ROT_DIRECTION = 1;
ROT_DIRECTION_PREV = -1;
}
void startCutters() {
analogWrite(CUT, 110);
digitalWrite(CUT1,HIGH);
digitalWrite(CUT2,LOW);
}
void stopCutters() {
analogWrite(CUT, 0);
digitalWrite(CUT1,LOW);
digitalWrite(CUT2,LOW);
}
int flag = 0;
int rotationDirection = 0;
void stopRotate() {
analogWrite(RWS, 0);
digitalWrite(RWR, LOW);
digitalWrite(RWF, LOW);
}
int goDown() {
if( distanceDown > 20 ) {
analogWrite(WS1, 50);
digitalWrite(WR1, LOW);
digitalWrite(WF1, HIGH);
analogWrite(WS2, 76);
digitalWrite(WR2, LOW);
digitalWrite(WF2, HIGH);
analogWrite(WS3, 76);
digitalWrite(WR3, LOW);
digitalWrite(WF3, HIGH);
return 1;
} else {
stopMotors();
delay(100);
return 0;
}
}
int goUp() {
if (
(digitalRead(SW2)==LOW && distanceUp > 12) ||
(digitalRead(SW2)==HIGH && distanceUp < 12) ) {
// if( distanceUp > 12 ) {
analogWrite(WS1, 170);
digitalWrite(WF1, LOW);
digitalWrite(WR1, HIGH);
analogWrite(WS2, 80);
digitalWrite(WF2, LOW);
digitalWrite(WR2, HIGH);
analogWrite(WS3, 80);
digitalWrite(WF3, LOW);
digitalWrite(WR3, HIGH);
return 1;
} else {
stopMotors();
delay(100);
return 0;
}
}
void stopMotors() {
analogWrite(WS1, 0);
digitalWrite(WF1, LOW);
digitalWrite(WR1, LOW);
analogWrite(WS2, 0);
digitalWrite(WF2, LOW);
digitalWrite(WR2, LOW);
analogWrite(WS3, 0);
digitalWrite(WF3, LOW);
digitalWrite(WR3, LOW);
}
void readDistance() {
// Up
pinMode(UUT, OUTPUT);
digitalWrite(UUT, LOW);
delayMicroseconds(2);
digitalWrite(UUT, HIGH);
delayMicroseconds(10);
digitalWrite(UUT, LOW);
pinMode(UUE, INPUT);
distanceUp = pulseIn(UUE, HIGH) / 29 / 2;
// Down
pinMode(UDT, OUTPUT);
digitalWrite(UDT, LOW);
delayMicroseconds(2);
digitalWrite(UDT, HIGH);
delayMicroseconds(10);
digitalWrite(UDT, LOW);
pinMode(UDE, INPUT);
distanceDown = pulseIn(UDE, HIGH) / 29 / 2;
// Print
Serial.println(distanceUp);
Serial.println(distanceDown);
Serial.println();
}
void readRotationStatus() {
long currentTime = millis();
long nextReadTime = ROT_DEBDELAY + ROT_DEBTIME;
if (nextReadTime < currentTime) {
if (digitalRead(ROT) == 0) {
ROT_DIRECTION = -1 * ROT_DIRECTION;
stopRotate();
}
}
}
Comments