Paso 4: Código de Arduino
Para que el mando a distancia trabajar, es necesario insertar la batería C2025.
Dato divertido: Luz infrarroja no puede verse por el ojo humano y se encuentra justo debajo del espectro de luz visible. Para comprobar si funciona el control remoto, encender la cámara de su teléfono, sostenga el teléfono con el bulbo de IR frente a la cámara y pulse cualquier botón. Verá una luz intermitente de la bombilla. Esto indica que está funcionando y se puede decir que con éxito han visto la luz infrarroja y muestra a tus amigos!
//Code for Movable Bat Signal //Written by Rachel Porter Spring 2015
#include #include Servo myservo; int IR_PIN = 11; int SERV_PIN = 6; int P_RED = 2; int P_GREEN = 3; int P_BLUE = 5; unsigned long lastresult = 0;//the last code from remote - keeps last value so that the last thing you did can be repeated IRrecv irrecv(IR_PIN);//IR receiver is connected to pin 11 int pos = 0;//the initial position of the servo arm is at 0 degrees decode_results results;//decoder for the results. The 8 number sequences assigned to the buttons on the remote are defined and given certain actions.
struct color{//creating a new type of variable "byte" that is able to manipulate the three variables below into different colors by using the numbers 0 to 255 byte r; byte g; byte b; };
color c_blue = {255, 0, 0};//r,g, and b can be manipulated either individually or at the same time so they can make different colors color c_red = {0, 255, 0}; color c_green = {0, 0, 255}; color c_orange = {0, 255, 65}; color c_purple = {255, 255, 0}; color c_none = {0, 0, 0}; color c_white = {255, 255, 255}; color c_yellow = {0, 255, 80};
void changeColor(struct color rgb, float brightness){//changing the brightness of the rgb colors if(brightness >= 0.0 && brightness <= 1.0){ analogWrite(P_RED, rgb.r * brightness); analogWrite(P_GREEN, rgb.g * brightness); analogWrite(P_BLUE, rgb.b * brightness); } }
void changeColor(struct color rgb){//rgb strip is analog as opposed to digital analogWrite(P_RED, rgb.r); analogWrite(P_GREEN, rgb.g); analogWrite(P_BLUE, rgb.b); }
void setup() { Serial.begin(9600);//default for the Arduino. Initializes serial connection myservo.attach(SERV_PIN);//the servo is attached to the servo pin (pin 6) defined above irrecv.enableIRIn();// Start the receiver myservo.write(0);//start position at 0 degrees pinMode(2, OUTPUT);//pins 2, 3, 5 send out information instead of receiving information pinMode(3, OUTPUT); pinMode(5, OUTPUT); }
void loop() { if (irrecv.decode(&results)) {//decode the 8 digit sequence coming from the remote buttons to their corresponding commands if(results.value == 4294967295){//happens when holding down the positive button results.value = lastresult; } switch(results.value){ case 16754775: //the positive button moves lamp up if(pos < 90){ //postion goes no higher than 90 degrees pos += 5; //the servo arm moves in increments of 5 degrees } break; case 16769055://the negative button moves lamp down if(pos > 0){//position is greater than 0, but less than 90 pos -= 5; //servo arm moves in increments of 5 degrees } break; case 16756815://last channel button makes no color changeColor(c_none); break; case 16736925://mode button makes the color blue changeColor(c_blue); break; case 16753245://power button makes the color white changeColor(c_white); break; case 16761405://play/pause button makes the color green changeColor(c_green); break; case 16769565://mute button makes the color red changeColor(c_red); break; case 16750695://100+ button makes the color purple changeColor(c_purple); break; case 16720605://previous button makes the color orange changeColor(c_orange); break; case 16712445://next button makes the color yellow changeColor(c_yellow); break; case 16748655:// EQ button makes the rainbow changeColor(c_white); delay(1000); changeColor(c_red); delay(1000); changeColor(c_orange); delay(1000); changeColor(c_yellow); delay(1000); changeColor(c_green); delay(1000); changeColor(c_blue); delay(1000); changeColor(c_purple); delay(1000); changeColor(c_white); delay(1000); changeColor(c_none); break;
case 16738455://0 button uses white strobe light - flashes 6 times changeColor(c_white); delay(100); changeColor(c_none); delay(100); changeColor(c_white); delay(100); changeColor(c_none); delay(100); changeColor(c_white); delay(100); changeColor(c_none); delay(100); changeColor(c_white); delay(100); changeColor(c_none); delay(100); changeColor(c_white); delay(100); changeColor(c_none); delay(100); changeColor(c_white); delay(100); changeColor(c_none); delay(100); break; case 16724175://1 button does the red strobe light - flashes 6 times changeColor(c_red); delay(100); changeColor(c_none); delay(100); changeColor(c_red); delay(100); changeColor(c_none); delay(100); changeColor(c_red); delay(100); changeColor(c_none); delay(100); changeColor(c_red); delay(100); changeColor(c_none); delay(100); changeColor(c_red); delay(100); changeColor(c_none); delay(100); changeColor(c_red); delay(100); changeColor(c_none); delay(100); break; case 16718055://2 button uses orange strobe light - flashes 6 times changeColor(c_orange); delay(100); changeColor(c_none); delay(100); changeColor(c_orange); delay(100); changeColor(c_none); delay(100); changeColor(c_orange); delay(100); changeColor(c_none); delay(100); changeColor(c_orange); delay(100); changeColor(c_none); delay(100); changeColor(c_orange); delay(100); changeColor(c_none); delay(100); changeColor(c_orange); delay(100); changeColor(c_none); delay(100); break; case 16743045://3 button uses yellow strobe light - flashes 6 times changeColor(c_yellow); delay(100); changeColor(c_none); delay(100); changeColor(c_yellow); delay(100); changeColor(c_none); delay(100); changeColor(c_yellow); delay(100); changeColor(c_none); delay(100); changeColor(c_yellow); delay(100); changeColor(c_none); delay(100); changeColor(c_yellow); delay(100); changeColor(c_none); delay(100); changeColor(c_yellow); delay(100); changeColor(c_none); delay(100); break; case 16716015://4 button uses green strobe light - flashes 6 times changeColor(c_green); delay(100); changeColor(c_none); delay(100); changeColor(c_green); delay(100); changeColor(c_none); delay(100); changeColor(c_green); delay(100); changeColor(c_none); delay(100); changeColor(c_green); delay(100); changeColor(c_none); delay(100); changeColor(c_green); delay(100); changeColor(c_none); delay(100); changeColor(c_green); delay(100); changeColor(c_none); delay(100); break; case 16726215://5 button uses blue strobe light - flashes 6 times changeColor(c_blue); delay(100); changeColor(c_none); delay(100); changeColor(c_blue); delay(100); changeColor(c_none); delay(100); changeColor(c_blue); delay(100); changeColor(c_none); delay(100); changeColor(c_blue); delay(100); changeColor(c_none); delay(100); changeColor(c_blue); delay(100); changeColor(c_none); delay(100); changeColor(c_blue); delay(100); changeColor(c_none); delay(100); break; case 16734885://6 button uses purple strobe light - flashes 6 times changeColor(c_purple); delay(100); changeColor(c_none); delay(100); changeColor(c_purple); delay(100); changeColor(c_none); delay(100); changeColor(c_purple); delay(100); changeColor(c_none); delay(100); changeColor(c_purple); delay(100); changeColor(c_none); delay(100); changeColor(c_purple); delay(100); changeColor(c_none); delay(100); changeColor(c_purple); delay(100); changeColor(c_none); delay(100); break;
}<br> myservo.write(pos);//writes the position of the servo. if(results.value != 4294967295){//This happens when holding down the negative button. Returns to initial position.
lastresult = results.value; } irrecv.resume(); // Receive the next value } }