Tim@Visual Micro wrote on Feb 11
th, 2015 at 11:52am:
In the sketch that you encountered the problem, is it possible your breakpoint happened in code that was not hit for a few seconds after the actual event?
I'm sorry about delay but I've been quite busy
No, I don't think that's possible if I understood your questions right. It's on the line which will be hit every third cycle.
There are only three states in this machine and it will be on the line every 10-20ms..
void loop()
{
switch(state)
{
case 0: //readPots
readPots();
break;
case 1: //rudderControl
rudderControl();
break;
case 2: //printLCD
printLCD();
break;
case 3: //readRotaryEncoder
readRotaryEncoder();
break;
}
//analogWrite(3, abs(Output));
}
void readPots() //state 0
{
currentState = 0; //nykytila
nextState = 1; //seuraava tila
PotSensorValue = analogRead(PotAnalogInPin); //luetaan ohjauspotikan arvo
ServoSensorValue = analogRead(ServoAnalogInPin); //luetaan actuattorin potikan arvo
deltaSensor = PotSensorValue-ServoSensorValue; //lasketaan potikoiden välinen erotus
if(filtering == true) //jos vaimennus on päällä (rivi 12)
{
//Vaimennus
//OUTPUT =(INPUT + OUTPUTlast * k - 1) / k
PotSensorValue = (float)(PotSensorValue + PotSensorValueWas*(PotVk-1))/PotVk;
PotSensorValueWas = PotSensorValue;
ServoSensorValue = (float)(ServoSensorValue + ServoSensorValueWas*(ServoVk-1))/ServoVk;
ServoSensorValueWas = ServoSensorValue;
}
//PotSensor: {PotSensorValue}, RudderCommand: {rudderCommand}, ServoSensor: {ServoSensorValue}, rudderPosition: {rudderPosition}
state = nextState; //asetetaan seuraava tila
}
void rudderControl() //state 1
{
currentState = 1; //nykytila
nextState = 2; //seuraava tila
if(deltaSensor > deadBand || deltaSensor < -deadBand)
{
if(deltaSensor > deadBand) //Actuator should move out
{
movingOut = true;
if(steeringActivated == true)
//if(outputValue <= 255 && steeringActivated == true)
{
outputValue = deltaSensor * sk + motorSpeedMin;
outputValue = constrain(outputValue, 0, 255); //outputValue can't be > 255
analogWrite(analogOutPin1, outputValue);
}
}
else if(deltaSensor < -deadBand) //Actuator should move in
{
movingIn = true;
if(steeringActivated == true)
//if(outputValue <= 255 && steeringActivated == true)
{
outputValue = -deltaSensor * sk + motorSpeedMin;
outputValue = constrain(outputValue, 0, 255);
analogWrite(analogOutPin2, outputValue);
}
}
}
else if(deltaSensor < deadBand || deltaSensor > -deadBand)
{
if(movingOut && deltaSensor <= 0)
{
movingOut = false;
outputValue = 0;
analogWrite(analogOutPin1, 0);
analogWrite(analogOutPin2, 0);
}
if(movingIn && deltaSensor >= 0)
{
movingIn = false;
outputValue = 0;
analogWrite(analogOutPin1, 0);
analogWrite(analogOutPin2, 0);
}
}
state = nextState; //asetetaan seuraava tila
}
void printLCD() //state 2
{
currentState = 2; //nykytila
nextState = 0; //seuraava tila
currentMillis = millis();
//lcdUpdate
if(currentMillis - previousMillisLcdUpdate >= lcdUpdateInterval)
{
previousMillisLcdUpdate = currentMillis;
// print the results to the lcd
lcd.setCursor(3,0);
lcd.print("Pot:");
lcd.setCursor(8,0);
lcd.print(PotSensorValue);
lcd.setCursor(1,2);
lcd.print("Servo:");
lcd.setCursor(8,2);
lcd.print(ServoSensorValue);
lcd.setCursor(3,3);
lcd.print("Out:");
lcd.setCursor(8,3);
lcd.print(outputValue,0);
lcd.print(" ");
}
state = nextState;
}
void readRotaryEncoder() //state 3
{
currentState = 3; //nykytila
nextState = 0; //seuraava tila
static int pos = 0;
encoder.tick();
debouncer.update();
int button = debouncer.read();
int newPos = encoder.getPosition();
if(button == false && buttonPressed == true) //nappi painettuna
{
digitalWrite(LED_PIN, HIGH);
buttonReleased = true;
buttonPressed = false;
if (cursorOn == false) cursorOn = true;
else cursorOn = false;
cursorPos++;
if (cursorPos >= 7) cursorPos = 1;
}
else if(button == true && buttonReleased == true) //nappi vapautettuna
{
digitalWrite(LED_PIN, LOW);
buttonReleased = false;
buttonPressed = true;
}
if (pos != newPos)
{
if(newPos < pos)
{
if(cursorPos == 1 && Kp <= 9) Kp = Kp + 1.0; //Kp 1.0
if(cursorPos == 2 && Kp <= 9) Kp = Kp + 0.05; //Kp 0.1
if(cursorPos == 3 && Ki <= 9) Ki = Ki + 1.0; //Ki 1.0
if(cursorPos == 4 && Ki <= 9) Ki = Ki + 0.05; //Ki 0.1
if(cursorPos == 5 && Kd <= 9) Kd = Kd + 1.0; //Kd 1.0
if(cursorPos == 6 && Kd <= 9) Kd = Kd + 0.05; //Kd 0.1
Kp = constrain(Kp, 0, 9.9);
Ki = constrain(Ki, 0, 9.9);
Kd = constrain(Kd, 0, 9.9);
}
else
{
if(cursorPos == 1 && Kp >= 0) Kp = Kp - 1.0; //Kp 1.0
if(cursorPos == 2 && Kp >= 0) Kp = Kp - 0.05; //Kp 0.1
if(cursorPos == 3 && Ki >= 0) Ki = Ki - 1.0; //Ki 1.0
if(cursorPos == 4 && Ki >= 0) Ki = Ki - 0.05; //Ki 0.1
if(cursorPos == 5 && Kd >= 0) Kd = Kd - 1.0; //Kd 1.0
if(cursorPos == 6 && Kd >= 0) Kd = Kd - 0.05; //Kd 0.1
Kp = constrain(Kp, 0, 9.9);
Ki = constrain(Ki, 0, 9.9);
Kd = constrain(Kd, 0, 9.9);
}
pos = newPos;
}
state = nextState;
}[/size]
Anyway, I've been working with debugger now in other projects and it has worked like a dream..
I must have done something stupid with that one but I can't tell what it is..