Saturday, October 27, 2007

Week_6 & 7_Midterm_code

// Output
int potColorPin = 0; // Color
int potColorVal = 0; // value read from the Color pot

int potBlinkPin = 1;
int potBlinkVal = 0;
int blinkSpeed = 0;

int collectiveBlueLed = 6;
int collectiveGreenLed = 10;

int GreenIndLed = 3; // Controls the Green value for individual
int BlueIndLed = 5; // Controls the Blue value for individual
int HeartBeatLed = 9; // Displays Heart beat /////////////////// ENABLED ////////

// Program variables
int GreenColorVal = 0; //
int BlueColorVal = 0;

int colorValB = 0;
int colorValC = 0;
int colorValD = 0;
int colorValE = 0;

int blinkSpeedB = 0;
int blinkSpeedC = 0;
int blinkSpeedD = 0;
int blinkSpeedE = 0;

int DEBUG = 1; // Set to 1 to turn on debugging output

int i = 0; // Loop counter for indivitual values
int j = 0; // loop counter for incoming values
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades

char inbyte = 0;
long beat; ////////////////////////////////
long timeCode; ///////////////////////////////////////

long colorReceive = 1;
long blinkReceive = 1; // 1 to prevent 0 being multiplied by 4..... safety feature

int collectiveGreen = 0;
int collectiveBlue = 0;
int collectiveSpeed = 0;


void setup() {

pinMode(GreenIndLed, OUTPUT); // sets the pins as output
pinMode(BlueIndLed, OUTPUT);
pinMode(HeartBeatLed, OUTPUT);
pinMode(potBlinkPin, INPUT);
pinMode(potColorPin, INPUT);

pinMode(collectiveBlueLed, OUTPUT);
pinMode(collectiveGreenLed, OUTPUT);

Serial.begin(9600); // ...set up the serial ouput in 0004 format

// if (DEBUG) { // If we want to see the pin values for debugging..//.

// }
}


