/* Name: Derek Salomon Major: Electrical Engineering Class: Make Course Date: 04/20/2015 Purpose: Code is for Zelda Platform Project to control LED on TriForce and direction of the motor */ #include //ir sensor library #include //stepper motor library #define gearratio 64 //1:64 gear ratio const int stepsPerRevolution = 2048; Stepper myStepper(stepsPerRevolution, 8,9,10,11); //pin with the stepper motor is connected to int IRpin = 12; //ir sensor attached to pin 12 IRrecv irrecv(IRpin); decode_results results; // leds are attached to int led13 = 5; int led12 = 4; int led11 = 3; int led10 = 2; int flag = 0; int i; void setup() { myStepper.setSpeed(0.15*gearratio); Serial.begin(9600); irrecv.enableIRIn(); pinMode(led13, OUTPUT); pinMode(led12, OUTPUT); pinMode(led11, OUTPUT); pinMode(led10, OUTPUT); } void loop() { //digitalWrite(led10, HIGH); //digitalWrite(led11, HIGH); //digitalWrite(led12, HIGH); //digitalWrite(led13, HIGH); if (irrecv.decode(&results)) {//has a transmission been received? Serial.println(results.value);//If yes: interpret the received commands... switch (results.value) //Using switch/case statement with results.value as a variable { case 16753245: //when results.value is equal to decoding results of power button on remote if (flag == 0) //check if flag is 0 or not { digitalWrite(led10, HIGH); //turns led connected to pin 2 on and trun device on flag = 1; // make flag = 1 } else // statement if flag is not equal to 0 the moment the power button is pressed { digitalWrite(led10, LOW); // turns the led connected to pin 2 off flag = 0; //make flag = 0 } break; //break will get you out of the case case 16769055: //when results.value is equal to decoding results of - button on remote if (flag == 1) //if the device is not on, it will not do anything { //step on revolution by making the moter rotating counterclock wise Serial.println("counterclockwise"); myStepper.step(-stepsPerRevolution); break; } else //if flag is not equal to 1 or the platform is off, it will break the case and try to pick up another signal break; case 16754775: //when results.value is equal to decoding results of + button on remote if (flag == 1) //if the device is not on, it will not do anything { //step on revolution by making the moter rotating clockwise Serial.println("clockwise"); myStepper.step(stepsPerRevolution); break; } else //if flag is not equal to 1 or the platform is off, it will break the case and try to pick up another signal break; case 16724175: //when results.value is equal to decoding results of 1 button on remote if (flag == 1) //if the device is not on, it will not do anything { digitalWrite(led10, LOW); // turn the led on pin 2 off for(i=0; i<4; i++) //for loop that will allow the led to blink for until i=4 { //blinking for leds digitalWrite(led11, HIGH); delay(200); digitalWrite(led11, LOW); delay(200); digitalWrite(led12, HIGH); delay(200); digitalWrite(led12, LOW); delay(200); digitalWrite(led13, HIGH); delay(200); digitalWrite(led13, LOW); delay(200); } digitalWrite(led10, HIGH); //turn led on break; } else //if flag is not equal to 1 or the platform is off, it will break the case and try to pick up another signal break; case 16718055: //when results.value is equal to decoding results of 2 button on remote digitalWrite(led10, LOW); // turn the led on pin 2 off if (flag == 1) //if the device is not on, it will not do anything { for (i=0; i<3; i++) //for loop that will allow the led be on until i = 3 {//turn on leds digitalWrite(led11, HIGH); delay(800); digitalWrite(led12, HIGH); delay(800); digitalWrite(led13, HIGH); delay(800); } //turn off leds that were on digitalWrite(led11, LOW); digitalWrite(led12, LOW); digitalWrite(led13, LOW); digitalWrite(led10, HIGH); //turn led on pin 2 } else //if flag is not equal to 1 or the platform is off, it will break the case and try to pick up another signal break; } irrecv.resume(); //pick up signal after each case } }