Finals Update – Nathan

Arduino Board

Currently, I have wired up the Arduino with everything I want to include in my final. It includes:

  • An ultrasonic distance sensor
  • A piezo
  • A photoresistor
  • A button
  • A knob (potentiometer)

Here is a picture of the layout:


For code, I currently have coded in the conditions for when the piezo is making sounds, as well as how its sound is being made. I also have the distance sensor and photoresistor coded in as functions, but I have not fully integrated them with the system in general.

I haven’t gotten to the processing side of the code yet, but I am planning to make several boolean values on the Arduino side to represent the signals being sent to processing. For example, if there is a friendly ghost, I will simply send the boolean to processing. I think this approach simplifies communication between the platforms. In my opinion, the more complicated alternative would be to send the raw values of the sensors and buttons on the Arduino to processing.

Here is the code I have now:

#include "pitches.h"

int piezo = 2;
bool piezoState = false;
int trig = 3;
int echo = 4;
int button = 5;
int buttonState = 0;
int prevButton = 0;
//photoresistor A0;
//knob = A1;
long timer;
int timerlength = 200;

void setup() {
  Serial.begin (9600);
  timer = millis() + timerlength;
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(button, INPUT);

void loop() {
//  int knobValue = analogRead(A1);
////  Serial.println(knobValue);

  buttonState = digitalRead(button);

void Piezo() {
  if (piezoState == false){
    if (millis() > timer){
      tone(piezo, C5);
      timer = millis() + timerlength;
    else if (millis() == timer){
  if (buttonState == HIGH && prevButton == LOW){
    piezoState = true;

  if (piezoState == true){

  prevButton = buttonState;

void Brightness(){
  int brightness;
  brightness = analogRead(A0);

  return brightness;

void Distance() {
  int time;
  int distance;
  digitalWrite(trig, HIGH);
  digitalWrite(trig, LOW);
  time = pulseIn(echo, HIGH);
//  speed of sound = 343 m/s = 0.0343 cm/µs
//  distance = time * speed
  distance = time * 0.0343 / 2;
  Serial.println(" cm");

  return distance;


Leave a Reply