/* Workout TV * ------------------ * * Program description: * * The Arduino board is connected with a universal remote, which has to be set up for the TV you are using. * To the Arduino board is also connected a PIR sensor (passive infrared / motion sensor). * The program counts the number of seconds since last activation of the motion sensor. * After 15 seconds without motion, the program is muted. * After another 5 seconds without motion the program enters scramble state. * In scramble state the program can do several actions by signals to the TV depending on the user's wishes. * This program is prepared for five kinds of actions: mute, channelup, channeldown, text TV and AV/TV. * The last two actions mentioned is not currently used, since the specifik TV do not support these features. * At any time the motion sensor is activated, the program changes back to the original channel and unmutes. * * * Created April 2006 * Freeware 2006, Jacob Remin & Martin Aggerbeck * http://arduino.berlios.de * */ // Analog pins int TV_ONPin = 0; // Light sensor, checks whether the TV's standby lamp is lit int randomPin = 1; // Used to collect random numbers // Digital pins int ledPin = 13; // High when motion is registrered int motionPin = 9; // PIR motionsensor // Remote pins, connected to the remote int mutePin = 12; int channelUpPin = 10; int channelDownPin = 11; int TV_AVPin = 6; // Only with expansion pack int TTVPin = 7; // Only with expansion pack // Global variables boolean motion = false; // Tells whether the motionsensor has registered motion int counter = 0; // Counts the time the users has been inactive boolean muteSignal = false; // Remembers whether the TV is muted or unmuted int channelMem = 0; // Remembers which channel the user were watching before scrambling :) int sendTime = 300; // The time it take for a command to be sent from the remote // Must be set before exibition int onValue = 1000; // The light level for the standby led of the TV void setup(){ pinMode(ledPin, OUTPUT); pinMode(motionPin, INPUT); beginSerial(9600); pinMode(mutePin, OUTPUT); pinMode(channelUpPin, OUTPUT); pinMode(channelDownPin, OUTPUT); pinMode(TV_AVPin, OUTPUT); pinMode(TTVPin, OUTPUT); } void pause(a){delay(a*500);} // Simple fundction to speed the program up and down, pause(2) = 1 second int random(ran){ // Uses the bagground noise of the randomPin to collect a random number between 0 and ran-1 int value = analogRead(randomPin); if (randomPin == 5) {randomPin = 1;} else {randomPin++;} printNewline(); printString("value: "); printInteger(value); value = value%ran; printNewline(); printString("value: "); printInteger(value); return(value); } boolean motionCheck(){ // Registers if there is a high signal from the motionsensor, if yes, the ledPin is turned on motion = digitalRead(motionPin); if (motion){ digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); return motion; } } boolean TVCheck(){ // Checks if the TV is turned on int on = analogRead(TV_ONPin); return (on > onValue);} void send(pin){ // Sends af signal from the remote for a period of sendtime digitalWrite(pin, HIGH); delay(sendTime); digitalWrite(pin, LOW); } // ACTIONS: mute, channelUp, channelDown, TV_AV, TTV void mute(){ // Mutes and unmutes the TV muteSignal = !muteSignal; send(mutePin); if (muteSignal){ printNewline(); printString("mute"); } else { printNewline(); printString("unmute"); } } void channelUp(){ // Switches the channel one up channelMem++; send(channelUpPin); printNewline(); printString ("channelUp: "); printInteger(channelMem); } void channelDown(){ // Switches the channel one down channelMem--; send(channelDownPin); printNewline(); printString ("channelDown: "); printInteger(channelMem); } void TV_AV(){ // Switches to AV and back to TV -Only with expansion pack!! send(TV_AVPin); printNewline(); printString ("switching to AV"); pause(2); printNewline(); printString ("switching to TV"); } void TTV(){ // Switches to Text TV and back to TV -Only with expansion pack!! send(TTV); printNewline(); printString ("switching to TTV"); pause(2); printNewline(); printString ("switching to TV"); } void loop(){ if (!TVCheck()){ // TV on? printNewline(); printString("TV off"); delay(5000); // If not, check again after five seconds } else { counter = 1; printNewline(); printString("TV on"); while (counter > 0 && counter <= 14 && TVCheck()){ // Counter goes one up for each second without movement counter++; printNewline(); printString("counter: "); printInteger(counter); pause(2); if (motionCheck()){ counter = 0; printNewline(); printString("movement"); } } if (counter==15) {mute();} // Counter goes one up for each second without movement while (counter >= 15 && counter <= 20 && TVCheck()) { counter++; printNewline(); printString("counter: "); printInteger(counter); pause(2); if (motionCheck()){ // If motion is registered, reset the counter and unmute counter = 0; mute(); printNewline(); printString("movement(mute)"); } } while (counter >= 20 && TVCheck()){ // Scramble state is starting, scrambling signal by using one of the different actions int ran = 0; channelMem = 0; // Remembers which channel was active before scrambling printNewline(); printString ("SCRAMBLE!! :o)"); printNewline(); printString ("channelMem: "); printInteger (channelMem); while (!motionCheck() && TVCheck()){ ran = random(3); // Only the three first kinds of actions is used // ACTIONS if (ran==0) {mute(); } if (ran==1) {channelUp(); } if (ran==2) {channelDown(); } if (ran==3) {TV_AV(); } // Only with expansion pack if (ran==4) {TTV(); } // Only with expansion pack pause(2); } if (!TVCheck()) { printNewline(); printString("TV off (scramble)"); } else { printNewline(); printString("movement (scramble)"); printNewline(); printString("channelMem: "); printInteger(channelMem); pause(1); while (channelMem!=0){ // Finds back to the original channel if (channelMem < 0){channelUp(); pause(1);} else {channelDown(); pause(1);} } if (muteSignal) {mute();} // Makes sure that the TV is with sound } counter = 0; } } }