LED/Web Mood Inferface

For this project I used node.js to communicate between a website and an Arduino. I use a physical button, a potentiometer, and a button on a website to change the output on an LED matrix.

  • The green button on the website changes the output to happy.
  • The physical button changes the output to sad.
  • Turning the dial of the potentiometer will transition the output from sad to neutral to happy.

Code

The code is based on an example we did in CCLab last semester where we exchanged values between a website and a button and potentiometer connected to an Arduino. Here I am using those values to control the output on an LED matrix. I also used the bicolor8x8 example from Adafruit. There are several files. I have only included the Arduino code below. If you would like to see the entire code message me and I can send it to you (WordPress does not allow me to share .zip files).

Arduino code

/*Control an LED matrix from a server using node.js
Designed specifically to work with the Adafruit LED Matrix backpacks
—-> http://www.adafruit.com/products/872
—-> http://www.adafruit.com/products/871
—-> http://www.adafruit.com/products/870
*/

//include the following for the matrix
#include <Wire.h>
#include “Adafruit_LEDBackpack.h”
#include “Adafruit_GFX.h”

Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();

const int ledPin = 13;

int potPin = 0;
int sensorVal = 0;
int prevVal = 0;
String inputString = “”;

//button variables
int buttonPin=2;
int buttonState=0; //var for reading the button statis, this changes
int prevButton=0;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);

//for the matrix
matrix.begin(0x70); // pass in the address
}
//make some things for the LED matrix to draw
static const uint8_t PROGMEM
smile_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10100101,
B10011001,
B01000010,
B00111100 },
neutral_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10111101,
B10000001,
B01000010,
B00111100 },
frown_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10011001,
B10100101,
B01000010,
B00111100 };
void loop()
{
//LED
if(Serial.available() > 0){
int incomingByte = Serial.read();
if (incomingByte == 1) {
digitalWrite(ledPin, HIGH);

//draw something
matrix.clear();
matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_GREEN);
matrix.writeDisplay();
delay(500);
} else if (incomingByte == 0) {
digitalWrite (ledPin, LOW);
matrix.clear();
}
}
//Pot
//When sending values keep serial monitor closed because otherwise it will go there instead of server
sensorVal = analogRead(potPin);
if(prevVal != sensorVal){
Serial.print(‘B’);
Serial.print(sensorVal);
Serial.print(‘E’);
prevVal = sensorVal;
if (sensorVal < 400){
matrix.clear();
matrix.drawBitmap(0, 0, frown_bmp, 8, 8, LED_YELLOW);
matrix.writeDisplay();
delay(500);
} else if (sensorVal > 800){
matrix.clear();
matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_YELLOW);
matrix.writeDisplay();
delay(500);
} else {
matrix.clear();
matrix.drawBitmap(0, 0, neutral_bmp, 8, 8, LED_YELLOW);
matrix.writeDisplay();
delay(500);
}

}
//button
//When sending values keep serial monitor closed because otherwise it will go there instead of server
buttonState = digitalRead(buttonPin);
Serial.print(‘J’);
Serial.print(buttonState);
if (buttonState == HIGH){
matrix.clear();
matrix.drawBitmap(0, 0, frown_bmp, 8, 8, LED_RED);
matrix.writeDisplay();
delay(500);
} else{
matrix.clear();
}
Serial.print(‘K’);

//delay
delay(500);

}

 

Advertisements

One thought on “LED/Web Mood Inferface

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s