My program is supposed to calculate a RPM from a reed contact counting "ticks" and display the result "realtime" on a neopixel 120 pixels. (im going to control 2 indiviual neopixels when this is working)
My problem is that the program stops working and freezes after some seconds. It works okay until it freezes. I guess that something is overflowing but i have no idea what.
Any help appreciated - im not a programmer by heart and this is a mix of several code snippets found on the net and my own.
// read RPM and output on neopixel 120 pixels
#include <Adafruit_NeoPixel.h>
#define PIN 5
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(120, PIN, NEO_GRB + NEO_KHZ800);
volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile
int rpm = 0;
unsigned long lastmillis = 0;
unsigned int sample;
int maximum = 110;
int peak;
int dotCount;
void setup(){
Serial.begin(9600);
attachInterrupt(0, rpm1, FALLING);//interrupt zero (0) is on pin two(2).
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop()
{
rpm = rpmcount;
unsigned long startMillis= millis(); // Start of sample window
if (millis() - lastmillis == 1000){ /Update every one second, this will be equal to reading frecuency (Hz)./
int led = map(rpm, 0, maximum, 0, strip.numPixels()) -1;
for(int i; i <= led; i++)
{
int color = map(i, 0, strip.numPixels(), 0, 255);
strip.setPixelColor(i, Wheel(color));
}
for(int i = strip.numPixels() ; i > led; i--)
{
strip.setPixelColor(i, 0);
}
strip.show();
detachInterrupt(0); //Disable interrupt when calculating
rpm = rpmcount * 60; /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Update lastmill
attachInterrupt(0, rpm1, FALLING); //enable interrupt
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rpm1(){ //this code will be executed every time the interrupt 0 (pin2) gets low.
rpmcount++;
}