Components and supplies
SparkFun Transceiver Breakout - nRF24L01+
Arduino Pro Mini 328 - 5V/16MHz
Arduino Micro
Apps and platforms
Fusion 360
Arduino IDE
Project description
Code
Remote.ino
arduino
Code for the transmitter
1#include <SPI.h> 2#include <NRFLite.h> 3#include "Arduino.h" 4#include <avr/sleep.h> 5#include <avr/power.h> 6#include "RF24.h" 7 8volatile bool input = true; 9bool YTmode = true; 10long startTime; 11int lowBattery = 740; //Low battery level (740 = 3.65V) 12 13const static uint8_t RADIO_ID = 21; // Our radio's id. 14const static uint8_t DESTINATION_RADIO_ID = 7; // Id of the radio we will transmit to. 15const static uint8_t PIN_RADIO_CE = 9; 16const static uint8_t PIN_RADIO_CSN = 10; 17 18struct RadioPacket // Any packet up to 32 bytes can be sent. 19{ 20 uint8_t FromRadioId; 21 uint32_t OnTimeMillis; 22 uint32_t FailedTxCount; 23}; 24 25RF24 radio(9,10); 26NRFLite _radio; 27RadioPacket _radioData; 28 29void setup() 30{ 31 //Serial.begin(115200); 32 33 pinMode(2, INPUT); //interrupt 34 pinMode(3, INPUT); //mode Button 35 pinMode(4, INPUT); //volume down button 36 pinMode(5, INPUT); //volume up button 37 pinMode(6, INPUT); //previous button 38 pinMode(7, INPUT); //next button 39 pinMode(8, INPUT); //play button 40/* 41Radio Arduino 42CE -> 9 43CSN -> 10 (Hardware SPI SS) 44MOSI -> 11 (Hardware SPI MOSI) 45MISO -> 12 (Hardware SPI MISO) 46SCK -> 13 (Hardware SPI SCK) 47IRQ -> No connection 48VCC -> No more than 3.6 volts 49GND -> GND 50*/ 51 pinMode(14, INPUT); //rewind button 52 pinMode(15, INPUT); //skip button 53 pinMode(16,OUTPUT); //backlight LED 54 pinMode(17,OUTPUT); //blue LED 55 pinMode(18,OUTPUT); //red LED 56 57 checkBattery(); //checks battery status 58 initRadio(); //initialization of radio 59 _radioData.FromRadioId = RADIO_ID; 60 sendButton(0); 61 modeStatus(); 62 attachInterrupt(digitalPinToInterrupt(2), isr, FALLING); //Interrupt setup 63} 64 65void loop() { 66 if(input){ //only occurs during isr method which is interrupt 67 checkButtons(); 68 delay(10); 69 } 70 enterSleep(); //puts arduino to sleep to save power 71} 72 73 74void checkButtons(){ //Scans which button is pressed and sends message 75 startTime = millis(); 76 digitalWrite(16,HIGH); //backlight on 77 checkBattery(); //makes sure battery isn't low 78 modeStatus(); //Lights up the mode LEDs(youtube / media) 79 radio.powerUp(); //wakes up radio from sleep 80 int message = 0; //message will be send at the of the method 81 82 while(millis()-startTime < 3000){ //runs for 3 seconds 83 message = 0; 84 if(digitalRead(4)) { 85 delay(3); //debouncing 86 while(digitalRead(4)){} //debouncing 87 delay(3); //debouncing 88 message = 4; //sets message that is going to be send 89 startTime = millis(); //resets timer 90 } else if 91 (digitalRead(5)) { 92 delay(3); 93 while(digitalRead(5)){} 94 delay(3); 95 message = 5; 96 startTime = millis(); 97 }else if (digitalRead(6)) { 98 delay(10); 99 while(digitalRead(6)){} 100 delay(10); 101 message = 6; 102 startTime = millis(); 103 }else if (digitalRead(7)) { 104 delay(10); 105 while(digitalRead(7)){} 106 delay(10); 107 message = 7; 108 startTime = millis(); 109 } 110 else if (digitalRead(8)) { 111 delay(10); 112 while(digitalRead(8)){} 113 delay(10); 114 message = 8; 115 startTime = millis(); 116 }else if (digitalRead(14)) { 117 delay(10); 118 while(digitalRead(14)){} 119 delay(10); 120 message = 14; 121 startTime = millis(); 122 }else if (digitalRead(15)) { 123 delay(10); 124 while(digitalRead(15)){} 125 delay(10); 126 message = 15; 127 startTime = millis(); 128 }else if (digitalRead(3)) { 129 delay(10); 130 while(digitalRead(3)){} 131 delay(10); 132 changeMode(); //changes between youtube and media mode 133 startTime = millis(); 134 } 135 136 if(message == 0){} //checks if there is any message 137 else if(YTmode)sendButton(message); //in youtube mode message gets send normally 138 else sendButton(message + 100); //in media mode message is incremented by 100 139 delay(10); 140 } 141 digitalWrite(16,LOW); //turns off backlight 142 radio.powerDown(); //Puts radio to sleep 143 input =false; //prevents from looping 144} 145 146void sendButton(int buttonIndex){ //send data over radio 147 148 _radioData.OnTimeMillis = buttonIndex; 149 150 if (_radio.send(DESTINATION_RADIO_ID, &_radioData, sizeof(_radioData))) // Note how '&' must be placed in front of the variable name. 151 { 152 // Serial.println("...Success"); 153 } 154 else 155 { 156 // Serial.println("...Failed"); 157 _radioData.FailedTxCount++; 158 } 159} 160 161void isr(){ //called when interrupt happens which wakes up arduino from sleep 162 input = true; 163} 164 165void enterSleep(){ //Puts arduino to sleep to save power 166 digitalWrite(16,LOW); //Turns off all LEDs 167 digitalWrite(17,LOW); 168 digitalWrite(18,LOW); 169 set_sleep_mode(SLEEP_MODE_PWR_DOWN); 170 sleep_enable(); 171 sleep_mode(); 172 //waits until woken up 173 sleep_disable(); 174 power_all_enable(); 175} 176 177void changeMode(){ //Changes between youtube and media mode 178 delay(10); 179 while (digitalRead(3)){} 180 delay(10); 181 YTmode = !YTmode; 182 if(YTmode) { 183 digitalWrite(17,LOW); 184 digitalWrite(18,HIGH); 185 }else{ 186 digitalWrite(17,HIGH); 187 digitalWrite(18,LOW); 188 } 189} 190 191void initRadio(){ //initialization of radio 192 if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) //if radio can't be found turn on all LEDs and do nothing. 193 { 194 digitalWrite(16,HIGH); 195 digitalWrite(17,HIGH); 196 digitalWrite(18,HIGH); 197 while (1); // Wait here forever. 198 } 199} 200 201void modeStatus(){ 202 if(YTmode) { //setting the mode LED 203 digitalWrite(17,LOW); 204 digitalWrite(18,HIGH); 205 }else{ 206 digitalWrite(17,HIGH); 207 digitalWrite(18,LOW); 208 } 209} 210 211void checkBattery(){ //makes sure battery isn't too low 212 if (analogRead(A5) < lowBattery){ //if battery is too low blink couple of times 213 for(int i=0; i<=4; i++){ 214 digitalWrite(16,LOW); 215 delay(200); 216 digitalWrite(16,HIGH); 217 delay(200); 218 } 219 detachInterrupt(0); // detach interrupt and go to sleep. Arduino cannot be woken after this point. only reset will make it work 220 enterSleep(); 221 } 222} 223 224
Remote.ino
arduino
Code for the transmitter
1#include <SPI.h> 2#include <NRFLite.h> 3#include "Arduino.h" 4#include 5 <avr/sleep.h> 6#include <avr/power.h> 7#include "RF24.h" 8 9volatile bool 10 input = true; 11bool YTmode = true; 12long startTime; 13int lowBattery = 740; 14 //Low battery level (740 = 3.65V) 15 16const static uint8_t RADIO_ID = 21; 17 // Our radio's id. 18const static uint8_t DESTINATION_RADIO_ID = 7; 19 // Id of the radio we will transmit to. 20const static uint8_t PIN_RADIO_CE = 9; 21const 22 static uint8_t PIN_RADIO_CSN = 10; 23 24struct RadioPacket // Any packet up to 25 32 bytes can be sent. 26{ 27 uint8_t FromRadioId; 28 uint32_t OnTimeMillis; 29 30 uint32_t FailedTxCount; 31}; 32 33RF24 radio(9,10); 34NRFLite _radio; 35RadioPacket 36 _radioData; 37 38void setup() 39{ 40 //Serial.begin(115200); 41 42 pinMode(2, 43 INPUT); //interrupt 44 pinMode(3, INPUT); //mode Button 45 pinMode(4, 46 INPUT); //volume down button 47 pinMode(5, INPUT); //volume up button 48 49 pinMode(6, INPUT); //previous button 50 pinMode(7, INPUT); //next 51 button 52 pinMode(8, INPUT); //play button 53/* 54Radio Arduino 55CE 56 -> 9 57CSN -> 10 (Hardware SPI SS) 58MOSI -> 11 (Hardware SPI MOSI) 59MISO 60 -> 12 (Hardware SPI MISO) 61SCK -> 13 (Hardware SPI SCK) 62IRQ -> No connection 63VCC 64 -> No more than 3.6 volts 65GND -> GND 66*/ 67 pinMode(14, INPUT); //rewind 68 button 69 pinMode(15, INPUT); //skip button 70 pinMode(16,OUTPUT); //backlight 71 LED 72 pinMode(17,OUTPUT); //blue LED 73 pinMode(18,OUTPUT); //red 74 LED 75 76 checkBattery(); //checks 77 battery status 78 initRadio(); //initialization 79 of radio 80 _radioData.FromRadioId = RADIO_ID; 81 sendButton(0); 82 modeStatus(); 83 84 attachInterrupt(digitalPinToInterrupt(2), isr, FALLING); //Interrupt setup 85} 86 87void 88 loop() { 89 if(input){ //only occurs during isr method which is interrupt 90 91 checkButtons(); 92 delay(10); 93 } 94 enterSleep(); //puts 95 arduino to sleep to save power 96} 97 98 99void checkButtons(){ //Scans 100 which button is pressed and sends message 101 startTime = millis(); 102 digitalWrite(16,HIGH); 103 //backlight on 104 checkBattery(); //makes sure battery isn't low 105 106 modeStatus(); //Lights up the mode LEDs(youtube / media) 107 radio.powerUp(); 108 //wakes up radio from sleep 109 int message = 0; //message will 110 be send at the of the method 111 112 while(millis()-startTime < 3000){ //runs 113 for 3 seconds 114 message = 0; 115 if(digitalRead(4)) { 116 delay(3); 117 //debouncing 118 while(digitalRead(4)){} //debouncing 119 120 delay(3); //debouncing 121 message = 4; //sets 122 message that is going to be send 123 startTime = millis(); //resets timer 124 125 } else if 126 (digitalRead(5)) { 127 delay(3); 128 while(digitalRead(5)){} 129 130 delay(3); 131 message = 5; 132 startTime = millis(); 133 }else 134 if (digitalRead(6)) { 135 delay(10); 136 while(digitalRead(6)){} 137 delay(10); 138 139 message = 6; 140 startTime = millis(); 141 }else if (digitalRead(7)) 142 { 143 delay(10); 144 while(digitalRead(7)){} 145 delay(10); 146 message 147 = 7; 148 startTime = millis(); 149 } 150 else if (digitalRead(8)) { 151 152 delay(10); 153 while(digitalRead(8)){} 154 delay(10); 155 message 156 = 8; 157 startTime = millis(); 158 }else if (digitalRead(14)) { 159 delay(10); 160 161 while(digitalRead(14)){} 162 delay(10); 163 message = 14; 164 startTime 165 = millis(); 166 }else if (digitalRead(15)) { 167 delay(10); 168 while(digitalRead(15)){} 169 170 delay(10); 171 message = 15; 172 startTime = millis(); 173 }else 174 if (digitalRead(3)) { 175 delay(10); 176 while(digitalRead(3)){} 177 delay(10); 178 179 changeMode(); //changes between youtube and media mode 180 startTime 181 = millis(); 182 } 183 184 if(message == 0){} //checks if 185 there is any message 186 else if(YTmode)sendButton(message); //in youtube mode 187 message gets send normally 188 else sendButton(message + 100); //in media 189 mode message is incremented by 100 190 delay(10); 191 } 192 digitalWrite(16,LOW); 193 //turns off backlight 194 radio.powerDown(); //Puts 195 radio to sleep 196 input =false; //prevents from looping 197} 198 199void 200 sendButton(int buttonIndex){ //send data over radio 201 202 _radioData.OnTimeMillis 203 = buttonIndex; 204 205 if (_radio.send(DESTINATION_RADIO_ID, &_radioData, sizeof(_radioData))) 206 // Note how '&' must be placed in front of the variable name. 207 { 208 // 209 Serial.println("...Success"); 210 } 211 else 212 { 213 // Serial.println("...Failed"); 214 215 _radioData.FailedTxCount++; 216 } 217} 218 219void isr(){ //called 220 when interrupt happens which wakes up arduino from sleep 221 input = true; 222} 223 224void 225 enterSleep(){ //Puts arduino to sleep to save power 226 digitalWrite(16,LOW); 227 //Turns off all LEDs 228 digitalWrite(17,LOW); 229 digitalWrite(18,LOW); 230 231 set_sleep_mode(SLEEP_MODE_PWR_DOWN); 232 sleep_enable(); 233 sleep_mode(); 234 235 //waits until woken up 236 sleep_disable(); 237 power_all_enable(); 238} 239 240void 241 changeMode(){ //Changes between youtube and media mode 242 delay(10); 243 244 while (digitalRead(3)){} 245 delay(10); 246 YTmode = !YTmode; 247 if(YTmode) 248 { 249 digitalWrite(17,LOW); 250 digitalWrite(18,HIGH); 251 }else{ 252 digitalWrite(17,HIGH); 253 254 digitalWrite(18,LOW); 255 } 256} 257 258void initRadio(){ //initialization 259 of radio 260 if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) //if radio 261 can't be found turn on all LEDs and do nothing. 262 { 263 digitalWrite(16,HIGH); 264 265 digitalWrite(17,HIGH); 266 digitalWrite(18,HIGH); 267 while 268 (1); // Wait here forever. 269 } 270} 271 272void modeStatus(){ 273 if(YTmode) 274 { //setting the mode LED 275 digitalWrite(17,LOW); 276 277 digitalWrite(18,HIGH); 278 }else{ 279 digitalWrite(17,HIGH); 280 digitalWrite(18,LOW); 281 282 } 283} 284 285void checkBattery(){ //makes sure battery isn't 286 too low 287 if (analogRead(A5) < lowBattery){ //if battery is too low blink couple 288 of times 289 for(int i=0; i<=4; i++){ 290 digitalWrite(16,LOW); 291 delay(200); 292 293 digitalWrite(16,HIGH); 294 delay(200); 295 } 296 detachInterrupt(0); 297 // detach interrupt and go to sleep. Arduino cannot be woken after this 298 point. only reset will make it work 299 enterSleep(); 300 } 301} 302 303
Receiver.ino
arduino
Code for the receiver
1#include <SPI.h> 2#include <NRFLite.h> 3#include "HID-Project.h" 4 5 6const static uint8_t RADIO_ID = 7; // Our radio's id. The transmitter will send to this id. 7const static uint8_t PIN_RADIO_CE = 9; 8const static uint8_t PIN_RADIO_CSN = 10; 9 10struct RadioPacket // Any packet up to 32 bytes can be sent. 11{ 12 uint8_t FromRadioId; 13 uint32_t OnTimeMillis; 14 uint32_t FailedTxCount; 15}; 16 17NRFLite _radio; 18RadioPacket _radioData; 19 20void setup() 21{ 22 if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) //Checks if radio is connected 23 { 24 while (1); // Wait here forever. 25 } 26 Consumer.begin(); //initialization of keyboard. Arduino will act as HID 27 pinMode(LED_BUILTIN, OUTPUT); 28} 29 30void loop() 31{ 32 while (_radio.hasData()) //when data is received over radio 33 { 34 digitalWrite(LED_BUILTIN,HIGH); 35 _radio.readData(&_radioData); 36 int msg = _radioData.OnTimeMillis; //saves received data in msg 37 bool YTflag = true; //Determines youtube or media mode 38 if (msg > 100) { //checks if the remote is in youtube or media mode 39 YTflag = false; 40 msg -= 100; 41 } 42 switch(msg){ //Outputs appropriate key presses as keyboard 43 case 4: 44 Consumer.write(MEDIA_VOLUME_DOWN); 45 break; 46 case 5: 47 Consumer.write(MEDIA_VOLUME_UP); 48 break; 49 case 6: 50 if (YTflag){ 51 Keyboard.print("f"); 52 } 53 else Consumer.write(MEDIA_PREVIOUS); 54 break; 55 case 7: 56 if (YTflag ){ 57 Keyboard.press(KEY_LEFT_SHIFT); 58 Keyboard.press('n'); 59 delay(50); 60 Keyboard.releaseAll(); 61 } 62 else Consumer.write(MEDIA_NEXT); 63 break; 64 case 8: 65 if (YTflag) Keyboard.print("k"); 66 else { 67 //Consumer.write(MEDIA_PLAY_PAUSE); 68 Keyboard.print(" "); 69 } 70 break; 71 case 14: 72 if (YTflag) Keyboard.print("j"); 73 else Consumer.write(MEDIA_REWIND); 74 break; 75 case 15: 76 if (YTflag) Keyboard.print("l"); 77 else Consumer.write(MEDIA_FAST_FORWARD); 78 break; 79 } 80 digitalWrite(LED_BUILTIN,LOW); 81 } 82} 83
Downloadable files
Recevier
Schematic for the receiver. Fritzing file
Recevier
Transmitter
Schematic for the transmitter. Fritzing file
Transmitter
Recevier
Schematic for the receiver. Fritzing file
Recevier
Transmitter
Schematic for the transmitter. Fritzing file
Transmitter
Documentation
3D models
STL files of all parts
3D models
3D models
STL files of all parts
3D models
Comments
Only logged in users can leave comments
Gyro1
0 Followers
•0 Projects
Table of contents
Intro
40
0