Hey, Im totally at a loss right now.
This code works okay, But theres a few things wrong with it..
1.I want to change between 3 states/ 3 modes of accuracy. So i set up a counter and set it up to switch accordingly. It doesnt for some reason.
- I included a last state function so the serial output doesnt repeat. Only the first one works correctly ( pulse cycle micro seconds) The other ones dont even show.
//const byte SpeedKnob_AIPin = A3;
const byte GreenLED_DOPin = 5;
const byte RedLED_DOPin = 6;
const byte UpButton_DIPin = 7;
const byte DownButton_DIPin = 8;
const byte changeAccuracy = 9;
const unsigned long PulseMicroseconds = 50; //the duration of the time the led stayes on
const unsigned long MinMicrosecondsPerCycle = 1UL; // 1000000 Pulse Per Second
const unsigned long MaxMicrosecondsPerCycle = 50000UL; // 20 Pulse Per Second
const unsigned change_LOW= 1;
const unsigned change_MID= 10; // Different levels of accuracy.
const unsigned change_HIGH = 100;
unsigned ChangePerButtonPress;
const unsigned ButtonUpdatesPerSecond = 20;
const unsigned ButtonUpdateIntervalMillis = 1000 / ButtonUpdatesPerSecond;
unsigned long PulseCycleMicroseconds = MaxMicrosecondsPerCycle;
unsigned long TimeOfCycleStart = 0;
unsigned Counter = 1;
unsigned long TimeOfLastSpeedUpdate = 0;
unsigned long lastPulse;
unsigned long lastAccuracy;
unsigned long RPM = 0;
unsigned long lastRPM;
void setup()
{
pinMode(GreenLED_DOPin, OUTPUT);
pinMode(RedLED_DOPin, OUTPUT);//as simple as that. or is it?
pinMode(UpButton_DIPin, INPUT_PULLUP);
pinMode(DownButton_DIPin, INPUT_PULLUP);
pinMode(changeAccuracy, INPUT_PULLUP);
Serial.begin(2000000);
}
void loop()
{
// GHet the current times at the start of loop()
unsigned long currenmMillis = millis();
unsigned long currentMicros = micros();
static boolean greenIsOn = false;
static boolean redPulseStarted = false;
// Is it time to check the buttons?
if (currenmMillis - TimeOfLastSpeedUpdate >= ButtonUpdateIntervalMillis)
{
TimeOfLastSpeedUpdate = currenmMillis;
boolean upPressed = digitalRead(UpButton_DIPin) == LOW;
boolean downPressed = digitalRead(DownButton_DIPin) == LOW;
if(Counter == 1){ChangePerButtonPress = change_HIGH;}
if(Counter == 2){ChangePerButtonPress = change_MID;}//switch according to counter..
if(Counter == 3){ChangePerButtonPress = change_LOW;}
if(changeAccuracy == LOW)
{
Counter + 1;
if(Counter == 4)
{
Counter = 1;//so the counter doesnt go overboard.
}
}
if (upPressed)
{
PulseCycleMicroseconds += ChangePerButtonPress;
}
if (downPressed)
{
PulseCycleMicroseconds -= ChangePerButtonPress;
}
// Keep the pulse length within limits
PulseCycleMicroseconds = constrain(PulseCycleMicroseconds, MinMicrosecondsPerCycle, MaxMicrosecondsPerCycle);
}
unsigned long cycleElapsed = currentMicros - TimeOfCycleStart;
// Now check to see if the cycle is over
if (cycleElapsed >= PulseCycleMicroseconds)
{
// Old cycle is over. Starting a new one.
TimeOfCycleStart = currentMicros;
// Starting the Green pulse
digitalWrite(GreenLED_DOPin, HIGH);
greenIsOn = true;
redPulseStarted = false;
}
// Is the Green pulse over?
if (greenIsOn && cycleElapsed >= PulseMicroseconds)
{
// Green pulse is over.
digitalWrite(GreenLED_DOPin, LOW);
greenIsOn = false;
}
// Is it time to start the Red pulse?
if (!redPulseStarted && cycleElapsed >= PulseCycleMicroseconds / 2)
{
// Start the Red pulse.
digitalWrite(RedLED_DOPin, HIGH);
redPulseStarted = true;
}
// Is the Red pulse over?
if (redPulseStarted && cycleElapsed >= (PulseCycleMicroseconds / 2) + PulseMicroseconds)
{
// Red pulse is over.
digitalWrite(RedLED_DOPin, LOW);
}
if(lastPulse != PulseCycleMicroseconds)
{
Serial.print("Pulse Time : ");
Serial.println(PulseCycleMicroseconds);
}
if(lastAccuracy != ChangePerButtonPress)
{
Serial.println("Change per button press : ");
Serial.print(ChangePerButtonPress);
}
RPM = (1/PulseCycleMicroseconds)*60;
if(RPM != lastRPM)
{
Serial.println("RPM : ");
Serial.print(RPM);
}
lastPulse = PulseCycleMicroseconds;
lastAccuracy = ChangePerButtonPress;
lastRPM = RPM;
}