/* -------------------------------------------------------------- Title: Sad Cat Fixer Project Name: Carson Burke Description: Create a arduino circuit that can be used as a product to turn a sad cat into a happy one. In this case I created a laser pointer that can rotate in 2 different dimensions, and is controlled by an infared remote. There is also a random setting which causes the servos to go to random positions for a set amount of time. Course Code: TEJ4M0 Pins Used: 2,3,4, and 6 --------------------------------------------------------------- */ //Includes the IR Remote and Servo libraries #include #include //Creates global constant integer variable, and assigns a value of 6 const int laserPin = 6; //Initiallizing the IR sensor, and assigning it to pin 2 const int irRecvPin = 2; IRrecv irRecv(irRecvPin); decode_results results; //Defining global variables as the wanted hexcodes going to be received //by the IR snsor #define leftTurn 0xFD20DF #define rightTurn 0xFD609F #define repeatRecv 0xFFFFFFFF #define upTilt 0xFD50AF #define downTilt 0xFD10EF #define lastRecvx 0 #define lastRecvy 0 #define powerButton 0xFD00FF #define functionButton 0xFD40BF //Initiallizing the integer variables for the motor degree values int xServoValue; int yServoValue; //Initiallizing the boolean power varaibles so arduino knows circuit is off bool powerMode = false; bool powerLastUse = false; //Initiallizing the old servo value integer variables int oldServoYValue; int oldServoXValue; //Initiallizing the delta servo values integer variables int deltaXValue; int deltaYValue; //Initiallizing xServo and yServo Servo yServo; Servo xServo; //Initiallizing boolean xAxis, yAxis bool xAxis, yAxis; //Initiallizing integer timer variables int lastTime; int currentTime; int differenceTime; //------------------------------------------------------------------------ //Start of setup function void setup(){ //Setting pinmodes for every pin used pinMode (2, INPUT); pinMode (3, OUTPUT); pinMode (4, OUTPUT); pinMode (laserPin, OUTPUT); //Initiallizing the IR sensor irRecv.enableIRIn(); irRecv.blink13(true); //Attaching xServo to pin3, and yServo to pin 4 xServo.attach(3); yServo.attach(4); } //End of setup function //--------------------------------------------------------------------------- //Start of loop function void loop() { //Checks to see if infared sensor has recieved anything, if yes continues if (irRecv.decode(&results)) { //Resumes the IR sensor irRecv.resume(); //Checks to see if the power button was pressed, or if it the signal //is a repeat signal and the power button was the last one pressed if (results.value == powerButton || (results.value == repeatRecv && powerLastUse)) { //If so, powerLastUse set to true powerLastUse = true; //Checks to see if circuit is off if (powerMode == false) { //If off, sets powerMode to ture and turns on laser/LED powerMode = true; digitalWrite (laserPin, HIGH); //Sets both servos and their corresponding variable values to 90 yServo.write(90); xServo.write(90); xServoValue = 90; yServoValue = 90; //This resets the Servos to nuetral whenever they turn on } //Else if circuit is on else if (powerMode) { //Sets powerMode to off and turns off laser/LED powerMode = false; digitalWrite(laserPin, LOW); } //Delay of 500ms so circuit doesnt keep turning on and off delay(500); } //Else if any other signal is recieved, sets powerLastUse to false else { powerLastUse = false; } //Checks to see if cicuit is on if (powerMode) { //If so, resets the last time clock to current time lastTime = millis(); //Check to see if recieved results are match the left turn signal if (results.value == leftTurn) { //Sets the current xServo value to oldServoXValue oldServoXValue = xServoValue; //Defines xServoValue to XChanger function sending parameters //of which direction to go, and what the current value is xServoValue = XChanger(-1, xServoValue); //Calculates the difference in the old xServo value and the new one, //assigns to deltaXValue deltaXValue = xServoValue - oldServoXValue; //Defines the lastRecvX variable to the left turn code, and resets lastRecvY #define lastRecvx 0xFD20DF #define lastRecvy 0 //Defines which axis was last used xAxis = true; yAxis = false; } //Check to see if recieved results are match the right turn signal else if (results.value == rightTurn) { //Sets the current xServo value to oldServoXValue oldServoXValue = xServoValue; //Defines xServoValue to XChanger function sending parameters //of which direction to go, and what the current value is xServoValue = XChanger(1, xServoValue); //Calculates the difference in the old xServo value andn the new one, //assigns to deltaXValue deltaXValue = xServoValue - oldServoXValue; //Defines the lastRecvX variable to the left turn code, and resets lastRecvY #define lastRecvx 0xFD609F #define lastRecvy 0 //Defines which axis was last used xAxis = true; yAxis = false; } //Checks to see if the results equal the repeat signal, X Axis was the last axis used //and if the lastRecvX equals left turn or right turn signal else if (results.value == repeatRecv && xAxis && ((lastRecvx == leftTurn) || (lastRecvx == rightTurn))) { //Sets the current xServo value to oldServoXValue oldServoXValue = xServoValue; //Adds deltaXValue to xServoValue xServoValue += deltaXValue; //Checks to see if the value goes over the limit of the Servo and fixes if so if (xServoValue < 0) { xServoValue = 0; } else if (xServoValue > 180) { xServoValue = 180; } //Resets the deltaValue in case the Servo limit was reached deltaXValue = xServoValue - oldServoXValue; //Sets xServo to its value xServo.write(xServoValue); } //Check to see if recieved results are match the down tilt signal else if (results.value == downTilt) { //Sets the current yServo value to oldServoYValue oldServoYValue = yServoValue; //Defines xServoValue to YChanger function sending parameters //of which direction to go, and whtat the current value is yServoValue = YChanger(-1, yServoValue); //Calculates the difference in the old yServo value andn the new one, //assigns to deltaYValue deltaYValue = yServoValue - oldServoYValue; //Defines the lastRecvY varaible to the down tilt code, and resets lastRecvX #define lastRecvy 0xFD10EF #define lastRecvx 0 //Defines which axis was last used xAxis = false; yAxis = true; } //Check to see if recieved results are match the up tilt signal else if (results.value == upTilt) { //Sets the current yServo value to oldServoYValue oldServoYValue = yServoValue; //Defines xServoValue to YChanger function sending parameters //of which direction to go, and whtat the current value is yServoValue = YChanger(1, yServoValue); //Calculates the difference in the old yServo value andn the new one, //assigns to deltaYValue deltaYValue = yServoValue - oldServoYValue; //Defines the lastRecvY varaible to the up tilt code, and resets lastRecvX #define lastRecvy 0xFD50AF #define lastRecvx 0 //Defines which axis was last used xAxis = false; yAxis = true; } //Checks to see if the results equal the repeat signal, y Axis was the last axis used //and if the lastRecvy equals up tilt or down tilt signal else if (results.value == repeatRecv && yAxis && ((lastRecvy == downTilt) || (lastRecvy == upTilt))) { //Sets the current yServo value to oldServoYValue oldServoYValue = yServoValue; //Adds deltaYValue to yServoValue yServoValue += deltaYValue; //Checks to see if the value goes over the limit of the Servo and fixes if so if (yServoValue < 0) { yServoValue = 0; } else if (yServoValue > 180) { yServoValue = 180; } //Resets the deltaValue in case the Servo limit was reached deltaYValue = yServoValue - oldServoYValue; //Sets xServo to its value yServo.write(yServoValue); } //Checks to see if function button was pressed else if (results.value == functionButton) { //Calls upon rancChanger cuntion randChanger(xServoValue, yServoValue); } } //Resets the results results.value = 0; //Delay so user canhave more control delay(70); } //Finds the current time currentTime = millis(); //Finds the diffrence between the last time of input and the current time differenceTime = currentTime - lastTime; //If that difference is greater than 10 seconds //(Would be chnaged in final product to fit users needs) if ( differenceTime >= 10000) { //Turns powerMode to false and turns off LED/laser powerMode = false; digitalWrite(laserPin, LOW); } } //End of loop function //------------------------------------------------------------------------------------------- //Start of XChanger function //Assigns parameters to integers multiplier and currentValue int XChanger(int multiplier, int currentValue) { //Determines the change value of the servo int changeValue = 10 * multiplier; //Caluculates the new value absed off of old value and changer int newValue = currentValue + changeValue; //Checks to see if it is past the Servo limits and fixes if so if (newValue < 0) { newValue = 0; } else if (newValue > 180) { newValue = 180; } //Sets xServo to value xServo.write(newValue); //Returns newValue return newValue; } //End of xChanger function //Start of YChanger function //Assigns parameters to integers multiplier and currentValue int YChanger(int multiplier, int currentValue) { //Determines the change value of the servo int changeValue = 10 * multiplier; //Caluculates the new value absed off of old value and changer int newValue = currentValue + changeValue; //Checks to see if it is past the Servo limits and fixes if so if (newValue < 0) { newValue = 0; } else if (newValue > 180) { newValue = 180; } //Sets yServo to value yServo.write(newValue); //Returns newValue return newValue; } //End of YChanger function //---------------------------------------------------------------------------------------- //Start of randChanger function //Sets parameters to integers xValue and yValue void randChanger(int xValue, int yValue) { //Finds the start time of function int startTime = millis(); //While loop under condition of it is less than 10 seconds since start //Can be changed in fianl product to fit user needs while (startTime + 10000 > millis()) { //Calculates random values for the X and Y target values int xTarget = 10*(random(1, 18)); int yTarget = 10*(random(1, 18)); //While loop for conditon current X and Y values do not match target X and Y values while (xValue != xTarget && yValue != yTarget) { //Finds out which direction the xServo needs to move to reach target if (xValue < xTarget) { xValue+=10; } else if (xValue > xTarget) { xValue-=10; } //Moves xServo towards target xServo.write(xValue); //Finds out which direction the yServo needs to move to reach target if (yValue < yTarget) { yValue+=10; } else if (yValue > yTarget) { yValue-=10; } //Moves yServo towards target yServo.write(yValue); //Delay of 100ms so Servos dont move too quick delay(100); } } //Turns powerMode false and turns off Laser/LED powerMode = false; digitalWrite(laserPin, LOW); } //End of randChanger function //-------------------------------------------------------------------------------------------- //End of program