Btw: Mode "impulsiveControl" works fine.
#######################################################################################################
// +++++++++ PINs +++++++
const int m_iPinMotor1 = 5;
const int m_iPinMotor2 = 9;
const int m_iPinLedMotor1 = 8;//++++
const int m_iPinLedMotor2 = 11;//++++
const int m_iPinButton1 = 3;//InterruptPIN
const int m_iPinButton2 = 2;//InterruptPIN
// +++++++++ Potis +++++++
int m_iValuePoti1 = 0;
int m_iValuePoti2 = 0;
volatile float m_fPwmPoti1 = 0.0;
volatile float m_fPwmPoti2 = 0.0;
// +++++++++ Systemstate +++++++
int m_iMode = 0;
int m_iModeMotor2 = 0; //Modus fuer Motor 2 (off-on-async)
long m_lLastCycleTime = 0;
// +++++++++ Systemeigenschaften +++++++
const int m_iNumberOfModes = 2;
const int m_iNumberOfMotor2Modes = 3;
volatile long lastDebounceTime = 0; // the last time the interrupt was triggered
volatile long lastDebounceTimeB1 = 0; // the last time the interrupt was triggered
void setup() {
pinMode(m_iPinButton1, INPUT);
pinMode(m_iPinButton2, INPUT_PULLUP);
pinMode(m_iPinMotor1, OUTPUT);
pinMode(m_iPinMotor2, OUTPUT);
pinMode(m_iPinLedMotor1, OUTPUT);
pinMode(m_iPinLedMotor2, OUTPUT);
attachInterrupt(0, setm_iModeMotor2, FALLING);
attachInterrupt(1, setMode, FALLING);
m_lLastCycleTime = millis();
}
// the loop routine runs over and over again forever:
void loop() {
m_iValuePoti1 = analogRead(A1);
m_fPwmPoti1 = (float) m_iValuePoti1/1023;
m_iValuePoti2 = analogRead(A2);
m_fPwmPoti2 = (float) m_iValuePoti2/1023;
setSystemValues();
long currentTime = millis();
if(m_iMode == 1) //Analog/Smooth
{
analogWrite(m_iPinMotor1, m_fPwmPoti1255);
analogWrite(m_iPinMotor2, m_fPwmPoti2255);
}
else if (m_iMode == 2) //Impulsiv
{
impulsiveControl();
}
else
{
digitalWrite(m_iPinMotor1, LOW);
digitalWrite(m_iPinMotor2, LOW);
digitalWrite(m_iPinLedMotor1, LOW);
digitalWrite(m_iPinLedMotor2, LOW);
}
delay(1); // delay in between reads for stability
}
void impulsiveControl()
{
digitalWrite(m_iPinMotor1, HIGH);
digitalWrite(m_iPinMotor2, HIGH);
delay((m_fPwmPoti1*500));
digitalWrite(m_iPinMotor1, LOW);
digitalWrite(m_iPinMotor2, LOW);
delay((1-m_fPwmPoti1)*500);
}
void setMode()
{
long currentTime = millis();
if ((currentTime - lastDebounceTimeB1) > 450)
{
lastDebounceTimeB1 = currentTime;
m_iMode++;
if(m_iMode > m_iNumberOfModes) m_iMode = 0;
}
}
void setm_iModeMotor2()
{
long currentTime = millis();
if ((currentTime - lastDebounceTime) > 450)
{
lastDebounceTime = currentTime;
m_iModeMotor2++;
if(m_iModeMotor2 > m_iNumberOfMotor2Modes) m_iModeMotor2 = 0;
}
}