No experimento de hoje vamos controlar uma fita led RGB através de um módulo muito simples e fácil de encontrar; o Módulo bluetooth HC06.
Que tal então deixar o ambiente mais aconchegante, preparado para assistir um filme, iluminado etc.., tudo sem precisar sair do lugar usando o smartphone e seu bluetooth.
Veja abaixo a lista de materiais necessários para este experimento:
Veja o esquema de como serão as ligações:
O código está disponível abaixo para você copiar e enviar para seu arduino.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <SoftwareSerial.h> #include <Wire.h> SoftwareSerial mySerial(7,8); // RX e TX pins int PIN_RED = 10; int PIN_GREEN = 11; int PIN_BLUE = 9; String RGB = ""; String RGB_Previous = "255.255.255"; String ON = "ON"; String OFF = "OFF"; boolean RGB_Completed = false; void setup() { pinMode (PIN_RED, OUTPUT); pinMode (PIN_GREEN, OUTPUT); pinMode (PIN_BLUE, OUTPUT); Serial.begin(9600); mySerial.begin(9600); RGB.reserve(30); } void loop() { while(mySerial.available()){ char ReadChar = (char)mySerial.read(); if(ReadChar == ')'){ RGB_Completed = true; }else{ RGB += ReadChar; } } if(RGB_Completed){ Serial.print("RGB:"); Serial.print(RGB); Serial.print(" PreRGB:"); Serial.println(RGB_Previous); if(RGB==ON){ RGB = RGB_Previous; Light_RGB_LED(); }else if(RGB==OFF){ RGB = "0.0.0"; Light_RGB_LED(); }else{ Light_RGB_LED(); RGB_Previous = RGB; } RGB = ""; RGB_Completed = false; } } void Light_RGB_LED(){ int SP1 = RGB.indexOf(' '); int SP2 = RGB.indexOf(' ', SP1+1); int SP3 = RGB.indexOf(' ', SP2+1); String R = RGB.substring(0, SP1); String G = RGB.substring(SP1+1, SP2); String B = RGB.substring(SP2+1, SP3); Serial.print("R="); Serial.println( constrain(R.toInt(),0,255)); Serial.print("G="); Serial.println(constrain(G.toInt(),0,255)); Serial.print("B="); Serial.println( constrain(B.toInt(),0,255)); analogWrite(PIN_RED, (R.toInt())); analogWrite(PIN_GREEN, (G.toInt())); analogWrite(PIN_BLUE, (B.toInt())); } |
Curta nossa página no Facebook: https://www.facebook.com/experimentosdegaragem/
Se inscreva em nosso canal: https://www.youtube.com/c/ExperimentosdeGaragem?sub_confirmation=1