Trash-Can Arduino Code :

//LED Matrix
//#include

//#define _DEBUG_DATA

//LED Matrix
//Define the "Normal" Colors
#define BLACK 0
#define RED 0xE0
#define GREEN 0x1C
#define BLUE 0x03
#define ORANGE RED|GREEN
#define MAGENTA RED|BLUE
#define TEAL BLUE|GREEN
#define WHITE (RED|GREEN|BLUE)-0xA0

//Define the SPI Pin Numbers
#define DATA_OUT_PIN 11//MOSI
#define DATA_IN_PIN 12//MISO
#define SPI_CLOCK_PIN 13//sclk
#define SLAVESELECT 10//cs

//PING Section
#define pingPin1 7
#define pingPin2 6

//myShiftOut Section
//Pin connected to ST_CP of 74HC595
#define LATCH_PIN 3
//Pin connected to SH_CP of 74HC595
#define CLOCK_PIN 4
////Pin connected to DS of 74HC595
#define DATA_PIN 5

//Switch
#define LID_SWITCH 9

//holders for information you're going to pass to shifting function
byte dataArray[6];

//char COLORS [10] = { RED, GREEN, BLUE, ORANGE, MAGENTA, TEAL, WHITE, ORANGE, RED, BLUE };
char G = GREEN;

//Define the variables we'll need later in the program
char config [2];
//char color_buffer [64];

