Thursday, March 29, 2012

Final Project Code

int buttonState1 = 0;
int buttonState2 = 0;
int buttonPin1 = 4;
int buttonPin2 = 6;
int power = 0;
int menuState = 0; //Channel Surf = 1, Channel input = 2, Volume = 3, Power = 4;
int displayState = 0; //Channel Surf = 1, Channel input = 2, Volume = 3, Power = 4;
int CS = 0; //Channel up = 1, Channel down = 2, Exit = 3
int CI = 0; //hundreds = 1, tens = 2, single = 3, Exit = 4
int vol = 0; //Volume up = 1, Volume down = 2, Exit = 3

void setup(){
  Serial.begin(9600);
}

void loop(){
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  if(power == 0){
    if(buttonState1 == 1){
      power = 1;
    }
    else if(buttonState2 == 1){
      power = 1;
    }
    if(power == 1){
      //transmit code to turn power on
      displayState = 1;
    }
    buttonState1 = 0;
    buttonState2 = 0;
  }

  if (power == 1){
    //switch through main menu
    if(menuState == 0){
      if(displayState == 1){
        //LCD Displays Channel Surf Menu
      }
      if(displayState == 2){
        //LCD Displays Channel Input menu
      }
      if(displayState == 3){
        //LCD Displays Volume Menu
      }
      if(displayState == 4){
        //LCD Displays Power Menu
      }

      if(buttonState2 == 1){
        displayState = displayState + 1;
        if(displayState == 5){
          displayState = 1;
        }
      }
    }

    //go into the menu (not just display)
    if(buttonState1 = 1){
      menuState = displayState;
    }
    //Channel Surf Menu
    if (menuState == 1){
      CS = 1;
      if(buttonState2 == 1){
        CS = CS+1;
        if(CS == 4){
          CS = 1;
        }
      }
      if(buttonState1 == 1){
        if(CS == 1){
          //transmit code to switch channel up
        }

        if(CS == 2){
          //transmit code to switch channel down
        }

        if(CS == 3){
          menuState = 0;
        } 
      }
    }

    //Channel Input Menu
    if (menuState == 2){
      int hundreds = 0;
      int tens = 0;
      int ones = 0;
      CI = 1;
      if(buttonState2 == 1){
        CI = CI + 1;
        if (CI = 5){
          CI = 1;
        }
      }
      if(buttonState1 == 1){
        if(CI == 1){
          hundreds = hundreds + 1;
          if(hundreds == 10){
            hundreds = 0;
          }
        }
        if(CI == 2){
          tens = tens + 1;
          if (tens == 10){
            tens = 0;
          }
        }
        if(CI == 3){
          ones = ones + 1;
          if (ones == 10){
            ones = 0;
          }
        }
        if(CI == 4){
          menuState = 0;
        }  
      }
    }

    if (menuState == 3){
      Vol = 1;
      if(buttonState2 == 1){
        Vol = Vol+1;
        if(Vol == 4){
          Vol = 1;
        }
      }
      if(buttonState1 == 1){
        if(Vol == 1){
          //transmit code to switch volume up
        }

        if(Vol == 2){
          //transmit code to switch volume down
        }

        if(Vol == 3){
          menuState = 0;
        } 
      }
    }
    if (menuState == 4){
      if (buttonState1 == 1){
        //transmit code o switc TV off
        power = 0; 
      }
      if (buttonState2 == 1){
        menuState = 0;
      }
    }
  }
}

pseudo code


If (power == off)
Hit either button to turn on
Else LCD displays “channel surf”
Button 2: cycle (channel input, volume, power)

Channel Surf
Button 1:  “enter” go into channel
Button 1: channel up
Button 2: switch to “channel down”
Button 1: becomes channel down
Button 2: exit

Channel input
Button 1: cycle digit (starting from hundreds, then tens, then single)
Button 2: enter

Favorite channel

Volume
Button 1: volume up
Button 2: switch to “volume down”
Button 1: becomes volume down
Button 2: exit

Power
Button 1: power off
Button 2: exit

IR code (up to channel input)

int buttonState1 = 0;
int buttonState2 = 0;
int buttonPin1 = 4;
int buttonPin2 = 6;
int power = 0;
int menuState = 0; //Channel Surf = 1, Channel input = 2, Volume = 3, Power = 4;
int displayState = 0; //Channel Surf = 1, Channel input = 2, Volume = 3, Power = 4;
int CS = 0; //Channel up = 1, Channel down = 2, Exit = 3
int CI = 0; //hundreds = 1, tens = 2, single = 3, Exit = 4

void setup(){
  Serial.begin(9600);
}

