Victor Espinoza – Update 10

Write Data Function Wrap – Up:

This week I finished up my custom Write Data function and changed all of the previous code to use this implementation. The code is as follows:

#define PERIOD 256

// Associate each number with its state name.
const int LOOKING_STATE = 0;
const int HAPPY_STATE = 1;
const int MAD_STATE = 2;
const int SAD_STATE = 3;
const int IDLE_STATE = 4;
const int INITIALIZE_TIMER = 5;

const int GREEN_LED = 11;  //Green LED signal going to RGB LED Strip
const int RED_LED   = 10;  //Red LED signal going to RGB LED Strip
const int BLUE_LED  = 9;   //Blue LED signal going to RGB LED Strip
const int FACE_DETECTED = 12; //Stimulates a face being detected
const int EXIT_IDLE = 13; // the number of the pushbutton pin

unsigned long previousMillis=0; //keep track of last updated time
static unsigned long currentMillis; //keep track of current time
volatile byte pinValue[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void timer_int_init(){
cli();//stop interrupts
//set timer1 interrupt at 2KHz (2000 Hz)
TCCR1A = 0;// zero-out TCCR1A register
TCCR1B = 0;// zero-out TCCR1B register
TCNT1  = 0;//initialize counter value to 0
// set compare match register for 2KHz (0.5ms)increments
OCR1A = 15624;// = (16*10^6) / (2000*256) – 1 (must be <65536)
TCCR1B |= (1 << WGM12); // turn on CTC mode
TCCR1B |= (1 << CS12);  // Set CS12 bit for 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt

sei();//allow interrupts
}//close timer_int_init()

// generate PWM on the different digital pins
void writePinValue(byte pinNum, byte dutyCycle) {
if (pinNum >= 2 && pinNum <= 13 && dutyCycle >= 0 && dutyCycle < PERIOD) {
pinNum -= 2;
pinValue[pinNum] = dutyCycle; //get proper pinValue index
if (dutyCycle == 0)
if(pinNum <= 7) // updating pins 2 – 7 (PORTD)
PORTD &= ((0 << pinNum) & 0xFF); // sets pin low
else // updating pins 8 – 13 (PORTB)
PORTB &= ((0 << pinNum-8) & 0xFF) ; // sets pin low
} //end if
} //end analogWrite(…)

// Routine to emit the PWM on the pins
ISR(TIMER1_COMPA_vect) {
static int counter = 0;
for(int i = 0; i < 12; i += 1) {
if (pinValue[i]){
if(i+2 <= 7) // updating pins 2 – 7 (PORTD)
PORTD &= (((pinValue[i]>counter)<<i+2) & 0xFF); // update pin value
else // updating pins 8 – 13 (PORTB)
PORTB &= (((pinValue[i]>counter)<<i+2) & 0xFF); // update pin value
} // end if
counter = ++counter > PERIOD ? 0 : counter;
} //end for
} //end ISR(…)

void setup() {
timer_int_init();
// initialize digital pin 13 as an output.
DDRB = DDRB | B00001110; // pins 9, 10, and 11 – outputs, pin 12 and 13 – inputs
//  pinMode(GREEN_LED, OUTPUT); // initialize GREEN_LED as an output.
//  pinMode(RED_LED, OUTPUT);   // initialize RED_LED as an output.
//  pinMode(BLUE_LED, OUTPUT);  // initialize BLUE_LED as an output.
//  pinMode(FACE_DETECTED, INPUT); // initialize BUTTON_PIN as an input.
//  pinMode(EXIT_IDLE, INPUT); // initialize BUTTON_PIN as an input.
} //close setup

void loop() {
static int current_state = LOOKING_STATE; //Initialize to Looking State
currentMillis = millis();
switch(current_state){
case LOOKING_STATE:
//PWM Combination for Gray color:
writePinValue(RED_LED, 20);  // analogWrite values from 0 to 255
writePinValue(GREEN_LED, 20);  // analogWrite values from 0 to 255
writePinValue(BLUE_LED, 20);  // analogWrite values from 0 to 255
if(digitalRead(FACE_DETECTED) == HIGH)
current_state = HAPPY_STATE;
else if((unsigned long) currentMillis – previousMillis > 120000 ){
//Desk-Buddy has been looking for a face for 2 minutes (120000 * .001 = 120s = 2 minutes)
//and has not found anyone. Go to the idle state…
current_state = IDLE_STATE;
previousMillis = currentMillis; //update current time value
}//end if
else
current_state = LOOKING_STATE;
break;
case HAPPY_STATE:
//PWM Combination for Green color:
writePinValue(RED_LED, 0);  // analogWrite values from 0 to 255
writePinValue(GREEN_LED, 150);  // analogWrite values from 0 to 255
writePinValue(BLUE_LED, 0);  // analogWrite values from 0 to 255
if(digitalRead(FACE_DETECTED) == LOW){
previousMillis = currentMillis; //update current time value
current_state = MAD_STATE;
}//end if
else
current_state = HAPPY_STATE;
break;
case MAD_STATE:
//PWM Combination for Red color
writePinValue(RED_LED, 150);  // analogWrite values from 0 to 255
writePinValue(GREEN_LED, 0);  // analogWrite values from 0 to 255
writePinValue(BLUE_LED, 0);  // analogWrite values from 0 to 255
if(digitalRead(FACE_DETECTED) == HIGH){
previousMillis = currentMillis; //update current time value
current_state = HAPPY_STATE;
}//end if
else if((unsigned long) currentMillis – previousMillis > 10000 ){
//Desk-Buddy has been in the Mad State for 10 seconds (10000 * .001 = 10s).
//Transition to the next state
current_state = SAD_STATE;
previousMillis = currentMillis; //update current time value
}//end else if
else
current_state = MAD_STATE;
break;
case SAD_STATE:
//PWM Combination for Blue color
writePinValue(RED_LED, 0);  // analogWrite values from 0 to 255
writePinValue(GREEN_LED, 0);  // analogWrite values from 0 to 255
writePinValue(BLUE_LED, 150);  // analogWrite values from 0 to 255
if(digitalRead(FACE_DETECTED) == HIGH){
previousMillis = currentMillis; //update current time value
current_state = HAPPY_STATE;
}//end if
else if((unsigned long) currentMillis – previousMillis > 5000 ){
//Desk-Buddy has been in the Mad State for 5 seconds (5000 * .001 = 5s).
//Transition to the next state
previousMillis = currentMillis; //update current time value
current_state = LOOKING_STATE;
}//end else if
else
current_state = SAD_STATE;
break;
case IDLE_STATE:
//PWM Combination for Purple color:
writePinValue(RED_LED, 50);  // analogWrite values from 0 to 255
writePinValue(GREEN_LED, 0);  // analogWrite values from 0 to 255
writePinValue(BLUE_LED, 50);  // analogWrite values from 0 to 255
if(digitalRead(EXIT_IDLE) == HIGH){
previousMillis = currentMillis; //update current time value
current_state = LOOKING_STATE;
}//end if
else
current_state = IDLE_STATE;
break;
default:
//PWM Combination for Purple color:
writePinValue(RED_LED, 0);  // analogWrite values from 0 to 255
writePinValue(GREEN_LED, 0);  // analogWrite values from 0 to 255
writePinValue(BLUE_LED, 0);  // analogWrite values from 0 to 255
current_state = LOOKING_STATE;
break;
}//end switch statement

} //close loop

Advertisements

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