//Define all the numbers
int color_buffer_mix[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int color_buffer_empty[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int color_buffer_first[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int color_buffer_second[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,G,0,0,G,0,0,0,0,G,0,0,G,0,0,0,0,G,0,0,G,0,0,0,0,0,G,G,0};
int color_buffer_7[] = {0,0,0,0,G,G,G,G,0,0,0,0,G,0,0,0,0,0,0,0,G,0,0,0,0,0,0,0,0,G,0,0,0,0,0,0,0,0,G,0,0,0,0,0,0,0,G,0,0,0,0,0,0,0,G,0,0,0,0,0,0,0,G,0};
int color_buffer_8[] = {0,0,0,0,0,G,G,0,0,0,0,0,G,0,0,G,0,0,0,0,G,0,0,G,0,0,0,0,0,G,G,0,0,0,0,0,G,0,0,G,0,0,0,0,G,0,0,G,0,0,0,0,G,0,0,G,0,0,0,0,0,G,G,0};
int color_buffer_9[] = {0,0,0,0,0,G,G,0,0,0,0,0,G,0,0,G,0,0,0,0,G,0,0,G,0,0,0,0,G,0,0,G,0,0,0,0,G,G,G,0,0,0,0,0,G,0,0,0,0,0,0,0,G,0,0,G,0,0,0,0,0,G,G,0};

// Storage
boolean blinkLeds = false;
boolean measure = true;
int trashAmount = 0;
int score = 99;

// Time storage for delaying
unsigned long gameTime = 0;
unsigned long sensorDelay = 1000;
unsigned long sensorTime = 0;
unsigned long blinkTime = 0;

//long int countTime = 0;

void setup() {

//Serial.println("hello");
//Serial.flush();

pinMode(LATCH_PIN, OUTPUT);

//SPI Bus setup
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
//Serial.println("hello");
//Serial.flush();
//Set the pin modes for the RGB matrix
pinMode(DATA_OUT_PIN, OUTPUT);
pinMode(DATA_IN_PIN, INPUT);
pinMode(SPI_CLOCK_PIN,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);

//Pinmode of the lid switch
pinMode(LID_SWITCH, INPUT);
//Serial.println("hello1");
//Make sure the RGB matrix is deactivated
digitalWrite(SLAVESELECT,HIGH);
//Data for the myShiftOut pins
dataArray[0] = 0x40; //01000000
dataArray[1] = 0x60; //01100000
dataArray[2] = 0x70; //01110000
dataArray[3] = 0x78; //01111000
dataArray[4] = 0x7C; //01111100
dataArray[5] = 0x7E; //01111110
//Serial.println("hello");

config[0] = '%';
config[1] = 1;

digitalWrite(SLAVESELECT, LOW);
//Send the config to the RGB Matrix
for(int CONF=0; CONF 120000)
{

#ifdef _DEBUG_DATA
Serial.print( "GameScore" );
#endif

Serial.print( score, BYTE );

score = 99;

int first = score/10;
int second = score%10;

// int randNumber = random(0, 10);
// changeColor(COLORS[randNumber], color_buffer_0);

printmat(first,second);

gameTime = millis();
//Serial.println("GameCycle of 1 min complete");
}

if(digitalRead(LID_SWITCH) )
{
if( measure)
{
if( (millis() - sensorTime) > sensorDelay )
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration1, inches1, duration2, inches2;
float cm1, cm2;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin1, OUTPUT);
digitalWrite(pingPin1, LOW);
delayMicroseconds(2);
digitalWrite(pingPin1, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin1, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin1, INPUT);
duration1 = pulseIn(pingPin1, HIGH);
// convert the time into a distance
inches1 = microsecondsToInches(duration1);
cm1 = microsecondsToCentimeters(duration1);

delay(50);

pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin2, INPUT);
duration2 = pulseIn(pingPin2, HIGH);
// convert the time into a distance
inches2 = microsecondsToInches(duration2);
cm2 = microsecondsToCentimeters(duration2);

#ifdef _DEBUG_DATA

Serial.println("--------------------------------");

Serial.print("Sensor 1 : ");
Serial.print(inches1);
Serial.print("in, ");
Serial.print(cm1);
Serial.print("cm");
Serial.println();

Serial.print("Sensor 2 : ");
Serial.print(inches2);
Serial.print("in, ");
Serial.print(cm2);
Serial.print("cm");
Serial.println();

Serial.print("AVERAGE : ");
Serial.print((inches1+inches2)/2.0);
Serial.print("in, ");
Serial.print((cm1+cm2)/2.0);
Serial.print("cm");
Serial.println();

Serial.println("--------------------------------");

Serial.println();

#endif

int previousAmount = trashAmount;
trashAmount = (cm1+cm2)/2;

int delta = trashAmount - previousAmount;

if( delta 1000 )
{

if( blinkLeds )
{
clearMatrix();
clearVolumeOutput();
blinkLeds = !blinkLeds;
}
else
{

int first = score/10;
int second = score%10;

// int randNumber = random(0, 10);
// changeColor(COLORS[randNumber], color_buffer_0);

printmat(first,second);

showVolumeOutput(trashAmount);
blinkLeds = !blinkLeds;
}

blinkTime = millis();
sensorTime = millis();
sensorDelay = 1000;
measure = true;
//Serial.write(65);
}
}
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
float microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2.0;
}

void clearVolumeOutput(){
//ground LATCH_PIN and hold low for as long as you are transmitting
digitalWrite(LATCH_PIN, 0);
//move 'em out
myShiftOut(DATA_PIN, CLOCK_PIN, 0);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(LATCH_PIN, 1);
delay(50);
}

void showVolumeOutput( byte volume ){

byte data;

if(volume < 9)
{
data = dataArray[5];
}
else if(volume < 16)
{
data = dataArray[4];
}
else if(volume < 23)
{
data = dataArray[3];
}
else if(volume < 30)
{
data = dataArray[2];
}
else if(volume < 37)
{
data = dataArray[1];
}
else if(volume =0; i--) {
digitalWrite(myCLOCK_PIN, 0);

//if the value passed to myDATA_OUT_PIN and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDATA_OUT_PIN & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}

//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDATA_PIN, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myCLOCK_PIN, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDATA_PIN, 0);
}

//stop shifting
digitalWrite(myCLOCK_PIN, 0);
}

//Prints data to the matrix
void printmat(int first,int second)
{
//Load colors into the first row color buffer array.
//This will be the array of colors sent to the RGB matrix.

delay(10);

//First Number
if(first == 0)
{
copyMatrix(color_buffer_0, color_buffer_first);
}
else if(first == 1)
{
copyMatrix(color_buffer_1, color_buffer_first);
}
else if(first == 2)
{
copyMatrix(color_buffer_2, color_buffer_first);
}
else if(first == 3)
{
copyMatrix(color_buffer_3, color_buffer_first);
}
else if(first == 4)
{
copyMatrix(color_buffer_4, color_buffer_first);
}
else if(first == 5)
{
copyMatrix(color_buffer_5, color_buffer_first);
}
else if(first == 6)
{
copyMatrix(color_buffer_6, color_buffer_first);
}
else if(first == 7)
{
copyMatrix(color_buffer_7, color_buffer_first);
}
else if(first == 8 )
{
copyMatrix(color_buffer_8, color_buffer_first);
}
else if(first == 9)
{
copyMatrix(color_buffer_9, color_buffer_first);
}

//Second Number
if(second == 0)
{
copyMatrix(color_buffer_0, color_buffer_second);
}
else if(second == 1)
{
copyMatrix(color_buffer_1, color_buffer_second);
}
else if(second == 2)
{
copyMatrix(color_buffer_2, color_buffer_second);
}
else if(second == 3)
{
copyMatrix(color_buffer_3, color_buffer_second);
}
else if(second == 4)
{
copyMatrix(color_buffer_4, color_buffer_second);
}
else if(second == 5)
{
copyMatrix(color_buffer_5, color_buffer_second);
}
else if(second == 6)
{
copyMatrix(color_buffer_6, color_buffer_second);
}
else if(second == 7)
{
copyMatrix(color_buffer_7, color_buffer_second);
}
else if(second == 8 )
{
copyMatrix(color_buffer_8, color_buffer_second);
}
else if(second == 9)
{
copyMatrix(color_buffer_9, color_buffer_second);
}

getMix(color_buffer_mix, color_buffer_first, color_buffer_second);

//Activate the RGB Matrix
digitalWrite(SLAVESELECT, LOW);
//Send the color buffer to the RGB Matrix
for(int LED=0; LED<64; LED++){
spi_transfer(color_buffer_mix[LED]);
}
//Deactivate the RGB matrix.
digitalWrite(SLAVESELECT, HIGH);
//wait for 100ms
delay(100); // allow some time for the Serial data to be sent

}

//Use this command to send a single color value to the RGB matrix.
//NOTE: You must send 64 color values to the RGB matrix before it displays an image!
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
{
};
return SPDR; // return the received byte
}

//Function to mix the values into a composite one
void getMix(int* mix, int* firstValue, int* secondValue)
{

for(int i = 0; i<8; i++)
{
for(int j = 0; j<4; j++)
{
mix[i*8+j] = secondValue[i*8+j+4];
mix[i*8+j+4] = firstValue[i*8+j+4];
}
}

}

void clearMatrix()
{
//Activate the RGB Matrix
digitalWrite(SLAVESELECT, LOW);
//Send the color buffer to the RGB Matrix
for(int LED=0; LED<64; LED++){
spi_transfer(color_buffer_empty[LED]);
}
//Deactivate the RGB matrix.
digitalWrite(SLAVESELECT, HIGH);
delay(100);

}

void copyMatrix(int source[64], int dest[64])
{
for(int i=0; i<64; i++)
{
dest[i] = source[i];
}
}

void changeColor(char newcolor, int color_buffer_temp[64])
{
for(int i =0; i<64; i++)
{
if (color_buffer_temp[i] != 0)
{
color_buffer_temp[i] = newcolor;
}
}
}


Sustanability Pyramids Arduino Code

#define MAX_LED_VALUE 99
#define MAX_GAMES_ROUNDS 5

int led1RedPin = 9; // LED connected to digital pin 9
int led1GreenPin = 3; // LED connected to digital pin 3
int led2RedPin = 10; // LED connected to digital pin 10
int led2GreenPin = 5; // LED connected to digital pin 5
int led3RedPin = 11; // LED connected to digital pin 11
int led3GreenPin = 6; // LED connected to digital pin 6

int ledPin13Pin = 13;
int previousValue = 99;

int gameIndex = 0;
int previousGameIndex = MAX_GAMES_ROUNDS - 1;
int secondPreviousGameIndex = MAX_GAMES_ROUNDS - 2;

int gameValues[ MAX_GAMES_ROUNDS ];

int fiveLatestTotal = 5 * MAX_LED_VALUE;
int threeLatestTotal = 3 * MAX_LED_VALUE;

float led1Value = 0;
float led2Value = 0;
float led3Value = 0;

float led1TargetValue = MAX_LED_VALUE;
float led2TargetValue = MAX_LED_VALUE;
float led3TargetValue = MAX_LED_VALUE;

unsigned long waitingTimeIncrease = 0;
unsigned long waitingTimeDim = 0;

int availableBytes = 0;

void setup() {
Serial.begin(9600);
/*
pinMode( led1RedPin, OUTPUT ); // LED connected to digital pin 9
pinMode( led1GreenPin, OUTPUT ); // LED connected to digital pin 3
pinMode( led2RedPin, OUTPUT ); // LED connected to digital pin 10
pinMode( led2GreenPin, OUTPUT ); // LED connected to digital pin 5
pinMode( led3RedPin, OUTPUT ); // LED connected to digital pin 11
pinMode( led3GreenPin, OUTPUT ); // LED connected to digital pin 6

*/
pinMode( ledPin13Pin, OUTPUT ); // LED connected to digital pin 13

waitingTimeIncrease = millis();
waitingTimeDim = millis();

for( int i = 0; i = 1 ){

int value = Serial.read();
Serial.println(value);
Serial.println(value, DEC);
Serial.println(value,BYTE);
Serial.println();
if( value >= 0 && value <= 99)
{

int fivePrevious = gameIndex - 5;
if( fivePrevious < 0 )
fivePrevious = MAX_GAMES_ROUNDS - fivePrevious;

int threePrevious = gameIndex - 3;
if( threePrevious = MAX_GAMES_ROUNDS )
{
gameIndex = 0;
}

}
}

if((millis() - waitingTimeDim ) > 200 )
{
if( led1Value != led1TargetValue )
{
if( led1Value > led1TargetValue )
{
led1Value--;
}
else if( led1Value led2TargetValue )
{
led2Value--;
}
else if( led2Value led3TargetValue )
{
led3Value--;
}
else if( led3Value < led3TargetValue )
{
led3Value++;
}

analogWrite( led3RedPin, MAX_LED_VALUE - led3Value );
analogWrite( led3GreenPin, led3Value );
}
waitingTimeDim = millis();
}
}

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

%d bloggers like this: