I am working on a sculpture that involves servos moving randomly but speed controlled by EZ0 analog input. This sketch runs the random sequence but linking speed to the sensor is a puzzle. Any suggestions?
ifugaopapercraft:
I am working on a sculpture that involves servos moving randomly but speed controlled by EZ0 analog input. This sketch runs the random sequence but linking speed to the sensor is a puzzle. Any suggestions?
The code look as if it chooses the new position randomly, and the interval between movements randomly, and makes the movement as fast as the servo will move.
Which part of this behaviour are you aiming to have influenced by the analog input value i.e. what do you mean by 'speed'?
Yes, they move too fast but provide the kind of jerky movement necessary. When the viewer approaches the sculpture, the servos should speed up. I want to use the analog values to control this transition.
ifugaopapercraft:
Yes, they move too fast but provide the kind of jerky movement necessary. When the viewer approaches the sculpture, the servos should speed up. I want to use the analog values to control this transition.
Sorry, but that didn't answer my question.
What aspect of the behaviour do you want the sensor input to change?
Do you want it to change the interval between movements? Do you want it to control how long the servo takes to move from one position to the next? Are you intending that the servo moves abruptly (and then stops and waits until the next movement) or that the movement occur continuously during the whole interval? You need to understand exactly what behaviour you want before you can code it, and I need to understand exactly what behaviour you want before I can advise you how to code it.
The sketch is causing random movement more or less as intended. Analog input from the Maxsonar sensor should control the speed of this random movement. The closer the viewer is to the sculpture, the faster the speed. After mapping the values, I don't understand how to write the sketch so that both the random number (pos1) and analog input (sonarVal)can be passed to the motor pins (servo/yservo).
Oops! Spoke to soon. While the sketch does deliver the correct behavior, it somehow corrupts the IDE so further adjustments can't be uploaded without clearing all pins. Worse still, the serial monitor is printing illegible garbage so further tweaking of the numbers is impossible. What could be causing this? Is it choking on random numbers?
ifugaopapercraft:
Analog input from the Maxsonar sensor should control the speed of this random movement.
I'm no closer to understanding what you mean by 'speed'. Which may seem a silly point to keep harping on about, but without understanding exactly what behaviour you want it's impossible to advise how to achieve it.
Do you mean you want to control the interval between movements?
Do you mean you want to control the speed of rotation of the servo as it moves from one position to the next?
If so, do you want the servo speed to be related to the movement interval i.e. so that the servo moves continuously from one position to the next?
Or, do you still want the servo to move relatively quickly (relative to the interval between positions) so that it moves, and then stops until the next movement?
You need to be clear what you want. No doubt you understand, but we don't.
I'm pretty sure this code isn't doing what you intend:
Do you mean you want to control the speed of rotation of the servo as it moves from one position to the next?
Yes
Do you mean you want to control the interval between movements?
No
Do you mean you want to control the interval between movements?
Yes
The xservo/yservo.write creates the movement I want. For some reason, the serial monitor isn't printing correctly (lots of garbage) and I can't upload without removing all the wire from pins.
When I cut and paste these numbers in a new sketch for testing, the serial monitor prints expected values, but the serial monitor either prints nothing or garbage within the original sketch.
Do you mean you want to control the interval between movements?
No
Yes
I'm not sure why it's proving so hard to get you to say what behaviour you want. Even straight forward yes/no questions don't seem to get a straight answer.
Let's try another way. Here's what I assume you want.
The servo starts off in a random position.
After a random interval, it moves to a new position. The new position is random. The speed of movement from the old to the new position is determined by the distance returned by a sonar sensor.
Am I on the right lines? In particular, that you are not trying to relate the interval between movements with the length of time taken to complete each movement?
Do you mean you want to control the interval between movements?
Yes, as the viewer approaches the sculpture, the interval between movements will be less and the motors will move faster determined by the value of the sensor. I appreciate your help. I am honestly not trying to be evasive. An art education doesn't help much here!
ifugaopapercraft:
as the viewer approaches the sculpture, the interval between movements will be less and the motors will move faster determined by the value of the sensor.
I think we're getting closer to an understanding. Possibly the last question:
Do you want the servo to move progressively from one position to the next so that it arrives at the new position just in time to start the next movement, or do you intend that it will move to the new position at some higher speed, and then stop and wait before the next movement starts?
or do you intend that it will move to the new position at some higher speed, and then stop and wait before the next movement starts?
This option would seem to involve more start and stop which is the better choice but either would work so long as their is a transition as the viewer walks closer. Light and sound will also fade in and out.
This has become an interesting study of "verbal polarity" - the language of technology and sculpture.
Found this sketch. Runs a smooth random direction, a little different than I had envisioned but is quiet. I don't know how to link the delay intervals to the sensor values.
#include <Servo.h>
Servo myservo;
unsigned int duration = 0;
int lasttime1 = 0;
int lasttime2 = 0;
int randstart = 0;
int randend = 0;
int pos = 0;
void setup()
{
myservo.attach(10);
Serial.begin(9600);
That uses a blocking approach, which would work but would make it hard for your sketch to do anything else at the same time. You could probably make that work but, given that you will be reading the distance sensor and timing subsequent movements as well as moving the servo I think you might find a non-blocking approach works better.
It doesn't need to be exact, but have you got an idea how fast you want the servo to move over the range of distances you're going to use? Just to give us some idea of the sort of speed we're aiming for here? For example, suppose you have a 90 degree movement from one position to the next - how long do you want that to take?
Too fast will cause the piece to convulse. Let's say two seconds max.The sketch above is about right in terms of speed. There is also a question of noise with faster movement.
How about something real simple like "if" statements that would cover the entire range of values from the sensor. Probably no more than ten would suffice. Starting with the max reading, the value is 400. The delay will be "x"? 350 next.....with increments of 50 down to 100, the min distance from the sculpture.