Hello,
I have been working on this project for a while and have been searching the boards for this problem. Here is what I'm trying to do. I do have to say that I am still not great with code.
I have a hand crank connected to a shaft. I'm using a photo diode and LED to read 4 stripes on the shaft as it rotates. This will be operated by hand so the RPMs will be low. I used 4 stripes to try an get a bit better response with the low RPMs.
For hardware I'm using a ATmega328 Adafruit Pro Trinket 5v which they say is equivalent to a Pro Mini. For the LEDs I'm using the dotstar product which is a 4 wire LED with separate clock and data. To control those LEDs I'm using the FastLED library which supposedly handles interrupts ok.
Here is the problem I'm having:
When I comment out the LED calls the RPM value is stable and pretty much stays within range. Usually staying under 300. When I add the LED back in the RPM value climes way up like it's just adding on top of itself and climes up to the thousands then overflows and resets. I put the code between the interrupt off and on so was hoping that would help. The LED works as desired and don't flicker at all.
Here is the code. Any help would be greatly appreciated.
Thank you.
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include <SPI.h>
#include "FastLED.h"
//Pin Info for 7 Segment LED readout
#define DATA_PIN 6
#define CLOCK_PIN 8
Adafruit_7segment matrix = Adafruit_7segment();
//Breaks LED string into 3 sections
#define NUM_LEDS_1 9
#define NUM_LEDS_2 26
#define NUM_LEDS_3 9
#define NUM_LEDS_ALL (NUM_LEDS_1 + NUM_LEDS_2 + NUM_LEDS_3)
CRGB ledsAll[NUM_LEDS_ALL];
CRGB* leds1 = ledsAll;
CRGB* leds2 = ledsAll + NUM_LEDS_1;
CRGB* leds3 = ledsAll + NUM_LEDS_1 + NUM_LEDS_2;
//Interrupt pin for photo diode
#define interruptPin 3
volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile
int rpm = 0;
unsigned long lastmillis = 0;
void setup(){
Serial.begin(19200);
matrix.begin(0x70); //Start LED
FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(ledsAll, NUM_LEDS_ALL);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), rpm_fan, FALLING);
}
void loop(){
if (millis() - lastmillis >= 1000){ /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
detachInterrupt(0); //Disable interrupt when calculating
rpm = rpmcount * 15; // 15 because of 4 pulses per rotation
rpm = constrain (rpm, 5, 255);
//if I remove section below RPM works fine
matrix.println(rpm);
matrix.writeDisplay(); //Right out RPM to led
bolt(); //Fade up middle LED Section to full
bolt1(); //if RPM higher than 200 light up
bolt3(); //if RPM higher than 150 light up
//if I remove section above RPM works fine
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
}
}
void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
rpmcount++;
}
void bolt(){
int rpm2 = rpm;
rpm2 = map (rpm2, 0, 200, 0, 255);
rpm2 = constrain (rpm2, 5, 255);
fill_solid( leds2, NUM_LEDS_2, CRGB(rpm2,rpm2,rpm2));
FastLED.show();
}
void bolt3(){
int rpm3 = rpm;
if (rpm3 >= 150)
{
fill_solid( leds3, NUM_LEDS_3, CRGB(255,255,255));
}
else
{
fill_solid( leds3, NUM_LEDS_3, CRGB(0,0,0));
}
FastLED.show();
}
void bolt1(){
int rpm1 = rpm;
if (rpm1 >= 200)
{
fill_solid( leds1, NUM_LEDS_1, CRGB(255,255,255));
}
else
{
fill_solid( leds1, NUM_LEDS_1, CRGB(0,0,0));
}
}