// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// Released under the GPLv3 license to match the rest of the
// Adafruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN_GRAL 4 // On Trinket or Gemma, suggest changing this to 1
#define PIN_24_a 16 // On Trinket or Gemma, suggest changing this to 1
#define PIN_24_b 17 // On Trinket or Gemma, suggest changing this to 1
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS_GRAL 86 // Popular NeoPixel ring size
#define NUMPIXELS_24 44 // Popular NeoPixel ring size
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels_gral(NUMPIXELS_GRAL, PIN_GRAL, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels_24_a(NUMPIXELS_24, PIN_24_a, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels_24_b(NUMPIXELS_24, PIN_24_b, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 1000 // Time (in milliseconds) to pause between pixels
//A bit
//byte display_code[11]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,0x6C};
byte display_code[11]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
void mostrar_caracter(int);
hw_timer_t *timer = NULL;
volatile SemaphoreHandle_t timerSemaphore;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
volatile uint32_t isrCounter = 0;
volatile uint32_t lastIsrAt = 0;
int INTCLOCK =32;
int INTSCLOCK1 =35;
int INTSCLOCK2 =34;
unsigned long ultima_vez_boton1 = 0;
unsigned long ultima_vez_boton2 = 0;
unsigned long ultima_vez_boton3 = 0;
const unsigned long DEBOUNCE_DELAY = 200;
int clockvalue = 12000;
int stimer1 = 0;
int stimer2 = 0;
bool running = false;
void ARDUINO_ISR_ATTR onTimer() {
// Increment the counter and set the time of ISR
portENTER_CRITICAL_ISR(&timerMux);
if (clockvalue > 0) clockvalue--;
if (stimer1 > 0) stimer1--;
else if (stimer2 > 0) stimer2--;
if (clockvalue == 0 || (stimer1 == 0 && stimer2 == 0)) {
timerStop(timer);
running = false;
}
isrCounter = isrCounter + 1;
lastIsrAt = millis();
portEXIT_CRITICAL_ISR(&timerMux);
// Give a semaphore that we can check in the loop
xSemaphoreGiveFromISR(timerSemaphore, NULL);
// It is safe to use digitalRead/Write here if you want to toggle an output
}
void ISR_clock(void){
if (millis() - ultima_vez_boton1 > DEBOUNCE_DELAY) {
if (running) {
timerStop(timer);
}
else if (stimer1 != 0 || stimer2 != 0){
timerStart(timer);
}
running = !running;
ultima_vez_boton1 = millis();
}
}
void ISR_sclock1(void){
if (millis() - ultima_vez_boton2 > DEBOUNCE_DELAY) {
if (running) {
timerStop(timer);
running = false;
}
stimer1 = 3000;
stimer2 = 0;
ultima_vez_boton2 = millis();
}
}
void ISR_sclock2(void){
if (millis() - ultima_vez_boton3 > DEBOUNCE_DELAY) {
if (running) {
timerStop(timer);
running = false;
}
stimer2 = 3000;
stimer1 = 0;
ultima_vez_boton3 = millis();
}
}
void setup() {
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.
// Create semaphore to inform us when the timer has fired
timerSemaphore = xSemaphoreCreateBinary();
// Set timer frequency to 1Mhz
timer = timerBegin(1000000);
// Attach onTimer function to our timer.
timerAttachInterrupt(timer, &onTimer);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(timer, 10000, true, 0);
timerStop(timer);
pixels_gral.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels_24_a.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels_24_b.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pinMode(INTCLOCK,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTCLOCK), ISR_clock, FALLING);
pinMode(INTSCLOCK1,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTSCLOCK1), ISR_sclock1, FALLING);
pinMode(INTSCLOCK2,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTSCLOCK2), ISR_sclock2, FALLING);
}
void loop() {
pixels_gral.clear(); // Set all pixel colors to 'off'
pixels_24_a.clear(); // Set all pixel colors to 'off'
pixels_24_b.clear(); // Set all pixel colors to 'off'
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
/*for(int i=0; i<10; i++) { // For each pixel...
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright green color:
//pixels.setPixelColor(i, pixels.Color(0, 150, 0));
//mostrar_caracter(i,255,0,0,0,255);
mostrar_4digitpluscln(i, i+1, i+2, i+3 , 2, 255, 255, 0, 0);
pixels_gral.show(); // Send the updated pixel colors to the hardware.
mostrar_2digitpluscln(i, i+1, 0, 255, 255, 0, 0, 2);
pixels_24_a.show();
mostrar_2digitpluscln(i+2, i+3, 0, 255, 255, 0, 0, 3);
pixels_24_b.show();
delay(DELAYVAL); // Pause before next pass through loop
}*/
if (clockvalue >= 6000) mostrar_4digitpluscln(clockvalue/60000, (clockvalue/6000)%1000, (clockvalue%6000)/1000, (clockvalue%1000)/100 , 2, 255, 255, 0, 0);
else mostrar_4digitpluscln((clockvalue%6000)/1000, (clockvalue%1000)/100, (clockvalue%100)/10, clockvalue%10 , 2, 255, 255, 0, 0);
pixels_gral.show(); // Send the updated pixel colors to the hardware.
if (stimer1 >= 1000) mostrar_2digit(stimer1/1000, (stimer1%1000)/100, 0, 255, 255, 0, 0, 2);
else mostrar_2digitpluscln((stimer1%1000)/100, (stimer1%100)/10, 0, 255, 255, 0, 0, 2);
pixels_24_a.show();
if (stimer2 >= 1000) mostrar_2digit(stimer2/1000, (stimer2%1000)/100, 0, 255, 255, 0, 0, 3);
else mostrar_2digitpluscln((stimer2%1000)/100, (stimer2%100)/10, 0, 255, 255, 0, 0, 3);
pixels_24_b.show();
delay(100);
}
void mostrar_2digitpluscln(int digit_1, int digit_0, int cln, int intensidad, int R, int G, int B,int tablero){
//cln 0 apagado, 1 prendido coma, 2 prendido 2 puntos
// digit_1 a 3 de 0 a 9
mostrar_caracter(digit_1, R, G, B, 0, intensidad, tablero);
mostrar_caracter(digit_0, R, G, B, 21, intensidad, tablero);
mostrar_cln(cln, R, G, B, 42, intensidad, tablero);
}
void mostrar_2digit(int digit_1, int digit_0, int cln, int intensidad, int R, int G, int B,int tablero){
//cln 0 apagado, 1 prendido coma, 2 prendido 2 puntos
// digit_1 a 3 de 0 a 9
mostrar_caracter(digit_1, R, G, B, 0, intensidad, tablero);
mostrar_caracter(digit_0, R, G, B, 21, intensidad, tablero);
}
void mostrar_4digitpluscln(int digit_3, int digit_2, int digit_1, int digit_0, int cln, int intensidad, int R, int G, int B){
//cln 0 apagado, 1 prendido coma, 2 prendido 2 puntos
// digit_1 a 3 de 0 a 9
mostrar_caracter(digit_3, R, G, B, 0, intensidad, 1);
mostrar_caracter(digit_2, R, G, B, 21, intensidad, 1 );
mostrar_caracter(digit_1, R, G, B, 42, intensidad, 1);
mostrar_caracter(digit_0, R, G, B, 63, intensidad, 1);
mostrar_cln(cln, R, G, B, 84, intensidad, 1);
}
void mostrar_cln(int dato, int R, int G, int B, int posicion, int intensidad, int tablero){
//intensidad 0 255
// cln 0,1,2
int segment_on = 0;
int set_R=0, set_G=0, set_B=0;
segment_on=1;
set_R= intensidad/255*segment_on*R;
set_G= intensidad/255*segment_on*G;
set_B= intensidad/255*segment_on*B;
actualizar_tablero(posicion, set_R, set_G, set_B, tablero);
actualizar_tablero(posicion+1, set_R, set_G, set_B,tablero);
}
void mostrar_caracter(int dato, int R, int G, int B, int posicion, int intensidad, int tablero){
//intensidad 0 255
int pixel_segment= 3;
int segment_num = 7;
int segment_on = 0;
int set_R=0, set_G=0, set_B=0;
for(int o=0; o< segment_num; o++){
for(int u=0; u<pixel_segment; u++){
segment_on=bitRead(display_code[dato], o);
set_R= intensidad/255*segment_on*R;
set_G= intensidad/255*segment_on*G;
set_B= intensidad/255*segment_on*B;
actualizar_tablero(posicion+u+o*pixel_segment, set_R, set_G, set_B, tablero);
}
}
}
void actualizar_tablero(int posicion, int R, int G, int B, int tablero){
if(tablero==1){
pixels_gral.setPixelColor(posicion, pixels_gral.Color(R,G,B));
}
if(tablero==2){
pixels_24_a.setPixelColor(posicion, pixels_24_a.Color(R,G,B));
}
if(tablero==3){
pixels_24_b.setPixelColor(posicion, pixels_24_b.Color(R,G,B));
}
}