After a very long time experimenting with different ‘if’ statements and trying to figure out the best solution to the problem of the sensor and motor being very slow and jittery, I finally came to an outcome decision which I am happy with. I started off by creating different ‘if’ statements for a range of distances, this was still giving out very random results and crashing quite often. I then tried an ‘if else’ statement, to declare that if nothing is detected in the first range, then check the second range, and if nothing is detected in the second range then it must be in the third range. This still did not give out reliable results. So I went back to doing normal ‘if’ statements and tweaked my code, after many different attempts I found a code that worked; In my loop, I created three ‘if’ statements, for a different output speeds for the motor, depending on how close or far away someone is, in a range of 0-400cm.
The first statement has an output speed of 25 when someone is within 200-400cm away. The second statement has an output speed of 150 when someone is within 100-200cm away. And when someone is as close as 100cm or less, the output speed is 200. The video below is an example of the two extremes, firstly when my hand is close, the motor spins fast, and when I move my hand away it jumps to the slowest speed as the background was between 200-400cm away.
The next video shows the three stages of speeds – starting with the fastest close up, then the middle speed and finally the slowest speed. It is a bit unreliable as the ping sensor sometimes throws out some random information, as I can see in the serial monitor, but overall it works and gives the effect I was hoping for – that if someone is closer it spins faster and if someone is far away or no one is there it spins very slowly.
This is my working prototype, so my next stage is to construct my installation and test it in the public space.