FINAL PROJECT: Phase 4 FINAL CODE

At long last, I've completed my Bug. I will walk you through the last of my coding work through this post.

Before retrieving my 3D print, I finished the last of my code. I swapped the white led for an RGB LED. This way, when you touch Bug's sensor, Bug will change color between red, green and blue. I've also coded BUG so that you CANNOT activate him unless it is dark, so you are supposed to play with Bug in the dark. I've also modified the tone that Bug plays upon touch.

Here is the code in action:



Here is my commented code:
 #include <MedianFilter.h> //https://github.com/daPhoosa/MedianFilter
#include <CapacitiveSensor.h> //Found in Arduino Library

MedianFilter test(20,0); //used to smooth data from touch sensor

CapacitiveSensor capSensor = CapacitiveSensor(4,2); //connect touch sensor to pin 4 and 2

int threshold = 50; //threshold for touch sensor
bool pass = false;

 //int ledPin = 13;
int photocellInput = 0; //photoresistor to pin 0
int soundPin = 9; //buzzer to pin 9
int bluepin = 10; //blue LED to pin 10
int greenpin = 11; //green LED to pin 11
int redpin = 12; //red LED to pin 12

int count = 0; //counter for color cycles

long sensorValue = 0; //current touch sensor reading
long pastSensorValue = 0; //past touch sensor reading

void setup()  {
//establishing outputs
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(soundPin, OUTPUT);
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
}


void loop()  {
photocellInput = map(analogRead(0),0,1023,0,254); //read light sensor and map to 0-255

     if(photocellInput < 25){
 //if light sensor reads low then turn on the BUG
      digitalWrite(redpin, HIGH);
      lightson(); //see function below
      }

      else {
//if light sensor reads high then shut BUG off
      digitalWrite(redpin, LOW);
      digitalWrite(greenpin, LOW);
      digitalWrite(bluepin, LOW);
      }
       
delay(10); //reading delay
}
   
void lightson() {
//first get touch sensor reading
sensorValue =
  capSensor.capacitiveSensor(30);
  test.in(sensorValue);
  sensorValue = test.out();

if(sensorValue != pastSensorValue){ //detects if the switch state has changed
if (sensorValue > threshold) { //if state has changed AND the sensor reads above threshold
    if( pass == false) {
      pass = true; //activate BUG
      count++; //determines what color the light will change to
     
        if(count == 1) { //if count is 1 turn on the green light
         digitalWrite(redpin, LOW);
         digitalWrite(greenpin, HIGH);
         digitalWrite(bluepin, LOW);
        }
     
         if(count == 2) { //if count is 2 turn on the blue light
         digitalWrite(redpin, LOW);
         digitalWrite(greenpin, LOW);
         digitalWrite(bluepin, HIGH);
        }
     
        if(count > 2) { //if count is more than 2 reset count and turn on the red light
        count=0;
        digitalWrite(redpin, HIGH);
        digitalWrite(greenpin, LOW);
        digitalWrite(bluepin, LOW);
        }
     //tone plays when BUG is activated
      tone(soundPin, 200, 10);
      tone(soundPin, 400, 10);
    }
  }
else {
      pass = false; //turn off BUG
      noTone(soundPin); //turn sound off
    }
pastSensorValue = sensorValue; //store current sensor value into past value for next test
}

}

Comments

Popular posts from this blog

PROJECT 1 (2.0!) : LOCK BOX (For 3/26/19)

Project one observations