Close

Arduino code

A project log for Dynamic Interactivity G2: UN_SDG

A group assignment to promote the United Nations Sustainable Development Goals with the use of digital fabrication and physical computing.

tan-yong-jieTan Yong Jie 08/29/2018 at 05:180 Comments
#include <Adafruit_NeoPixel.h>

#define trigPin01 12
#define echoPin01 13

#define trigPin02 10
#define echoPin02 11

#define trigPin03 8
#define echoPin03 9

#define trigPin04 6
#define echoPin04 7

#define trigPin05 4
#define echoPin05 5

#define vibrationPin06 3

#define vibrationPin07 2

unsigned char stateInterrupt1 = 0;  // pin 3
unsigned char stateInterrupt0 = 0;  // pin 2

#define LED01 A5
#define NUM_LED01 176
Adafruit_NeoPixel strip01 = Adafruit_NeoPixel(NUM_LED01, LED01, NEO_GRB + NEO_KHZ800);


void setup() {

  Serial.begin(9600);

  pinMode(trigPin01, OUTPUT);
  pinMode(echoPin01, INPUT);
  pinMode(LED01, OUTPUT);

  pinMode(trigPin02, OUTPUT);
  pinMode(echoPin02, INPUT);

  pinMode(trigPin03, OUTPUT);
  pinMode(echoPin03, INPUT);

  pinMode(trigPin04, OUTPUT);
  pinMode(echoPin04, INPUT);

  pinMode(trigPin05, OUTPUT);
  pinMode(echoPin05, INPUT);

  pinMode(vibrationPin06, INPUT);

  pinMode(vibrationPin07, INPUT);

  attachInterrupt(1, interrupt1, FALLING);
  attachInterrupt(0, interrupt0, FALLING);

  strip01.begin();
  strip01.show();

}

void loop() {

  byte incomingByte = '?';

  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.println(incomingByte);
  }

  switch (incomingByte) {

    case 62: //this should be equivalent to ASCII ">"
      turnAllRed();
      break;

    case 60: //this should be equivalent to ASCII "<"
      turnAllOff();
      break;

    case 43: //this should be equivalent to ASCII "+"
      stateInterrupt1 = 0;
      stateInterrupt0 = 0;
      break;

    case 49: //this should be equivalent to ASCII "1"
      LEDTurnGreen01();
      delay(500);
      while (checkVSensor06() == false) {
        delay(10); // Sensor is slow in returning response, so add a little delay
      }
      break;

    case 50: //this should be equivalent to ASCII "2"
      LEDTurnGreen02();
      delay(500);
      while (checkVSensor07() == false) {
        delay(10); // Sensor is slow in returning response, so add a little delay
      }
      break;

    case 51: //this should be equivalent to ASCII "3"
      LEDTurnGreen03();
      delay(500);
      while (checkSensor01() == false) {
      }
      break;

    case 52: //this should be equivalent to ASCII "4"
      LEDTurnGreen04();
      delay(500);
      while (checkSensor02() == false) {
      }
      break;

    case 53: //this should be equivalent to ASCII "5"
      LEDTurnGreen05();
      delay(500);
      while (checkSensor03() == false) {
      }
      break;

    case 54: //this should be equivalent to ASCII "6"
      LEDTurnGreen06();
      delay(500);
      while (checkSensor04() == false) {
      }
      break;

    case 55: //this should be equivalent to ASCII "7"
      LEDTurnGreen07();
      delay(500);
      while (checkSensor05() == false) {
      }
      break;
  }
}


void interrupt1() {
  stateInterrupt1++;
}

void interrupt0() {
  stateInterrupt0++;
}

boolean checkSensor01() {

  float duration01, distance01;

  digitalWrite(trigPin01, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin01, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin01, LOW);

  duration01 = pulseIn(echoPin01, HIGH, 1000000);
  distance01 = (duration01 / 2) * 0.0344;

  if (distance01 >= 40 ) {
    Serial.print('c');
    turnAllRed();
    return true;
  }
  else {
    return false;
  }

}
boolean checkSensor02() {

  float duration02, distance02;

  digitalWrite(trigPin02, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin02, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin02, LOW);

  duration02 = pulseIn(echoPin02, HIGH);
  distance02 = (duration02 / 2) * 0.0344;

  if (distance02 >= 40) {
    Serial.print('d');
    turnAllRed();
    return true;
  }
  else {
    return false;
  }
}

boolean checkSensor03() {

  float duration03, distance03;

  digitalWrite(trigPin03, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin03, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin03, LOW);

  duration03 = pulseIn(echoPin03, HIGH);
  distance03 = (duration03 / 2) * 0.0344;

  if (distance03 >= 40) {
    Serial.print('e');
    turnAllRed();
    return true;
  }
  else {
    return false;
  }
}


boolean checkSensor04() {

  float duration04, distance04;

  digitalWrite(trigPin04, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin04, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin04, LOW);

  duration04 = pulseIn(echoPin04, HIGH);
  distance04 = (duration04 / 2) * 0.0344;

  if (distance04 <= 90) {
    Serial.print('f');
    turnAllRed();
    return true;
  }
  else {
    return false;
  }
}

