Hello,
I need to create a code which does the following:
Check RPMs with a hall effect sensor
if RPM <= 530 , it needs to light up a led but not HIGH/LOW, but slowly trough a brightness ramp
The issue is: in the void loop, when the RPMs are low, the led does actually ramp to full brightness, but than as the void loop is run again, the led goes from 0 brightness to 255brightness while I would need it to be kept at full brightness, and once the RPMs > 530 turn the led off (without ramp, just off)
code attached via google CLICK HERE
P.S. the rpm code isn't made by me, the part i'm trying to code is the LED one but im not able to
Thanks for the help!
LarryD
February 25, 2022, 5:27pm
2
Post your code here.
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch .
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
// Calibration:
const byte PulsesPerRevolution = 2;
const unsigned long ZeroTimeout = 100000;
// Calibration for smoothing RPM:
const byte numReadings = 2;
// Variables:
volatile unsigned long LastTimeWeMeasured;
volatile unsigned long PeriodBetweenPulses = ZeroTimeout + 1000;
volatile unsigned long PeriodAverage = ZeroTimeout + 1000;
unsigned long FrequencyRaw;
unsigned long FrequencyReal;
unsigned long RPM;
unsigned int PulseCounter = 1;
unsigned long PeriodSum;
unsigned long LastTimeCycleMeasure = LastTimeWeMeasured;
unsigned long CurrentMicros = micros();
unsigned int AmountOfReadings = 1;
unsigned int ZeroDebouncingExtra;
int PWM = 9;
int brightness = 0;
int fadeAmount = 5;
int c_brightness = 0;
// Variables for smoothing tachometer:
unsigned long readings[numReadings];
unsigned long readIndex;
unsigned long total;
unsigned long average;
void setup() // Start of setup:
{
Serial.begin(9600); // Begin serial communication.
attachInterrupt(digitalPinToInterrupt(2), Pulse_Event, RISING); // Enable interruption pin 2 when going from LOW to HIGH.
pinMode(PWM, OUTPUT);
delay(1000);
} // End of setup.
void loop() // Start of loop:
{
LastTimeCycleMeasure = LastTimeWeMeasured;
CurrentMicros = micros(); // Store the micros() in a variable.
if (CurrentMicros < LastTimeCycleMeasure)
{
LastTimeCycleMeasure = CurrentMicros;
}
// Calculate the frequency:
FrequencyRaw = 10000000000 / PeriodAverage;
if (PeriodBetweenPulses > ZeroTimeout - ZeroDebouncingExtra || CurrentMicros - LastTimeCycleMeasure > ZeroTimeout - ZeroDebouncingExtra)
{
FrequencyRaw = 0;
ZeroDebouncingExtra = 2000;
}
else
{
ZeroDebouncingExtra = 0;
}
FrequencyReal = FrequencyRaw / 10000; // Get frequency without decimals.
// Calculate the RPM:
RPM = 500; /*FrequencyRaw / PulsesPerRevolution * 60;
RPM = RPM / 10000;
*/
// Smoothing RPM:
total = total - readings[readIndex];
readings[readIndex] = RPM;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings)
{
readIndex = 0;
}
// Calculate the average:
average = total / numReadings;
Serial.print("Period: ");
Serial.print(PeriodBetweenPulses);
Serial.print("\tReadings: ");
Serial.print(AmountOfReadings);
Serial.print("\tFrequency: ");
Serial.print(FrequencyReal);
Serial.print("\tRPM: ");
Serial.print(RPM);
Serial.print("\tTachometer: ");
Serial.println(average);
//THIS IS THE PART WHERE I HAVE ISSUES
if (RPM <= 530 )
{
if (c_brightness < 254) {
analogWrite(PWM, c_brightness + 50);
delay(30);
analogWrite(PWM, c_brightness + 50);
delay(30);
analogWrite(PWM, c_brightness + 50);
delay(30);
analogWrite(PWM, c_brightness + 50);
delay(30);
analogWrite(PWM, c_brightness + 55);
delay(30);
Serial.println(c_brightness);
}
else {
Serial.println("ALREADY IN RAMP");
Serial.println(c_brightness);
}
}
else {
Serial.println("RPM ARE OKAY");
}
} // End of loop.
void Pulse_Event() // The interrupt runs this to calculate the period between pulses:
{
PeriodBetweenPulses = micros() - LastTimeWeMeasured;
LastTimeWeMeasured = micros();
if (PulseCounter >= AmountOfReadings)
{
PeriodAverage = PeriodSum / AmountOfReadings;
PulseCounter = 1;
PeriodSum = PeriodBetweenPulses;
int RemapedAmountOfReadings = map(PeriodBetweenPulses, 40000, 5000, 1, 10);
RemapedAmountOfReadings = constrain(RemapedAmountOfReadings, 1, 10);
AmountOfReadings = RemapedAmountOfReadings;
}
else
{
PulseCounter++;
PeriodSum = PeriodSum + PeriodBetweenPulses;
}
} // End of Pulse_Event.
ranchifrancesco:
The issue is: in the void loop, when the RPMs are low, the led does actually ramp to full brightness, but than as the void loop is run again, the led goes from 0 brightness to 255brightness while I would need it to be kept at full brightness, and once the RPMs > 530 turn the led off (without ramp, just off)
You never set c_brightness to any value other than 0. Therefore c_brightness is always less than 254.
1 Like
kolaha
February 25, 2022, 6:55pm
6
RPM = 60000000 * PulsesPerRevolution /PeriodBetweenPulses ;
void Pulse_Event() { // The interrupt runs this to calculate the period between pulses:
LastTimeWeMeasured = micros() - LastTimeWeMeasured;
if (LastTimeWeMeasured > 0) {
PeriodBetweenPulses = LastTimeWeMeasured;
AmountOfReadings++;
}
LastTimeWeMeasured = micros();
} // End of Pulse_Event.
i made that part so that once the RPM are low, if c_brightness is less than 254 than it goes into the first if statement and makes c_brightness go from 50 to 255 and when the loop repeats, if c_brightness is equal to 255 than it doesnt repeat the part where it increases brightness
kolaha
February 26, 2022, 1:49pm
8
He said, you has no operation that change c_brightness. It will be always <254. You put same 0+50 to analogWrite many times and ones 0 + 55. So last value is 55. Never 255.
1 Like
kolaha
February 26, 2022, 1:55pm
9
And here must be analogWrite (PWM,0);
DaveX
February 26, 2022, 11:38pm
10
Maybe you meant +=
instead of +
in these:
ranchifrancesco:
analogWrite(PWM, c_brightness + 50);
delay(30);
analogWrite(PWM, c_brightness + 50);
delay(30);
analogWrite(PWM, c_brightness + 50);
delay(30);
analogWrite(PWM, c_brightness + 50);
delay(30);
analogWrite(PWM, c_brightness + 55);
delay(30);
Serial.println(c_brightness);
system
Closed
August 25, 2022, 11:38pm
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.