Components and supplies
1
5 pack servo y cables
1
Arduino Pro Mini 328 - 5V/16MHz
1
ribon cable
1
high power leds
1
ws2812b
Project description
Code
Untitled fileLight controller
arduino
Controll rc lighting by radio output
1inary file (no preview
rc light controller ws2812b and led version.
arduino
1 2//rc Lighting Controller 3//For use with ws2812 and bright high powered leds. 4//jkw feb 16 2018 5//ch1 = throttle from rc radio via splitter cable. 6//ch2 = steering from rc radio via splitter cable. 7//ch3 = right side button, for lights on / off. 8#include <Breathe.h> 9Breathe Breathe; 10#include <Adafruit_NeoPixel.h> 11#include "WS2812_Definitions.h" 12#define LIBRARYUNDERTEST "PinChangeInt" 13#include <PinChangeInt.h> 14#define BAT_VOLTS_SCALE 0.00385 // 0.00785 15#define AVG_NUM 8 16#define PIN 7 // yellow wire 17#define LED_COUNT 4 18int LightsOn = 0; 19int FlasherOn = 0; 20int led1 = 6; // orange wire - Brakelights - PWM 21int led2 = 8; // brown wire Tail Headlights - non pwm 22int led3 = 9; // blue wire - left pwm 23int led4 = 10; // purple wire Right pwm 24int ch1 = 4; 25int ch2 = 3; //pin assignment input 26int ch3 = 5; //pin assignment input 27int batV = A0; // Battery measurement disabled.. voltage divider needs to be tuned. 28volatile int THROTTLE_IN_PIN = 3; 29uint32_t ulThrottleStart; 30uint32_t ulBrakeStart; 31long ulBrake; 32long ulBrakeOld; 33int dbt; //throttle 34int dbtH; 35int dbtL; 36int dbs; //steering deadband center 37int dbsH; 38int dbsL; 39int dbb; //button 40int taps = 0; 41int radio; 42long braketime = 0; 43Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800); 44volatile uint16_t unThrottleInShared; 45 46 47void setup() { //not much to see here, run once and go away... 48Serial.begin (9600); 49pinMode (PIN, OUTPUT); 50dbt = pulseIn (ch2, HIGH); 51dbtL = (dbt * .93); //if the center position isn't wide enough try adjusting 52dbtH = (dbt * 1.03); // these constants to .9 and 1.1 53dbs = pulseIn (ch1, HIGH); 54dbsL = (dbs * .93); //if the center position isn't wide enough try adjusting 55dbsH = (dbs * 1.03); // these constants to .9 and 1.1 56leds.begin(); // Call this to start up the LED leds. 57PCintPort::attachInterrupt(THROTTLE_IN_PIN, calcThrottle,CHANGE); 58pinMode(led1, OUTPUT); 59pinMode(led2, OUTPUT); 60pinMode(led3, OUTPUT); 61pinMode(led4, OUTPUT); 62digitalWrite(led2, LOW); 63} 64 65void loop() { 66readradio(); 67directionDetection(); 68Detectreverse(); 69output(); 70} 71 72void calcThrottle() 73{ 74 taps = taps + 1; 75 if (taps > 2) { 76 taps = 2; 77 } 78 if(digitalRead(THROTTLE_IN_PIN) == HIGH) 79 { 80 ulThrottleStart = micros(); 81 } 82 else 83 { 84 dbt = (uint16_t)(micros() - ulThrottleStart); 85 } 86 } 87 88void Detectreverse() { 89 if (radio != 0 && dbt <= 1410) { //wev'e gone below count up taps 90 ulBrakeStart = millis(); 91 } 92 else if (radio != 0 && dbt >= 1420) { //weve gone over count down 93 ulBrake = (ulBrake + ((uint16_t)(millis() - ulBrakeStart))); 94 braketime = (ulBrake + ulBrakeOld); 95 ulBrakeOld = ulBrake; 96 ulBrake = 0; 97 } 98 } 99 100 101void directionDetection() { 102 if ( radio == 0) { 103 Serial.println("sleeping"); 104 dbs = 1500; 105 dbt = 1500; 106 dbb = 0; 107 breathAll(); 108 } 109 if (radio != 0 && dbt <= dbtL) { 110 if (taps >= 2 && braketime < 2000) { 111 if (dbs >= dbsH) { // blink while moving forward. 112 Blink(2,3,255,215,0,50); 113 Serial.println("Backing Right "); 114 } 115 if (dbs <= dbsL) { 116 Blink(0,1,255,215,0,50); 117 Serial.println("Backing Left "); 118 } 119 Serial.println("Reversing"); 120 analogWrite(led1, 255); 121 leds.setPixelColor(0,255,0,0); 122 leds.setPixelColor(3,255,0,0); 123 124 leds.show(); 125 } 126 else if ( taps <= 1 || braketime > 2000) { 127 if (dbs >= dbsH) { // blink while moving forward. 128 Blink(2,3,255,215,0,50); 129 Serial.println("Braking Right "); 130 } 131 if (dbs <= dbsL) { 132 Blink(0,1,255,215,0,50); 133 Serial.println("Braking Left "); 134 } 135 taps = taps -1; 136 Serial.println("Braking "); 137 analogWrite(led1, 255); 138 leds.setPixelColor(0,255,0,0); 139 leds.setPixelColor(3,255,0,0); 140 leds.show(); 141 } 142 } 143 //if (radio != 0 && dbt >= 1570 && LightsOn == 0) { 144 if (radio != 0 && dbt >= 1570) { 145 int color = map(dbt, 1570, 2000, 0, 255); 146 Serial.println("Driving "); 147 if (LightsOn == 1) { 148 analogWrite(led1, 30); 149 } 150 else if (LightsOn == 0) { 151 analogWrite(led1, 0); 152 } 153 leds.setPixelColor(1,color+15,0,color); 154 leds.setPixelColor(2,color+15,0,color); 155 if (dbs >= dbsH) { // blink while moving forward. 156 Blink(2,3,255,211,0,50); 157 blinkright(); 158 Serial.println("Driving Right "); 159 } 160 if (dbs <= dbsL) { 161 Blink(0,1,255,211,0,50); 162 blinkleft(); 163 Serial.println("Driving Left "); 164 } 165 leds.show(); 166 } 167 if (radio != 0 && dbs >= dbsH) { // blinkers 168 if ( dbb > 1600 && FlasherOn == 0) { 169 LightsOn = 1; 170 } 171 else if (dbb < 1400) { 172 LightsOn = 0; 173 } 174 Blink(2,3,255,211,0,50); 175 Serial.println("Right "); 176 } 177 if (radio != 0 && dbs <= dbsL) { 178 if ( dbb > 1600 && LightsOn == 0) { 179 FlasherOn = 1; 180 } 181 else if (dbb < 1400) { 182 FlasherOn = 0; 183 } 184 Blink(0,1,255,211,0,50); 185 Serial.println("Left "); 186 } 187 if (dbb >1600) { // aux button,,, here come the cops! 188 if (LightsOn == 1 ) { 189 digitalWrite(led2, HIGH); 190 analogWrite(led1, 30); 191 analogWrite(led3, 30); 192 analogWrite(led4, 30); 193 leds.setPixelColor(1,255,255,255); 194 leds.setPixelColor(2,255,255,255); 195 leds.setPixelColor(0,60,0,0); 196 leds.setPixelColor(3,60,0,0); 197 leds.show(); 198 } 199 if (FlasherOn == 1) { 200 Sparkle(0, 0, 255, 25); 201 delay(3); 202 Sparkle(255, 0, 0, 25); 203 } 204 } 205 if (radio != 0 && dbt > dbtL && dbt < dbtH && dbs < dbsH && dbs > dbsL ) { 206 if ( LightsOn == 0 ) { 207 digitalWrite(led2, LOW); 208 analogWrite(led1, 0); 209 analogWrite(led3, 0); 210 analogWrite(led4, 0); 211 leds.setPixelColor(0,0,0,0); 212 leds.setPixelColor(1,0,0,0); 213 leds.setPixelColor(2,0,0,0); 214 leds.setPixelColor(3,0,0,0); 215 } 216 Serial.println("Stop "); 217 leds.setPixelColor(0,0,0,0); 218 leds.setPixelColor(3,0,0,0); 219 leds.show(); 220 } 221 if ( dbb < 1410 ) { // aux button off,,, ts cool now! 222 LightsOn = 0; 223 FlasherOn = 0; 224 } 225} 226 227void readradio() { 228radio = (pulseIn (3, HIGH, 50000)); // if new packet received from radio in 50 ms 25 was too fast for the mini. 229//Serial.println(radio); 230if ( radio != 0) 231{ 232dbs = pulseIn (ch1, HIGH); 233calcThrottle(); 234dbb = pulseIn (ch3, HIGH); 235delay(500); 236} 237} 238 239void Sparkle(byte red, byte green, byte blue, int SpeedDelay) { 240int Pixel = random(LED_COUNT); 241leds.setPixelColor(Pixel,red,green,blue); 242leds.show(); 243delay(SpeedDelay); 244leds.setPixelColor(Pixel,0,0,0); 245leds.show(); 246} 247 248void Blink(int Pixel, byte red, byte green, byte blue, int SpeedDelay) { 249leds.setPixelColor(Pixel,red,green,blue); 250leds.show(); 251delay(SpeedDelay); 252leds.setPixelColor(Pixel,0,0,0); 253leds.show(); 254} 255 256void output() { 257Serial.print("dbs "); 258Serial.println(dbs); 259Serial.print("dbt "); 260Serial.println(dbt); 261Serial.print("dbb "); 262Serial.println(dbb); 263} 264 265void blinkleft() { 266 analogWrite(led3, 255); 267 delay(250); 268 Serial.println("Left "); 269 if (LightsOn == 1 ) { 270 analogWrite(led3, 30); 271 } 272 else 273 analogWrite(led3, 0); 274} 275 276void blinkright() { 277 analogWrite(led4, 255); 278 delay(250); 279 Serial.println("Right "); 280 if (LightsOn == 1 ) { 281 analogWrite(led4, 30); 282 } 283 else 284 analogWrite(led4, 0); 285} 286 287void Blink(int Pixel1, int Pixel2, byte red, byte green, byte blue, int SpeedDelay) { 288leds.setPixelColor(Pixel1,red,green,blue); 289leds.setPixelColor(Pixel2,red,green,blue); 290leds.show(); 291delay(SpeedDelay); 292leds.setPixelColor(Pixel1,0,0,0); 293leds.setPixelColor(Pixel2,0,0,0); 294leds.show(); 295} 296// Sets all LEDs to off, but DOES NOT update the display; 297// call leds.show() to actually turn them off after this. 298void clearLEDs() 299{ 300 for (int i=0; i<LED_COUNT; i++) 301 { 302 leds.setPixelColor(i, 0); 303 } 304} 305 306int read_adc(int channel) { 307 308 int sum = 0; 309 int temp; 310 int i; 311 312 for (i = 0; i < AVG_NUM; i++) { // loop through reading raw adc values AVG_NUM number of times 313 temp = analogRead(channel); // read the input pin 314 sum += temp; // store sum for averaging 315 delayMicroseconds(50); // pauses for 50 microseconds 316 } 317 return (sum / AVG_NUM); // divide sum by AVG_NUM to get average and return it 318} 319 320void breathAll() { 321 for (int i; i < 225 ; i++) { 322 for(int j=0; j<leds.numPixels(); j++) { 323 leds.setPixelColor(j, leds.Color(i+15,i,0)); // yellow with a little extra red to make it warmer 324 } 325 leds.show(); 326 delay(2); 327 } 328 for (int i = 225; i > 0; i--) { 329 for(int j=0; j<leds.numPixels(); j++) { 330 leds.setPixelColor(j, leds.Color(i+15,i,0)); 331 } 332 leds.show(); 333 delay(4); 334 } 335} 336 337
Untitled fileLight controller
arduino
Controll rc lighting by radio output
1inary file (no preview
rc light controller ws2812b and led version.
arduino
1 2//rc Lighting Controller 3//For use with ws2812 and bright high 4 powered leds. 5//jkw feb 16 2018 6//ch1 = throttle from rc radio via splitter 7 cable. 8//ch2 = steering from rc radio via splitter cable. 9//ch3 = right side 10 button, for lights on / off. 11#include <Breathe.h> 12Breathe Breathe; 13#include 14 <Adafruit_NeoPixel.h> 15#include "WS2812_Definitions.h" 16#define LIBRARYUNDERTEST 17 "PinChangeInt" 18#include <PinChangeInt.h> 19#define BAT_VOLTS_SCALE 0.00385 20 // 0.00785 21#define AVG_NUM 8 22#define PIN 7 // yellow wire 23#define LED_COUNT 24 4 25int LightsOn = 0; 26int FlasherOn = 0; 27int led1 = 6; // orange wire - Brakelights 28 - PWM 29int led2 = 8; // brown wire Tail Headlights - non pwm 30int led3 = 9; 31 // blue wire - left pwm 32int led4 = 10; // purple wire Right pwm 33int ch1 = 34 4; 35int ch2 = 3; //pin assignment input 36int ch3 = 5; //pin 37 assignment input 38int batV = A0; // Battery measurement disabled.. 39 voltage divider needs to be tuned. 40volatile int THROTTLE_IN_PIN = 3; 41uint32_t 42 ulThrottleStart; 43uint32_t ulBrakeStart; 44long ulBrake; 45long ulBrakeOld; 46int 47 dbt; //throttle 48int dbtH; 49int dbtL; 50int dbs; //steering deadband center 51int 52 dbsH; 53int dbsL; 54int dbb; //button 55int taps = 0; 56int radio; 57long braketime 58 = 0; 59Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800); 60volatile 61 uint16_t unThrottleInShared; 62 63 64void setup() { //not much to see here, 65 run once and go away... 66Serial.begin (9600); 67pinMode (PIN, OUTPUT); 68dbt 69 = pulseIn (ch2, HIGH); 70dbtL = (dbt * .93); //if the center position 71 isn't wide enough try adjusting 72dbtH = (dbt * 1.03); // these constants 73 to .9 and 1.1 74dbs = pulseIn (ch1, HIGH); 75dbsL = (dbs * .93); //if 76 the center position isn't wide enough try adjusting 77dbsH = (dbs * 1.03); // 78 these constants to .9 and 1.1 79leds.begin(); // Call this to start up the LED 80 leds. 81PCintPort::attachInterrupt(THROTTLE_IN_PIN, calcThrottle,CHANGE); 82pinMode(led1, 83 OUTPUT); 84pinMode(led2, OUTPUT); 85pinMode(led3, OUTPUT); 86pinMode(led4, OUTPUT); 87digitalWrite(led2, 88 LOW); 89} 90 91void loop() { 92readradio(); 93directionDetection(); 94Detectreverse(); 95output(); 96} 97 98void 99 calcThrottle() 100{ 101 taps = taps + 1; 102 if (taps > 2) { 103 taps 104 = 2; 105 } 106 if(digitalRead(THROTTLE_IN_PIN) == HIGH) 107 { 108 ulThrottleStart 109 = micros(); 110 } 111 else 112 { 113 dbt = (uint16_t)(micros() - ulThrottleStart); 114 115 } 116 } 117 118void Detectreverse() { 119 if (radio != 0 && dbt 120 <= 1410) { //wev'e gone below count up taps 121 ulBrakeStart = millis(); 122 123 } 124 else if (radio != 0 && dbt >= 1420) { //weve gone over count down 125 126 ulBrake = (ulBrake + ((uint16_t)(millis() - ulBrakeStart))); 127 braketime 128 = (ulBrake + ulBrakeOld); 129 ulBrakeOld = ulBrake; 130 ulBrake = 0; 131 132 } 133 } 134 135 136void directionDetection() { 137 if ( radio == 0) 138 { 139 Serial.println("sleeping"); 140 dbs = 1500; 141 dbt = 1500; 142 143 dbb = 0; 144 breathAll(); 145 } 146 if (radio != 0 && dbt <= dbtL) { 147 148 if (taps >= 2 && braketime < 2000) { 149 if (dbs >= dbsH) { // blink 150 while moving forward. 151 Blink(2,3,255,215,0,50); 152 Serial.println("Backing 153 Right "); 154 } 155 if (dbs <= dbsL) { 156 Blink(0,1,255,215,0,50); 157 158 Serial.println("Backing Left "); 159 } 160 Serial.println("Reversing"); 161 162 analogWrite(led1, 255); 163 leds.setPixelColor(0,255,0,0); 164 leds.setPixelColor(3,255,0,0); 165 166 167 leds.show(); 168 } 169 else if ( taps <= 1 || braketime > 2000) { 170 171 if (dbs >= dbsH) { // blink while moving forward. 172 Blink(2,3,255,215,0,50); 173 174 Serial.println("Braking Right "); 175 } 176 if (dbs <= dbsL) { 177 178 Blink(0,1,255,215,0,50); 179 Serial.println("Braking Left "); 180 } 181 182 taps = taps -1; 183 Serial.println("Braking "); 184 analogWrite(led1, 185 255); 186 leds.setPixelColor(0,255,0,0); 187 leds.setPixelColor(3,255,0,0); 188 189 leds.show(); 190 } 191 } 192 //if (radio != 0 && dbt >= 1570 && LightsOn 193 == 0) { 194 if (radio != 0 && dbt >= 1570) { 195 int color = map(dbt, 1570, 196 2000, 0, 255); 197 Serial.println("Driving "); 198 if (LightsOn == 1) { 199 200 analogWrite(led1, 30); 201 } 202 else if (LightsOn == 0) { 203 analogWrite(led1, 204 0); 205 } 206 leds.setPixelColor(1,color+15,0,color); 207 leds.setPixelColor(2,color+15,0,color); 208 209 if (dbs >= dbsH) { // blink while moving forward. 210 Blink(2,3,255,211,0,50); 211 212 blinkright(); 213 Serial.println("Driving Right "); 214 } 215 if 216 (dbs <= dbsL) { 217 Blink(0,1,255,211,0,50); 218 blinkleft(); 219 Serial.println("Driving 220 Left "); 221 } 222 leds.show(); 223 } 224 if (radio != 0 && dbs >= dbsH) 225 { // blinkers 226 if ( dbb > 1600 && FlasherOn == 0) { 227 LightsOn 228 = 1; 229 } 230 else if (dbb < 1400) { 231 LightsOn = 0; 232 } 233 234 Blink(2,3,255,211,0,50); 235 Serial.println("Right "); 236 } 237 if 238 (radio != 0 && dbs <= dbsL) { 239 if ( dbb > 1600 && LightsOn == 0) { 240 FlasherOn 241 = 1; 242 } 243 else if (dbb < 1400) { 244 FlasherOn = 0; 245 } 246 247 Blink(0,1,255,211,0,50); 248 Serial.println("Left "); 249 } 250 if 251 (dbb >1600) { // aux button,,, here come the cops! 252 if (LightsOn == 1 ) 253 { 254 digitalWrite(led2, HIGH); 255 analogWrite(led1, 30); 256 analogWrite(led3, 257 30); 258 analogWrite(led4, 30); 259 leds.setPixelColor(1,255,255,255); 260 261 leds.setPixelColor(2,255,255,255); 262 leds.setPixelColor(0,60,0,0); 263 264 leds.setPixelColor(3,60,0,0); 265 leds.show(); 266 } 267 if (FlasherOn 268 == 1) { 269 Sparkle(0, 0, 255, 25); 270 delay(3); 271 Sparkle(255, 0, 272 0, 25); 273 } 274 } 275 if (radio != 0 && dbt > dbtL && dbt < dbtH && 276 dbs < dbsH && dbs > dbsL ) { 277 if ( LightsOn == 0 ) { 278 digitalWrite(led2, 279 LOW); 280 analogWrite(led1, 0); 281 analogWrite(led3, 0); 282 analogWrite(led4, 283 0); 284 leds.setPixelColor(0,0,0,0); 285 leds.setPixelColor(1,0,0,0); 286 287 leds.setPixelColor(2,0,0,0); 288 leds.setPixelColor(3,0,0,0); 289 } 290 291 Serial.println("Stop "); 292 leds.setPixelColor(0,0,0,0); 293 leds.setPixelColor(3,0,0,0); 294 295 leds.show(); 296 } 297 if ( dbb < 1410 ) { // aux button off,,, ts cool 298 now! 299 LightsOn = 0; 300 FlasherOn = 0; 301 } 302} 303 304void readradio() 305 { 306radio = (pulseIn (3, HIGH, 50000)); // if new packet received from radio in 307 50 ms 25 was too fast for the mini. 308//Serial.println(radio); 309if ( radio != 310 0) 311{ 312dbs = pulseIn (ch1, HIGH); 313calcThrottle(); 314dbb = pulseIn (ch3, 315 HIGH); 316delay(500); 317} 318} 319 320void Sparkle(byte red, byte green, byte blue, 321 int SpeedDelay) { 322int Pixel = random(LED_COUNT); 323leds.setPixelColor(Pixel,red,green,blue); 324leds.show(); 325delay(SpeedDelay); 326leds.setPixelColor(Pixel,0,0,0); 327leds.show(); 328} 329 330void 331 Blink(int Pixel, byte red, byte green, byte blue, int SpeedDelay) { 332leds.setPixelColor(Pixel,red,green,blue); 333leds.show(); 334delay(SpeedDelay); 335leds.setPixelColor(Pixel,0,0,0); 336leds.show(); 337} 338 339void 340 output() { 341Serial.print("dbs "); 342Serial.println(dbs); 343Serial.print("dbt 344 "); 345Serial.println(dbt); 346Serial.print("dbb "); 347Serial.println(dbb); 348} 349 350void 351 blinkleft() { 352 analogWrite(led3, 255); 353 delay(250); 354 Serial.println("Left 355 "); 356 if (LightsOn == 1 ) { 357 analogWrite(led3, 30); 358 } 359 else 360 361 analogWrite(led3, 0); 362} 363 364void blinkright() { 365 analogWrite(led4, 366 255); 367 delay(250); 368 Serial.println("Right "); 369 if (LightsOn == 1 370 ) { 371 analogWrite(led4, 30); 372 } 373 else 374 analogWrite(led4, 0); 375} 376 377void 378 Blink(int Pixel1, int Pixel2, byte red, byte green, byte blue, int SpeedDelay) { 379leds.setPixelColor(Pixel1,red,green,blue); 380leds.setPixelColor(Pixel2,red,green,blue); 381leds.show(); 382delay(SpeedDelay); 383leds.setPixelColor(Pixel1,0,0,0); 384leds.setPixelColor(Pixel2,0,0,0); 385leds.show(); 386} 387// 388 Sets all LEDs to off, but DOES NOT update the display; 389// call leds.show() to 390 actually turn them off after this. 391void clearLEDs() 392{ 393 for (int i=0; i<LED_COUNT; 394 i++) 395 { 396 leds.setPixelColor(i, 0); 397 } 398} 399 400int read_adc(int 401 channel) { 402 403 int sum = 0; 404 int temp; 405 int i; 406 407 for (i 408 = 0; i < AVG_NUM; i++) { // loop through reading raw adc values AVG_NUM 409 number of times 410 temp = analogRead(channel); // read the input pin 411 sum 412 += temp; // store sum for averaging 413 delayMicroseconds(50); 414 // pauses for 50 microseconds 415 } 416 return (sum / AVG_NUM); 417 // divide sum by AVG_NUM to get average and return it 418} 419 420void 421 breathAll() { 422 for (int i; i < 225 ; i++) { 423 for(int j=0; j<leds.numPixels(); 424 j++) { 425 leds.setPixelColor(j, leds.Color(i+15,i,0)); // yellow with a little 426 extra red to make it warmer 427 } 428 leds.show(); 429 delay(2); 430 } 431 432 for (int i = 225; i > 0; i--) { 433 for(int j=0; j<leds.numPixels(); j++) { 434 435 leds.setPixelColor(j, leds.Color(i+15,i,0)); 436 } 437 leds.show(); 438 439 delay(4); 440 } 441} 442 443
Documentation
arduino pro mini case
case for the project if you have a 3d printer.
https://www.thingiverse.com/thing:2807708
Comments
Only logged in users can leave comments