void loop(){
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  if(power == 0){
    if(buttonState1 == 1){
      power = 1;
    }
    else if(buttonState2 == 1){
      power = 1;
    }
    if(power == 1){
     //transmit code to turn power on
     displayState = 1;
    }
    buttonState1 = 0;
    buttonState2 = 0;
  }
 
  if (power == 1){
  //switch through main menu
  if(menuState == 0){
    if(displayState == 1){
      //LCD Displays Channel Surf Menu
    }
    if(displayState == 2){
      //LCD Displays Channel Input menu
    }
    if(displayState == 3){
      //LCD Displays Volume Menu
    }
    if(displayState == 4){
      //LCD Displays Power Menu
    }
   
  if(buttonState2 == 1){
    displayState = displayState + 1;
    if(displayState == 5){
      displayState = 1;
    }
  }
   }
  
  //go into the menu (not just display)
  if(buttonState1 = 1){
   menuState = displayState;
  }
   //Channel Surf Menu
    if (menuState == 1){
      CS = 1;
    if(buttonState2 == 1){
      CS = CS+1;
      if(CS == 4){
        CS = 1;
      }
    }
    if(CS == 1){
      if(buttonState1 == 1){
        //transmit code to switch channel up
      }
    }
    if(CS == 2){
      if(buttonState1 == 1){
        //transmit code to switch channel down
      }
    }
    if(CS == 3){
      if(buttonState1 == 1){
        menuState = 0;
      }
    }


   //Channel Input Menu
  while (menuState == 2){
    int hundreds = 0;
    int tens = 0;
    int ones = 0;
    CI = 1;
    if(buttonState2 == 1){
      CI = CI + 1;
      if (CI = 5){
        CI = 1;
      }
    while(CI == 1){
      if(buttoState1 == 1
  }
}

Thursday, March 15, 2012

#include "pitches.h"

long result = 0;
int melody[] = {
  NOTE_C4, NOTE_D4,NOTE_E4, NOTE_D4};
int noteDurations[] = {
  8, 8, 8, 8};
int dark = 0; 
int pushbutton = 0;
int buttonState;
int buttonPin = 12;
int LEDpin = 7;
int LEDon = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("start");
}


long RCtime(int sensPin){
  long result = 0;
  pinMode(sensPin, OUTPUT); // make pin OUTPUT
  digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
  delay(1); // wait a ms to make sure cap is discharged
  pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
  digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
  while(digitalRead(sensPin)){ // wait for pin to go low
    result++;
  }
  return result; // report results
}



void loop()
{



  Serial.println(analogRead(0));
  while (pushbutton == 0){
    if (analogRead(0)>900) {
      Serial.println("dark");
      dark = 1;

      while(dark == 1){
        for (int thisNote = 0; thisNote < 4; thisNote++) {
          int noteDuration = 1000/noteDurations[thisNote];
          tone(8, melody[thisNote],noteDuration);
          int pauseBetweenNotes = noteDuration * 1.30;
          delay(pauseBetweenNotes);
          noTone(4);
        }
        //  Serial.println("for");
        pinMode(buttonPin,INPUT);
        buttonState = digitalRead(buttonPin);
        Serial.println(buttonState);
        if (buttonState == 0){
          Serial.println("button is pressed");
          pinMode(LEDpin, OUTPUT);
          digitalWrite(LEDpin, LOW);
          dark = 0;
        }
      }
    }

  }
}



photocell with analog

//this one gives you a number fluctuation based on light intensity. high number = dark, low number = bright

int sensorPin = 4; // 220 or 1k resistor connected to this pin
long result = 0;

void setup() // run once, when the sketch starts
{
  Serial.begin(9600);
  Serial.println("start"); // a personal quirk
}

void loop() // run over and over again
{
  Serial.println(analogRead(0));
//  if (RCtime(sensorPin)>100){
//    Serial.println("there is bright light");
//    Serial.println(RCtime(sensorPin));
//  }
//  Serial.println( RCtime(sensorPin) );
//  delay(100);
}
long RCtime(int sensPin){
  long result = 0;
  pinMode(sensPin, OUTPUT); // make pin OUTPUT
  digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
  delay(1); // wait a ms to make sure cap is discharged
  pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
  digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
  while(digitalRead(sensPin)){ // wait for pin to go low
    result++;
  }
  return result; // report results
}

success sound in void loop

 #include "pitches.h"

int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };

void setup() {

}

void loop() {
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}

Thursday, March 1, 2012

EEPROM write/read pushbutton state


#include <EEPROM.h>
int buttonState = 0;
int buttonPin = 7;
void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i=0; i < 13; i++){
  pinMode(buttonPin,INPUT);

  buttonState = digitalRead(buttonPin);
  if(buttonState==0){
    Serial.println("Button is pressed");

  }
 
  else{

   Serial.println("Button is not pressed");
  }
  EEPROM.write(i, buttonState);
  }
  while(1){}

 
}
 
 

EEPROM record light

#include <EEPROM.h>
int sensorPin = 4;
long result = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("start");
  for (int i=0; i < 13; i++){
    Serial.println(RCtime(sensorPin) );
    delay(1);
    EEPROM.write(i, RCtime(sensorPin));
    delay(15000);
  }
}


void loop()
{
}

long RCtime(int sensPin){
  long result = 0;
  pinMode(sensPin, OUTPUT); // make pin OUTPUT
  digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
  delay(1); // wait a ms to make sure cap is discharged
  pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
  digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
  while(digitalRead(sensPin)){ // wait for pin to go low
    result++;
  }
  return result;
}

Writing and Reading EEPROM

//Write


#include <EEPROM.h>

void setup()
{
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, i);
}

void loop()
{
}






//Read

#include <EEPROM.h>

int a = 0;
int value;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  value = EEPROM.read(a);

  Serial.print(a);
  Serial.print("\t");
  Serial.print(value);
  Serial.println();

  a = a + 1;

  if (a == 10)
    a = 0;

  delay(500);
}