I am hacking an app that allows users to doodle onto an LCD shield and the get a robot to draw it out using pens.
The robot is a 2 wheel device and to make a turn it has to rotate x degrees.
For example:
They press up 4 times, right 6 times they get this:
I fill up an array with the following. m = 1111333333
m = moves
1 = fwd
3 = right
I can then play this back by looping over the array and all seems good. The functions for fwd and right are called in sequence to drive the motors.
Now this is where I become unstuck!
Going fwd is fine, when the bot encounters a right turn it has to rotate 90 degress, ok so far. But the command set is issuing right commands because the LCD was updated in the X axis, so the bot will do 6 right truns.
I need to create some rules that look at the last and current command. So the above example would look like
1111311111 do 4 fwds, 1 right turn, 5 fwds.
I have tried to set a variable that contains the last run command, and getting the current instruction to make a decision based on that. I can't seem to figure out how to create the rule.
The 3 doesn't represent a turn, it represents movement in a different direction. The turns are represented by the change from one value to another in the array. If the user enters 5 1's and 5 3's then they want to move 5 spaces, turn, and move 5 more spaces. If you are interpreting the first 3 as a turn you will only move 4 spaces after the turn unless you work that into your logic.
I would use logic like such:
look at the element of the array that you are on, and compare it to the last element, if they are different then figure out which direction to turn (1 to 3, 3 to 1, etc), do the turn, and then process the element as forward movement.
You could also rework the drawing method to only store a 3 when there is a turn, and store 1's for the X movements, but I think that it would be more complex that way.