Im hoping someone can just help me get this right quick, its simple but im just not getting it right.
I would like the code to do the following:
If no blocks are detected increase motor speed by set amount for a set time say a second.
if no blocks are still not detected in that second stop motors.
and if blocks are detected in that second or after it has stopped reset the timer.
if (blocks)
{
int trackedBlock = TrackBlock(blocks);
FollowBlock(trackedBlock);
lastBlockTime = millis();
go = 1;
if (goFast == 1){//to reset the millis if the timer has not run out and a block is found
previousMillis = currentMillis;
}
}
else if (millis() - lastBlockTime > 40 && go == 1) //was on 70
{
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
md.setM1Brake(300);
md.setM2Brake(300);
go = 0;
goFast = 0;
}
else {
if (MotorR <= 100){
ExtraSpeedR = 0;
}
else{
ExtraSpeedR = 150;
}
if (MotorL <= 100){
ExtraSpeedL = 0;
}
else{
ExtraSpeedL = 150;
}
md.setM1Speed(MotorR + ExtraSpeedR);
md.setM2Speed(MotorL + ExtraSpeedL);
currentMillis = millis();
goFast = 1;
}
}
I can't relate your code to your question. Can you add some comments to the code so it is easier to understand what is happening (or supposed to happen).
Why are you using else if (millis() - lastBlockTime when elsewhere else you are using currentMillis
uint16_t blocks;
blocks = pixy.getBlocks();
if (blocks)//if a block is present follow it but I have noticed this is not true each time through the loop even if the block is in sight
{
int trackedBlock = TrackBlock(blocks);
FollowBlock(trackedBlock);
lastBlockTime = millis();
go = 1;
}
else if (millis() - lastBlockTime > 50 && go == 1) //if no blocks are in sight increase motor speed for a moment and then stop
{ // The go == 1 is so that this will only happen if the if blocks statement was true at least once
currentMillis = millis();
if(currentMillis - previousMillis > interval) //this must only be true if the motor speed has increased for a second
{
previousMillis = currentMillis;
md.setM1Brake(300);
md.setM2Brake(300);
Serial.println("Stop");
}
else {
Serial.println("ExtraSpeed");
md.setM1Speed(MotorR + ExtraSpeedR);
md.setM2Speed(MotorL + ExtraSpeedL);
}
}
Hey DrDiettrich
I think that might be part of the problem because the if blocks is not true through every loop.
Hey Robin
I have added some comments hope it helps a little.
The else if millis is so that if no blocks are found within that time then increase the motor speed.
The currentMillis is so that when the motor speed is increased after 1 second the motors stop.
Undermentioned:
The else if millis is so that if no blocks are found within that time then increase the motor speed.
The currentMillis is so that when the motor speed is increased after 1 second the motors stop.
Sorry, I was not clear enough. The point I wanted to make is that you should use currentMillis in both tests.
That also means that the line currentMillis = millis(); should be somewhere else. I usually put it as the first thing in loop() and that way the same value is used in all tests in a single iteration of loop(). In fact, given where you have it I wonder if it updates often enough - could that be your problem?
Your earlier statement "The above works but not always." is not very informative.
I still don't feel I know enough to make sense of what your code snippet is trying to achieve. It's a bit like seeing a wheel without knowing if it is a steered wheel or a driven wheel. It is better to post a complete program so that we can see things in the overall context.
That also means that the line currentMillis = millis(); should be somewhere else. I usually put it as the first thing in loop() and that way the same value is used in all tests in a single iteration of loop().
Thank you so much Robin, I did just that and changed a few things and got it working.