Final Project -- Save the World!

Final Project -- Save the World!

I used scratch, micro bit, compass sensor, Arduino, distance sensor, neopixel, servo, and Processing program and also some handcraft to tell the whole story! I also tried S4A, Makey Makey, power switch and other kinds of stuff as experience, though at last, I didn't use them.

Every time when I made something done, I just wanted to add something new and challenged myself again. And finally, I'm happy to expose myself to C language and Java as a freshman.

Besides the intangible tangible, I'm happy I did much more than this. ; )

Please contact me to see the live version, this video is rough and not enough to show my results!









Scratch



Arduino Code:

#include <Servo.h>
#include <Adafruit_NeoPixel.h>

//define NeoPixel Pin and Number of LEDs
#define PIN 5
#define NUM_LEDS 30
#define TRACE_LENGTH 5

//create a NeoPixel strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

boolean story_end = false;

Servo myservo; // create servo object to control a servo

int pos = 68; // variable to store the servo position

// defines pins numbers
const int trigPin = 10;
const int echoPin = 11;
// defines variables
long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(pos);

    // start the strip and blank it out
  strip.begin();
  strip.show();

  Serial.begin(9600); // Starts the serial communication
}

void loop() {

  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034/2;
  // Prints the distance on the Serial Monitor


  if(distance > 2 && distance < 320){
//    Servo
    if(story_end){
      myservo.write(0);
    } else {
      myservo.write(pos+distance);
    }

//    light
    int position = int(distance/3-1);
    if (story_end){
      Serial.println(distance*10+1);
    } else {
      Serial.println(distance*10+0);
    }
 
    for(int i = 0; i < 35; i++){
      if (i < position){
        strip.setPixelColor(i, strip.Color(i*7, 8, 211));
      }else {
        strip.setPixelColor(i, strip.Color(0, 0, 0));
      }
      if (position == 1){
        story_end = true;
      }
    }
    strip.show();

    //send distance over the serial port
 
    //wait 100 milliseconds so we don't drive ourselves crazy
//    delay(100);
    delay(200);
  }
}

Processing Code:
import processing.serial.*;

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
float x, y;
float dim = 80.0;
int distance;
int pass_value;

void setup()
{

  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);

  size(3000, 1500);
  noStroke();
}


void draw()
{
  if ( myPort.available() > 0)
  {  // If data is available,
    val = trim(myPort.readStringUntil('\n'));         // read it and store it in val
    //println(val); //print it out in the console
 
    background(102, 102, 102);
    if (val != null && val != "" ) {
      pass_value = int(val);
      int tmp = int(int(pass_value)/10*10*1.2)-48;
      //println(tmp);
      if (tmp < 2000){
        x = tmp;
      }
    }
    //translate(x, height/2-dim/2);
    println(x);
    if (pass_value%10 == 0){
      fill(187, 34, 34);
    } else {
      fill(61, 145, 64);
    }
    rect(0, 100, x, 500);
  }
}

Comments

Popular Posts