I have the following working code which I am running on an Arduino Uno R3, and also on an Arduino Nano, which illuminates the LEDs on an Adafruit Neopixel ring (16 LEDs) and a Neopixel Strip (8 LEDs).
At the moment, the period of the timer is adjusted by a potentiometer.
Basically I would like to modify this code so that the software detects the frequency (period) of a 5v voltage applied to a digital pin - this voltage switches on and off at a fixed frequency (around once a second), and I would like the software to transfer this detected period to my ‘interval’ value in the code. Can this be done using just one Uno or Nano, or would it require more than one? Any help would be much appreciated!
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 6
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 24
#define RINGSIZE 16
// When we setup 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 = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define MAIN_COLOR pixels.Color(255, 127, 0)
#define NEXT_COLOR_2 pixels.Color(96, 48, 0)
#define NEXT_COLOR_1 pixels.Color(48, 24, 0)
#define NEXT_COLOR_0 pixels.Color(6, 3, 0)
#define DIMMEST_COLOR pixels.Color(3, 1, 0)
#define OFF_COLOR pixels.Color(0, 0, 0)
unsigned long previousMicros=0;
unsigned long interval=20000; // the time we need to wait
int potentiometer = A0;
int potRead = 0;
bool ledState = false; // state variable for the LED
int count = 0;
i
bool anticlockwise;
bool shouldRunLEDs;
bool started;
void setup() {
// put your setup code here, to run once:
pinMode(flasherInPin, INPUT);
pinMode(rearLightInPin, INPUT);
//pinMode(brakeLightInPin, INPUT);
anticlockwise = false;
pixels.begin(); // This initializes the NeoPixel library.
for (int i=0; i<NUMPIXELS;i++){
pixels.setPixelColor(i, OFF_COLOR);
}
pixels.show();
Serial.begin(9600);
shouldRunLEDs = true;
pinMode(potentiometer,INPUT);
}
void runLEDs(){
if (shouldRunLEDs){
unsigned long currentMicros = micros(); // grab current time
if ((unsigned long)(currentMicros - previousMicros) >= interval) {
if (anticlockwise){
pixels.setPixelColor(count+5-RINGSIZE*(count+5>=RINGSIZE), MAIN_COLOR);
pixels.setPixelColor(count+5+RINGSIZE/2-RINGSIZE*(count+5+RINGSIZE/2>=RINGSIZE), MAIN_COLOR);
pixels.setPixelColor(count+4-RINGSIZE*(count+4>=RINGSIZE), NEXT_COLOR_2);
pixels.setPixelColor(count+4+RINGSIZE/2-RINGSIZE*(count+4+RINGSIZE/2>=RINGSIZE), NEXT_COLOR_2);
pixels.setPixelColor(count+3-RINGSIZE*(count+3>=RINGSIZE), NEXT_COLOR_1);
pixels.setPixelColor(count+3+RINGSIZE/2-RINGSIZE*(count+3+RINGSIZE/2>=RINGSIZE), NEXT_COLOR_1);
pixels.setPixelColor(count+2-RINGSIZE*(count+2>=RINGSIZE), NEXT_COLOR_0);
pixels.setPixelColor(count+2+RINGSIZE/2-RINGSIZE*(count+2+RINGSIZE/2>=RINGSIZE), NEXT_COLOR_0);
pixels.setPixelColor(count+1-RINGSIZE*(count+1>=RINGSIZE), DIMMEST_COLOR);
pixels.setPixelColor(count+1+RINGSIZE/2-RINGSIZE*(count+1+RINGSIZE/2>=RINGSIZE), DIMMEST_COLOR);
pixels.setPixelColor(count, OFF_COLOR);
pixels.setPixelColor(count+RINGSIZE/2-RINGSIZE*(count+RINGSIZE/2>=RINGSIZE), OFF_COLOR);
if (8-count+RINGSIZE > 15){
pixels.setPixelColor(8-count+RINGSIZE, MAIN_COLOR);
}
if (8-count+RINGSIZE+7 > 15){
pixels.setPixelColor(8-count+RINGSIZE+7, OFF_COLOR);
}
//pixels.setPixelColor(-count%16 + NUMPIXELS, MAIN_COLOR);
//pixels.setPixelColor(-count%16 + 8 + NUMPIXELS, OFF_COLOR);
//pixels.setPixelColor(-count%16 - 8 + NUMPIXELS, OFF_COLOR);
}
else
{
pixels.setPixelColor(RINGSIZE-count+5-RINGSIZE*(RINGSIZE-count+5>=RINGSIZE), OFF_COLOR);
pixels.setPixelColor(RINGSIZE-count+5+RINGSIZE/2-RINGSIZE*(RINGSIZE-count+5+RINGSIZE/2>=RINGSIZE), OFF_COLOR);
pixels.setPixelColor(RINGSIZE-count+4-RINGSIZE*(RINGSIZE-count+4>=RINGSIZE), DIMMEST_COLOR);
pixels.setPixelColor(RINGSIZE-count+4+RINGSIZE/2-RINGSIZE*(RINGSIZE-count+4+RINGSIZE/2>=RINGSIZE), DIMMEST_COLOR);
pixels.setPixelColor(RINGSIZE-count+3-RINGSIZE*(RINGSIZE-count+3>=RINGSIZE), NEXT_COLOR_0);
pixels.setPixelColor(RINGSIZE-count+3+RINGSIZE/2-RINGSIZE*(RINGSIZE-count+3+RINGSIZE/2>=RINGSIZE), NEXT_COLOR_0);
pixels.setPixelColor(RINGSIZE-count+2-RINGSIZE*(RINGSIZE-count+2>=RINGSIZE), NEXT_COLOR_1);
pixels.setPixelColor(RINGSIZE-count+2+RINGSIZE/2-RINGSIZE*(RINGSIZE-count+2+RINGSIZE/2>=RINGSIZE), NEXT_COLOR_1);
pixels.setPixelColor(RINGSIZE-count+1-RINGSIZE*(RINGSIZE-count+1>=RINGSIZE), NEXT_COLOR_2);
pixels.setPixelColor(RINGSIZE-count+1+RINGSIZE/2-RINGSIZE*(RINGSIZE-count+1+RINGSIZE/2>=RINGSIZE), NEXT_COLOR_2);
pixels.setPixelColor(RINGSIZE-count, MAIN_COLOR);
pixels.setPixelColor(RINGSIZE-count+RINGSIZE/2-RINGSIZE*(RINGSIZE-count+RINGSIZE/2>=RINGSIZE), MAIN_COLOR);
if (count+RINGSIZE > 15){
pixels.setPixelColor(count+RINGSIZE, MAIN_COLOR);
}
if (count+RINGSIZE-7 > 15){
pixels.setPixelColor(count+RINGSIZE-7, OFF_COLOR);
}
}
// pixels.show(); // This sends the updated pixel color to the hardware.
// save the "current" time
if (count < RINGSIZE-1){
count++;
previousMicros = micros();
}
else
{
count = 0;
ledState = !ledState;
}
}
}
}
void killLEDs(){
for (int n = 0; n < RINGSIZE;n++){
pixels.setPixelColor(n, OFF_COLOR);
}
}
void loop() {
// put your main code here, to run repeatedly:
potRead = analogRead(potentiometer);
interval = potRead * 60 +20000;
runLEDs();
pixels.show();
}