Need help for random servo steering

Hello dear guys,

My name is Michaela and I got a (for me) difficult task to support a model project in university where I need to bring an Arduino code by tomorow. The requirements are not rocket science but I am totally new to this Arduino topic and I have no clou about programming. I read the tutorials but I dont get it done. Could you maybe correct my sketch and make it work? Would be so wonderful.

What the sketch should do in a loop:

  1. one servo should start in zero position
  2. Servo should have 4 defined positions 20 degrees, 75, 110 and full (170)
  3. Servo should move to one of these positions randomly
  4. Servo should rest in this position randomly from 20 to 50 seconds
    5)servo should move to next random position (preferred not the last one as it would not change)

I paste my code so far below, but it does not work and it is only fractions

Thanks so much for helping me,
Michaela

const int servopos = {0, 20, 50, 120, 180}; // die möglichen Positionen - Erweiterbar
// const int waitforServo = 500; // Minimum time for servo to reach position
unsigned long lastmillis; // Merker für Zeit
const unsigned long pause_min = 2000; // millisekunden
const unsigned long pause_max = pause_min + 3000;
unsigned int pause_time = pause_min; //Initialise of pause between servo moves
int old_pos = 0, new_pos = 0;

#include <VarSpeedServo.h>

#define SERVO_P2 11 // Servo connected to PIN 11
#define WAIT_I 1000 // wait after init in ms

void setup()
{
Serial.begin (115200);
randomSeed (millis());
lastmillis = servo_move (servopos[0]); // Start at Position 0
}

unsigned long servo_move (const int pos)
{
Serial.print (F (“taken over position: “));
Serial.print (pos);
Serial.println (F (” can be started”));
return millis();
}

void loop()
{
if (millis() - lastmillis > pause_time)
{
Serial.println (F (“pause over”));
while (new_pos == old_pos)
{
new_pos = random (1, sizeof (servopos) - 1);
Serial.println (F ("calculated position "));
Serial.println (new_pos);
}
Serial.print (F ("handover to Servo: "));
Serial.println (servopos[new_pos]);
lastmillis = servo_move (servopos[new_pos]);
old_pos = new_pos;
pause_time = random (pause_min, pause_max + 1);
Serial.print (F (“Wait: “));
Serial.print (pause_time);
Serial.println (F (” Millisekunden”));
}

unsigned long servo_move (const int pos)
{
  Serial.print (F ("taken over position: "));
  Serial.print (pos);
  Serial.println (F (" can be started"));
  return millis();
}

It seems strange that a function with that name does not seem to write to the servo to move it

Yes my sketch is totally wrong, can you help me to get it running? Big regards, Michaela

It's not "totally wrong" but it certainly is incomplete.

You will need to create a VarSpeedServo object and you'll need to initialise it with a .attach() in setup(). Then you'll want to actually write to it at some point with a .write().

Also

new_pos = random (1, sizeof (servopos) - 1);

will not do what you think. sizeof() an array is NOT the number of items in it but the total memory used. Your Serial prints should have shown you that.

Steve

Hi Steve, Can you try to put this together for me? I am totally lost with this task :(

Would be sooo great, Michaela :)

No I'm not going to write your university project for you. If you make some effort to apply the advice you've already had and post your best attempt then we can help you get it going but you have to do some of the work.

Steve

Also seems to be posted in the German section of the forum

Helo all,
ok, so the current sketch looks like this and servo is initialized and is moving into start position and then it should go to next random position but it does not. Currently no clou why it does not do the next step in the code…

const int servopos = {0, 20, 50, 120, 180}; // die möglichen Positionen - Erweiterbar
// const int waitforServo = 500; // Mindestzeit für den Servo, um an Position zu kommen - entfällt, weil Pausezeit Mindestzeit überschreitet
unsigned long lastmillis; // Merker für Zeit
const unsigned long pause_min = 2000; // millisekunden
const unsigned long pause_max = pause_min + 3000;
unsigned int pause_time = pause_min; //Initialisierung der Pause zwischen Servobewegungen
int old_pos = 0, new_pos = 0;

#include <Servo.h>

Servo servo;

void setup()
{
Serial.begin (115200);
randomSeed (millis());
servo.attach(11);
lastmillis = servo_bewegung (servopos[0]); // Start an Position 0
}

unsigned long servo_bewegung (const int pos)
{
Serial.print (F (“übernommene Position: “));
Serial.print (pos);
Serial.println (F (” kann angefahren werden”));
return millis();
}

void loop()
{
if (millis() - lastmillis > pause_time)
{
Serial.println (F (“Pause abgelaufen”));
while (new_pos == old_pos)
{
new_pos = random (1, sizeof (130) / sizeof (int));
Serial.println (F ("errechnete Position "));
Serial.println (new_pos);
}
Serial.print (F ("Übergebe an Servo: "));
Serial.println (servopos[new_pos]);
lastmillis = servo_bewegung (servopos[new_pos]);
old_pos = new_pos;
pause_time = random (pause_min, pause_max + 1);
Serial.print (F ("Warte jetzt für: "));
Serial.print (pause_time);
//Serial.println (F (200000));
}
// Serial.println(F(“Hier kann noch was zwischendurch gemacht werden”)); // Die Zeile auskommentieren, um zu sehen was passiert
}

To make a servo move you use servo.write(). There are no servo.write() commands anywhere in your program so the servo is just going to a default position when you attach() it and you never move it again.

Also why sizeof (130) ? Where does 130 come from?

Steve

unsigned long servo_bewegung (const int pos)
{
  Serial.print (F ("übernommene Position: "));
  Serial.print (pos);
  Serial.println (F (" kann angefahren werden"));
  return millis();
}

Bizarre.

Please remember to use code tags when posting code

so the current sketch looks like this

I still don't see where in the code a value is written to the servo See reply #1