boolean checkSensor05() {

  float duration05, distance05;

  digitalWrite(trigPin05, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin05, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin05, LOW);

  duration05 = pulseIn(echoPin05, HIGH);
  distance05 = (duration05 / 2) * 0.0344;

  if (distance05 >= 40) {
    Serial.print('g');
    turnAllRed();
    return true;
  }
  else {
    return false;
  }
}

boolean checkVSensor06() {
  if (stateInterrupt1 != 0) {
    stateInterrupt1 = 0;
    turnAllRed();
    Serial.print('a');
    return true;
    //    delay(500);
  }
  else
    return false;
}

boolean checkVSensor07() {
  if (stateInterrupt0 != 0) {
    stateInterrupt0 = 0;
    turnAllRed();
    Serial.print('b');
    return true;
    //    delay(500);
  }
  else
    return false;
}

void LEDTurnGreen01() {

  for (int pixID = 0; pixID < 14; pixID++) {
    strip01.setPixelColor(pixID, 0, 255, 0);
    strip01.show();
  }


}

void LEDTurnRed01() {

  for (int pixID = 0; pixID < 14; pixID++) {
    strip01.setPixelColor(pixID, 255, 0, 0);
    strip01.show();
  }


}


void LEDTurnOff01() {

  for (int pixID = 0; pixID < 14; pixID++) {
    strip01.setPixelColor(pixID, 0, 0, 0);
    strip01.show();
  }


}

void LEDTurnGreen02() {

  for (int pixID = 14; pixID < 38; pixID++) {
    strip01.setPixelColor(pixID, 0, 255, 0);
    strip01.show();
  }


}

void LEDTurnRed02() {

  for (int pixID = 14; pixID < 38; pixID++) {
    strip01.setPixelColor(pixID, 255, 0, 0);
    strip01.show();
  }


}

void LEDTurnOff02() {

  for (int pixID = 14; pixID < 38; pixID++) {
    strip01.setPixelColor(pixID, 0, 0, 0);
    strip01.show();
  }


}

void LEDTurnGreen03() {

  for (int pixID = 38; pixID < 47; pixID++) {
    strip01.setPixelColor(pixID, 0, 255, 0);
    strip01.show();
  }


}

void LEDTurnRed03() {

  for (int pixID = 38; pixID < 47; pixID++) {
    strip01.setPixelColor(pixID, 255, 0, 0);
    strip01.show();
  }


}

void LEDTurnOff03() {

  for (int pixID = 38; pixID < 47; pixID++) {
    strip01.setPixelColor(pixID, 0, 0, 0);
    strip01.show();
  }


}

void LEDTurnGreen04() {

  for (int pixID = 47; pixID < 55; pixID++) {
    strip01.setPixelColor(pixID, 0, 255, 0);
    strip01.show();
  }


}

void LEDTurnRed04() {

  for (int pixID = 47; pixID < 55; pixID++) {
    strip01.setPixelColor(pixID, 255, 0, 0);
    strip01.show();
  }


}

void LEDTurnOff04() {

  for (int pixID = 47; pixID < 55; pixID++) {
    strip01.setPixelColor(pixID, 0, 0, 0);
    strip01.show();
  }


}

void LEDTurnGreen05() {

  for (int pixID = 55; pixID < 70; pixID++) {
    strip01.setPixelColor(pixID, 0, 255, 0);
    strip01.show();
  }


}

void LEDTurnRed05() {

  for (int pixID = 55; pixID < 70; pixID++) {
    strip01.setPixelColor(pixID, 150, 0, 0);
    strip01.show();
  }


}

void LEDTurnOff05() {

  for (int pixID = 55; pixID < 70; pixID++) {
    strip01.setPixelColor(pixID, 0, 0, 0);
    strip01.show();
  }


}

void LEDTurnGreen06() {

  for (int pixID = 70; pixID < 113; pixID++) {
    strip01.setPixelColor(pixID, 0, 255, 0);
    strip01.show();
  }


}

void LEDTurnRed06() {

  for (int pixID = 70; pixID < 113; pixID++) {
    strip01.setPixelColor(pixID, 255, 0, 0);
    strip01.show();
  }


}

void LEDTurnOff06() {

  for (int pixID = 70; pixID < 113; pixID++) {
    strip01.setPixelColor(pixID, 0, 0, 0);
    strip01.show();
  }


}

void LEDTurnGreen07() {

  for (int pixID = 114; pixID < 175; pixID++) {
    strip01.setPixelColor(pixID, 0, 255, 0);
    strip01.show();
  }



}

void LEDTurnRed07() {

  for (int pixID = 114; pixID < 175; pixID++) {
    strip01.setPixelColor(pixID, 255, 0, 0);
    strip01.show();
  }


}

void LEDTurnOff07() {

  for (int pixID = 114; pixID < 175; pixID++) {
    strip01.setPixelColor(pixID, 0, 0, 0);
    strip01.show();
  }


}

void turnAllRed() {

  LEDTurnRed01();
  LEDTurnRed02();
  LEDTurnRed03();
  LEDTurnRed04();
  LEDTurnRed05();
  LEDTurnRed06();
  LEDTurnRed07();

}

void turnAllOff() {

  LEDTurnOff01();
  LEDTurnOff02();
  LEDTurnOff03();
  LEDTurnOff04();
  LEDTurnOff05();
  LEDTurnOff06();
  LEDTurnOff07();

}
 

Discussions