The Switch

For this project, I wanted to do something simple in order to focus on my final project. So I came up with a simple concept of a switch replying via twitter to people who have touched it. When the switch is turned on, it lets you know. When it is turned off, it lets you know. I could see this project as being done much better with an animatronic object of some sort.

VIDEO:

SCREENSHOT:

switchTwitterScreen

SKETCHES:

switchSketch

FRITZING:

switch_bb

ARDUINO CODE:

//Arduino code for a switch
// It tells processing when it is turned on or off.
// Author: Matthew Kellogg
// Date: November 17, 2014
// Copyright 2014 Matthew Kellogg

//Keeps track of how long switch is in either position
// Used for debouncing
int onTime = 0;
int offTime = 0;

// Pin used for the switch
int switchPin = 2;

void setup(){
  Serial.begin(9600);
  pinMode(switchPin, INPUT);
}

void loop(){
  //write 'O' if switch is switched on after THRESH of off
  //write 'F' if switch is switched off after THRESH of on
  static const int DELAY = 100;
  static const int THRESH = 1000;
  bool on = digitalRead(switchPin);
  if (on){
    if (offTime > THRESH){
      Serial.write('O');
    }
    onTime+=DELAY;
    offTime = 0;
  }else{
    if (onTime > THRESH){
      Serial.write('F');
    }
    offTime+=DELAY;
    onTime = 0;
  }
  delay(DELAY);
}

PROCESSING:

import com.temboo.core.*;
import com.temboo.Library.Twitter.Tweets.*;
import processing.serial.*;

Serial myPort;

// Create a session using your Temboo account application details
TembooSession session = new TembooSession("kellogg92", "myFirstApp", "afd7abe943404e90b715b4b2aa9f681d");

void setup() {
  // connect the serial
  if (Serial.list().length > 0) {
    String portName = Serial.list()[0]; 
    myPort = new Serial(this, portName, 9600);
  }
}

void draw(){
  readSerial();
}

void onTurnedOn(){
  println("ON!!!");
  runStatusesUpdateChoreo("The switch said, \"Mmm, you've turned me on.\"");
}

void onTurnedOff(){
  println("OFF!!!");
  runStatusesUpdateChoreo("The switch said, \"Eww, you've turned me off.\"");
}

//Does a status update with str
void runStatusesUpdateChoreo(String str) {
  // Create the Choreo object using your Temboo session
  StatusesUpdate statusesUpdateChoreo = new StatusesUpdate(session);

  // Set credential
  statusesUpdateChoreo.setCredential("Twitter");

  // Set inputs
  statusesUpdateChoreo.setStatusUpdate(str);
  
  // Run the Choreo and store the results
  StatusesUpdateResultSet statusesUpdateResults = statusesUpdateChoreo.run();
  
  // Print results
  println(statusesUpdateResults.getResponse());
}

//Reads all the characters from serial input
void readSerial() {
  while (myPort!=null && myPort.available () > 0) {
    char aChar = (char) myPort.read();
    if (aChar == 'O') {
      onTurnedOn();
    } else if (aChar == 'F') {
      onTurnedOff();
    }
  }
}

Comments are closed.