Go Down

### Topic: Looking to hire a programmer to write an Arduino Sketch for a small project (Read 3719 times)previous topic - next topic

#### OneGoal

What I meant was that you want to do something different when the time is up OR when there is something different to do (because the mode switch was pressed). Just changing rowNum is not sufficient.

Inside the else block, you'll still need to determine what to do.
Ok, got it.

Code: [Select]
` if (sensorValue >= 513)   {    moveTime = motorMoves[rowNum][mtime];    interval = moveTime;    motorMode = motorMoves[rowNum][mode];    motorPwr = motorMoves[rowNum][pwr];    motor(motorMode, motorPwr);    rowNum = 4;     }     else if (currMillis - prevMillis >= interval)   {    // update the timing values    prevMillis += interval;    moveTime = motorMoves[rowNum][mtime];    interval = moveTime;    // get the latest motor values    motorMode = motorMoves[rowNum][mode];    motorPwr = motorMoves[rowNum][pwr];    // call the motor function    motor(motorMode, motorPwr);    // update the rowNum ready for the next time    rowNum += 1;    if (rowNum > rowMax)    {      rowNum = 0;       }  }  }`

Now it reacts immediately which is what I want.

But after few tests, something happened ... while reacting, if the sensor values becomes >= 513 it reacts again which extends the reaction time (reacting while reacting) ... and this is understandable because it was told to react whenever the value is >= 513.

That brings up two curious questions :

1- Is there a way to stop it doing that?

2- Is there a way to to make it do something if something happens over a period of time?
for example :
Code: [Select]
`if (sensorValue >= 513 for more than 3 seconds){ //React }`

#### PaulS

The problem with your code now is that you are thinking that if(){}/else if() {} is equivalent to if(){if(){} else {}}.

They are not. There is point in your code where you want to do something if some condition is now true OR if what needs to be done has changed. Once you have determined that one, or both, of those conditions is true, you still need to decide which one is true.

#### OneGoal

The problem with your code now is that you are thinking that if(){}/else if() {} is equivalent to if(){if(){} else {}}.
Ok, so now I tried this

Code: [Select]
`   if(sensorValue >= 513) {       moveTime = motorMoves[rowNum][mtime];    interval = moveTime;    motorMode = motorMoves[rowNum][mode];    motorPwr = motorMoves[rowNum][pwr];    motor(motorMode, motorPwr);    rowNum = 4;    }    else {     if (currMillis - prevMillis >= interval)      {    // update the timing values    prevMillis += interval;    moveTime = motorMoves[rowNum][mtime];    interval = moveTime;    // get the latest motor values    motorMode = motorMoves[rowNum][mode];    motorPwr = motorMoves[rowNum][pwr];    // call the motor function    motor(motorMode, motorPwr);    // update the rowNum ready for the next time    rowNum += 1;    if (rowNum > rowMax)    {      rowNum = 0;       }  } }}`

and I have noticed that it is much better now than when I used the "else if", so when I interrupt it while it is reacting it carries on until the interval is over ( sometimes +2-3 seconds), but sometimes the motor gets confused for under a second and acts strange ( turning back and forth for a split of a second ) and looks like it is deciding which way to go, then it returns to normal !
I am suspecting it is the delay in the Serial port ??!!
Or is it something else??

There is point in your code where you want to do something if some condition is now true OR if what needs to be done has changed. Once you have determined that one, or both, of those conditions is true, you still need to decide which one is true.
Not sure I fully understand that !! ...
If its not too much trouble to modify this part of the code so I can compare it to what you said and try to understand it, that would be appreciated

And the second question of the previous post remains.

#### PaulS

Quote
2- Is there a way to to make it do something if something happens over a period of time?
for example :
You need to record when the event of interest starts.

Periodically, you see if the event of interest is no longer happening. If not, you clear the start time.

Periodically, you need to see if the event of interest is still happening, and, if so, whether enough time has passed. If so, do whatever needs doing.

As for the other issue, I can't keep track of what you are trying to do, and how well that is (or isn't) working. It's time for you to post some more snippets, at http://snippets-r-us.com, and summarize the problems here.

#### OneGoal

It's time for you to post some more snippets, at http://snippets-r-us.com, and summarize the problems here.
I followed the link but unfortunately I didn't understand the difference between posting here and there !
it is a different website, plus I didn't find a link to where I can post my problem there !

But whether this means the help stops here or not, I cant thank you enough for all the help and support, i have learned a lot in the last few days and started to understand some of the concepts.

Thank you very much.

#### PaulS

Quote
I followed the link but unfortunately I didn't understand the difference between posting here and there !
it is a different website, plus I didn't find a link to where I can post my problem there !
I used to refer people to that site a lot. It did not exist, which caused people to stop and think. "Hmm, maybe I need to post ALL of my code...". But, not everybody got it. So, Nick Gammon actually registered that domain, and put some stuff there to explain why posting snippets is a waste of time. It clearly says "Post all of your code". Apparently, you still don't get it.

Quote
But whether this means the help stops here or not,
It does, until you post ALL of your code.