Created
May 23, 2019 19:11
-
-
Save gilliangoud/e9a7ed393e4e42aad79529fd0f2bb01e to your computer and use it in GitHub Desktop.
Arduino file for a 2 digit remote controlled lap counter with a nrf24L
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int NUM_DISPLAYS = 2; | |
const int NUM_LEDS_SEGMENT = 7; | |
const int DATA_PIN = 2; | |
#include <FastLED.h> | |
const int NUM_SEGMENTS = 7; | |
const int NUM_LEDS = NUM_DISPLAYS * NUM_LEDS_SEGMENT * NUM_SEGMENTS; | |
CRGB leds[NUM_LEDS]; | |
#include "nRF24L01.h" // NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24 | |
#include "RF24.h" | |
#include "SPI.h" | |
RF24 radio(8,7); // NRF24L01 used SPI pins + Pin 9 and 10 on the UNO | |
const uint64_t pipe = 0xE6E6E6E6E6E6; // Needs to be the same for communicating between 2 NRF24L01 | |
int number = 1; | |
void setup() { | |
//Serial.begin(9600); | |
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); | |
radio.begin(); // Start the NRF24L01 | |
radio.openReadingPipe(1,pipe); // Get NRF24L01 ready to receive | |
radio.startListening(); // Listen to see if information received | |
radio.setPALevel(RF24_PA_HIGH); // RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX, RF24_PA_ERROR | |
showDigit(number, CRGB::Red); | |
} | |
void loop() { | |
// for(int i = 0; i <= 29; i++){ | |
// showDigit(i, CRGB( 10, 0, 0)); | |
// delay(1000); | |
// } | |
while (radio.available()){ | |
radio.read(&number, sizeof(int)); // Read information from the NRF24L01 | |
//Serial.print(number); | |
// showDigit(number, CRGB( 10, 0, 0)); | |
showDigit(number, CRGB::Red); | |
} | |
} | |
void showDigit(int digit, CRGB color){ | |
// set everything to black so previous digits wont remain | |
setAllLeds(CRGB::Black); | |
// get the last number, setting all the numbers right to left | |
for(int i = NUM_DISPLAYS; i > 0; i--){ | |
int temp = digit; | |
if(i == 1){ | |
temp = (digit <= 9)? -1 :digit / 10; | |
} | |
temp = temp % 10; | |
setDigit(i, temp, color); | |
} | |
FastLED.show(); | |
} | |
void setDigit(int displayNum, int digit, CRGB color){ | |
int segments1[] = {3,4}; | |
int segments2[] = {2,3,7,6,5}; | |
int segments3[] = {2,3,7,4,5}; | |
int segments4[] = {1,7,3,4}; | |
int segments5[] = {2,1,7,4,5}; | |
int segments6[] = {1,2,4,7,6,5}; | |
int segments7[] = {2,3,4}; | |
int segments8[] = {1,2,3,4,5,6,7}; | |
int segments9[] = {1,2,3,4,5,7}; | |
int segments0[] = {1,2,3,4,5,6}; | |
switch(digit){ | |
case 1: | |
setSegmentColorWithArray(displayNum, segments1, 2, color); | |
break; | |
case 2: | |
setSegmentColorWithArray(displayNum, segments2, 5, color); | |
break; | |
case 3: | |
setSegmentColorWithArray(displayNum, segments3, 5, color); | |
break; | |
case 4: | |
setSegmentColorWithArray(displayNum, segments4, 4, color); | |
break; | |
case 5: | |
setSegmentColorWithArray(displayNum, segments5, 5, color); | |
break; | |
case 6: | |
setSegmentColorWithArray(displayNum, segments6, 6, color); | |
break; | |
case 7: | |
setSegmentColorWithArray(displayNum, segments7, 3, color); | |
break; | |
case 8: | |
setSegmentColorWithArray(displayNum, segments8, 7, color); | |
case 9: | |
setSegmentColorWithArray(displayNum, segments9, 6, color); | |
break; | |
case 0: | |
setSegmentColorWithArray(displayNum, segments0, 6, color); | |
break; | |
default: | |
setSegmentColorWithArray(displayNum, segments8, 7, CRGB::Black); | |
} | |
} | |
void setSegmentColorWithArray(int displayNum, int segments[], int sizeOfArray, CRGB color){ | |
for(int i = 0; i < sizeOfArray; i++){ | |
setSegmentColor(segments[i], displayNum, color); | |
} | |
} | |
void setSegmentColor(int segment, int displayNum, CRGB color){ | |
segment--; | |
int startSegment = segment * NUM_LEDS_SEGMENT; | |
if (displayNum > 1){ | |
startSegment = (segment + NUM_SEGMENTS) * NUM_LEDS_SEGMENT; | |
} | |
for(int i = startSegment; i < startSegment + NUM_LEDS_SEGMENT; i++){ | |
leds[i] = color; | |
} | |
} | |
void setAllLeds(CRGB color){ | |
for(int i = 0; i < NUM_LEDS; i++){ | |
leds[i] = color; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment