Things used in this project

Code

VEPCRo_CodeArduino
Vertical External Pole Climbing Robot_Arduino code_Still tuning the code
#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();
    }
  }
}

Credits

A300x8saqdmrw16wrtg6
Pramod C Wickramasinghe

The kind of person who turns ideas into projects, and projects into success.

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Flightduino 101
Intermediate
  • 54
  • 2

Full instructions

A drone project that uses the Neural Pattern Recognition Module and other aspects of the Curie Module to advantage for creating a cool drone

Solar Smart Greenhouse using Vertical Aquaponic Towers
Intermediate
  • 1,640
  • 27

Work in progress

Feed your family with 22 sqm greenhousing by combining permaculture, aquaponic vertical towers and IOT.

Drop of Life
Intermediate
  • 541
  • 16

Full instructions

A display to remind me when I can donate blood and save lives, powered by the Red Cross API and Particle.

Arduino 101 BLE App
Intermediate
  • 1,215
  • 16

Work in progress

I want to show you how to make an application and connect it with the Arduino 101 platform for Internet of things applications.

IoT Drone - Part 2 - Sensors
Intermediate
  • 208
  • 4

Work in progress

Part 2 incorperates a GPS receiver, 10 DOF IMU, and Ultrasonic distance sensor to be used for flight controls.

Bluetooth smart home
Intermediate
  • 174
  • 4

Full instructions

A concept showing smart home control and automation with Arduino 101, accompanied by a custom Android application.

ProjectsCommunitiesContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Respect project
Feedback