New to programming, trying to make a mechanical duck for school

Hey everyone. I am working on a project for school where I have to make a mechanical duck. I am fairly new to the programming world and I need some help. I need to run three servos and a motor. One of the servos needs to move at a random time to a random angle between 0 and 180 degrees(for the head). The second needs to randomly turn to 180 degrees delay for a few seconds then turn back to 0(for raising the wings). The last one is a little bit trickier and involves the motor. I need the servo to turn from position 0 to position 180, turn the motor on for a second, turn the motor off then turn the servo back to position 0. Ill attach the code I have below.

I'm still new to coding so I'm not quite sure why its not working. I have one servo that seems to be clicking one degree every couple of seconds, and apart from that its showing no signs of working. Any help would be greatly appreciated!!!

#include <Servo.h>

Servo servo_head;
Servo servo_feathers;
Servo servo_wingpull;

int pos = 0;
int pull_feathers = 0;
int feathers_default = 0;
int timer = 0;

void setup() {
  servo_head.attach(9);
  servo_feathers.attach(10);
  servo_wingpull.attach(6);
  pinMode (8, OUTPUT);
}

void loop() {
  
int head_position = random(0,180);
servo_head.write(head_position);
int delay_time = random(1000,3000);
delay(delay_time);

   for (timer < 30; timer += random(1,2);){
    delay(1000);
    }
  
  if (timer >= 30) {
   for (pull_feathers <= 180; pull_feathers += 1;) {
    servo_feathers.write(pull_feathers);
    }
  }
  
  if (pull_feathers = 180) {
    digitalWrite(8, HIGH);
    delay(2000);
    digitalWrite(8, LOW);
    servo_feathers.write(feathers_default);
  }

  int up_or_down = random(0,1);
  if (up_or_down = 1){
  servo_wingpull.write(180);
  }  else {
  servo_wingpull.write(0);
  }
}

Duck_Code.ino (899 Bytes)

  if (pull_feathers = 180) {oops.

Please remember to use code tags when posting code

So you edited your post, and instead of simply adding in some code tags, you removed the code completely, and attached it instead, thereby removing any hope of help from members on mobile devices.

:o

Sorry for all the trouble, first time posting on the forum.

  if (pull_feathers = 180) {
...
...
  if (up_or_down = 1){

Still oops

   for (timer < 30; timer += random(1,2);)

Interesting construction. Never seen that before, not even in C++ reference.

JPAvner13:
Sorry for all the trouble, first time posting on the forum.

Then why not read the rules How to use this forum

Thank you all for your kind advice, I was able to get the code to work! For others that may be having a similar problem, here is the code I wrote:

#include <Servo.h>

Servo servo_head;
Servo servo_feathers;
Servo servo_wingpull;

int timer = 0;

void setup() {
  Serial.begin(9600);
  servo_head.attach(9);
  servo_feathers.attach(10);
  servo_wingpull.attach(6);
  pinMode (8, OUTPUT);
}

void loop() {

timer += 1;
Serial.println(timer);
  
long head_position = random(50, 130);
Serial.println ("head position: head_position");
servo_head.write(head_position);
delay(1500);
int wait_time = (3000);
Serial.println("delay: " + (wait_time));
delay(wait_time);
  
  if (timer >= 2) {
   for (int pull_feathers = 0; pull_feathers <= 180; pull_feathers += 1) {
    servo_feathers.write(pull_feathers);
    }
    delay(500);
    digitalWrite(8, HIGH);
    delay(2000);
    digitalWrite(8, LOW);
    delay(200);
    for (int reset_feathers = 180; reset_feathers >= 0; reset_feathers -= 1) {
    servo_feathers.write(reset_feathers);
    }
    delay(1500);
    timer = 1;
    Serial.println(timer);
  }


  int up_or_down = random(0,1);
  if (up_or_down = 1){
  servo_wingpull.write(180);
  delay(1500);
  servo_wingpull.write(0);
  }  else {
  servo_wingpull.write(0);
  delay(1500);
  }
}
   for (int pull_feathers = 0; pull_feathers <= 180; pull_feathers += 1) {
    servo_feathers.write(pull_feathers);
    }

You could have saved effort, and just have written

    servo_feathers.write(180);

Delta_G:

Serial.println("delay: " + (wait_time));

Does that compile? What does it actually print?

A nasty thought occurs: might the compiler treat ("delay: " + (wait_time)) as a String? Arduino corp does not share my feelings about String objects on Arduino, I am sure since section 4 of the Built In Examples is a String-fest.

AWOL:

   for (int pull_feathers = 0; pull_feathers <= 180; pull_feathers += 1) {

servo_feathers.write(pull_feathers);
    }


You could have saved effort, and just have written

servo_feathers.write(180);

OP, this is true. That loop will run faster than the servo can.

And BTW,

 byte x = 0;

...... and down in setup()....

if ( x = 1 )  Serial.println( " x is 1!" );
else          Serial.println( " x is not 1!" );

will always print x is 1!

Simple reason, ( x = 1 ) sets x to 1 and evaluates to non-zero (1 is not 0) which is true.

( x == 1 ) compares x to 1 for equality, in the example it would evaluate to false and you'd see x is not 1!

You will mix them up for a while and then you won't if you keep at coding.