void loop() {
potColorVal = analogRead(potColorPin); // read the potentiometer value at the input pin
// Premature Blinkage ASK ROB!
potBlinkVal = analogRead(potBlinkPin); // blink controlled by second pot
blinkSpeed = potBlinkVal +42; // +42 scale blink value 0 - 2000ish

if (potColorVal < 5) { // solves problem of no color at all by setting minimum possible as 5 ASK ROB!!!
potColorVal = 5;
}

//////////////////////////////////////////////////////

if (Serial.available() > 0 ) { // check that a byte has come in
char inByte = Serial.read(); // read the byte
if (inByte == '*') { // if this was the start byte
while (Serial.available() < 7) {
; // do nothing while waiting for all five bytes of information
}
beat = Serial.read(); // read the beat number
timeCode = Serial.read(); // read the first byte of the time code
timeCode = timeCode + (Serial.read() << 8); // read the second byte of the time code, shift it up and add to the existing number
timeCode = timeCode + (Serial.read() << 16); // read the third byte of the time code, shift it up and add to the existing number
timeCode = timeCode + (Serial.read() << 24); // read the fourth byte of the time code, shift it up and add to the existing number

Serial.flush(); // get rid of the carriage return and linefeed characters, to clear the input for the next beat
}

else if (inByte =='^') {
while (Serial.available() < 4) {
; // do nothing while waiting for all 2 bytes of information
}
colorReceive = Serial.read();
blinkReceive = Serial.read();

Serial.flush(); // get rid of the carriage return and linefeed characters, to clear the input for the next beat
}
}

/////// Send Serial to Fellow Meshie Pets based on initial value '$', colorVal, and blinkSpeed



////// Your beat

if (beat == 3){ // MUST CHANGE ACCORDINGLY: James 1, Tim 2, Youjeong 3, Keith 4
digitalWrite(HeartBeatLed, HIGH);
// potColorVal = colorValB;
// blinkSpeed = blinkSpeedB;

// send the PotColorValue and BlinkSpeed Value
}
else {
digitalWrite(HeartBeatLed, LOW);
}

///// Listen to each beat
if (beat == 1){
colorValB = colorReceive;
blinkSpeedB = blinkReceive;
/*
// incoming values pulled from meshpets assigned to local colorValB, blinkSpeedB
colorValB = GreenColorVal;
blinkSpeedB = blinkSpeed;

Serial.print('^'); // sends out the values to the group
Serial.print(colorValB, BYTE);
Serial.print(blinkSpeedB, BYTE);

beat = 0; /// beat is set to an unused value so that it is only sent usable
*/

}

else if (beat == 2){

colorValC = colorReceive;
blinkSpeedC = blinkReceive;


/* // incoming values pulled from meshpets assigned to local colorValB, blinkSpeedB
colorValC = GreenColorVal;
blinkSpeedC = blinkSpeed;

Serial.print('^'); // sends out the values to the group
Serial.print(colorValC, BYTE);
Serial.print(blinkSpeedC, BYTE);

beat = 0; /// beat is set to an unused value so that it is only sent usable once
*/
}

else if (beat == 3){
/* colorValD = colorReceive;
blinkSpeedD = blinkReceive;
*/
//incoming values pulled from meshpets assigned to local colorValB, blinkSpeedB
colorValD = GreenColorVal;
blinkSpeedD = blinkSpeed;

Serial.print('^'); // sends out the values to the group
Serial.print(colorValD, BYTE);
Serial.print(blinkSpeedD, BYTE);

Serial.print('^'); // sends out the values to the group
Serial.print(colorValD, BYTE);
Serial.print(blinkSpeedD, BYTE);

beat = 0; /// beat is set to an unused value so that it is only sent usable


}

else if (beat == 4){
colorValE = colorReceive;
blinkSpeedE = blinkReceive;

/*
// incoming values pulled from meshpets assigned to local colorValB, blinkSpeedB
colorValE = GreenColorVal;
blinkSpeedE = blinkSpeed;

Serial.print('^'); // sends out the values to the group
Serial.print(colorValE, BYTE);
Serial.print(blinkSpeedE, BYTE);

beat = 0; /// beat is set to an unused value so that it is only sent usable
*/

}

collectiveGreen = ( colorValB + colorValC + colorValD + colorValE) / 4;
collectiveBlue = 1023 - collectiveGreen;
collectiveSpeed = ( blinkSpeedB + blinkSpeedC + blinkSpeedD + blinkSpeedE) / 4;


if(j > collectiveSpeed*3) { // if i equals up to half of blink speed (scaled up 3 times) turn light on

analogWrite(collectiveGreenLed, collectiveGreen); // Write values to LED pins
analogWrite(collectiveBlueLed, collectiveBlue); // write blue value
}

else if(i < collectiveSpeed*3){ // if i equals upper half of blink speed (scaled up 3 times) turn light off
collectiveGreen = 0; // scales down GreenColorVal to 0 - 255 range
collectiveBlue = 0; // inverts correlation of blue and Green led

analogWrite(collectiveGreenLed, GreenColorVal); // Write values to LED pins
analogWrite(collectiveBlueLed, BlueColorVal);
}


j++; // j is the new delay controlled by the speed of the program currently:9600

if (j >= collectiveSpeed*6 ){ // resets j to 0
j = 0;
}


/////////////////////

if(i > blinkSpeed*3) { // if i equals up to half of blink speed (scaled up 3 times) turn light on
GreenColorVal = potColorVal/4; // scales down GreenColorVal to 0 - 255 range
BlueColorVal = 1023-GreenColorVal; // inverts correlation of blue and Green led

analogWrite(GreenIndLed, GreenColorVal); // Write values to LED pins
analogWrite(BlueIndLed, BlueColorVal); // write blue value
}

else if(i < blinkSpeed*3){ // if i equals upper half of blink speed (scaled up 3 times) turn light off
GreenColorVal =0; // scales down GreenColorVal to 0 - 255 range
BlueColorVal = 0; // inverts correlation of blue and Green led

analogWrite(GreenIndLed, GreenColorVal); // Write values to LED pins
analogWrite(BlueIndLed, BlueColorVal);
}


i++; // i is the new delay controlled by the speed of the program currently:9600

if (i >= blinkSpeed*6 ){ // resets i to 0
i = 0;
}

}

No comments: