Need help with random blinking eyes

Hi, i have attached 2 servos to robot eyes, one servo is for blinking and the other is for the eyes to look right and left. Both servos are set to operate with random movement open/close, right/left.

I have two issues, i want the eye lids to open, close and open again fast without delay. But sometimes the eye lids close for a few seconds, which makes the robot look like he is sleeping (something i dont want). Basically i want a fast blink, with the eyes always returning to being open.

My second issue is with the eyes, i want them to always start in the center. I then want the eyes to randomly turn right, then go back to center. Then randomly turn left and back to center. Because it currently just turns right to left randomly.

I am very new to this, i just started the other day.

Thank you im advance for anyone who can help.

I am using a Arduino uno R3 attached to a L293D DC Motor Control Shield Module.

Servo eyelids2 is for the (eyelids)

This is the code:

#include <Servo.h>

Servo eyes1;
Servo eyelids2;
boolean state;

void setup() {
  eyes1.attach(10);
  eyelids2.attach(9);
}
void loop() {
  state = random(2);
  if (state) eyelids2.write(101);  //close
  else eyelids2.write(87);         //open
  delay(500);
  {
    if (state) eyes1.write(170);  //right
    else eyes1.write(80);         //left
    delay(random(500));
  }
}

I have never seen a robot sleep, so I am at a loss. : )

First, remove the "image" of the code. Next, cut your code (in text) from your post and paste it into the IDE and pressing CTRL-T. Then, cut your formatted code from the IDE. Next, click the < CODE > button in the message box and paste your code where you see type or paste code here.

About your code... you are using a function called delay() which is causing your "sleeping robot." Search "arduino blink without delay" to read about using the function millis()... then give it a try yourself and post what you have learned. See you soon!

1 Like
#include <Servo.h>

Servo eyes1;
Servo eyelids2;
boolean state;

void setup() {
  eyes1.attach(10);
  eyelids2.attach(9);
}
void loop() {
  state = random(2);
  if (state) eyelids2.write(101);  //close
  else eyelids2.write(87);         //open
  delay(500);
  {
    if (state) eyes1.write(170);  //right
    else eyes1.write(80);         //left
    delay(random(500));
  }
}

I have no idea how to apply millis. I searched, tried to put the code for millis and i keep getting error messages. I have no idea where to put it or how to write it into arduino.

Show these.

What part about the "blink without delay" (using millis()) do you not understand?

All of it lol i dont know how type the code correctly. I need someone to do it for me, that way i can visually see it and understand what it means

You do not need to know how to type. The letters are on the keys.

Knowing how to code will help, but at this moment, is not imperative.

What part about "blink without delay" is someone NOT doing it for you?

I removed the delay function, but the eyes still dont open and close quick. It sometimes remains closed for like 2 seconds. Not sure why.

Is it because i have it set on random?

Servos need time to settle and your program is not allowing that.

I don't know how to fix that

Read this. Ask any question. Show your work. 40, and you won't even try.

wokwi files.

sketch.ino

#include <Servo.h>
Servo eyes1;
Servo eyelids2;

char openLeft[] = "[o  ] [o  ]";
char closeLeft[] = "[_  ] [_  ]";
char openRight[] = "[  o] [  o]";
char closeRight[] = "[  _] [  _]";
char openCenter[] = "[ o ] [ o ]";
char closeCenter[] = "[ _ ] [ _ ]";

// two timers for two events (left/right) and (open/close)
unsigned long timer1, timer2, interval1 = 1000, interval2 = 500;

void setup() {
  randomSeed(analogRead(A0)); // create pseudo randomness
  Serial.begin(115200);
  eyes1.attach(10);
  eyelids2.attach(9);
}

void loop() {
  byte state = random(3); // states: left, center, right
  if (millis() - timer1 > interval1) { // check timer
    timer1 = millis(); // reset timer
    switch (state) {
      case 0: lookLeft(); break;
      case 1: lookRight(); break;
      case 2: lookCenter(); break;
    }
  }
  delay(50); // let relays arrive
}


void lookLeft() {
  Serial.print(openLeft);
  Serial.println(" left");
  eyes1.write(10); //left
  if (!random(3)) { // one in three chances
    blinkLeft();
  }
}

void lookRight() {
  Serial.print(openRight);
  Serial.println(" right");
  eyes1.write(170); //right
  if (!random(3)) {
    blinkRight();
  }
}

void lookCenter() {
  Serial.print(openCenter);
  Serial.println(" center");
  eyes1.write(90); // center
  if (!random(3)) {
    blinkCenter();
  }
}

void blinkLeft() {
  delay(500);
  Serial.print(closeLeft);
  Serial.println(" left closed");
  eyelids2.write(101); //close
  delay(250);
  Serial.print(openLeft);
  Serial.println(" left open");
  eyelids2.write(87); //open
  delay(500);
}

void blinkRight() {
  delay(500);
  Serial.print(closeRight);
  Serial.println(" right closed");
  eyelids2.write(101); //close
  delay(250);
  Serial.print(openRight);
  Serial.println(" right open");
  eyelids2.write(87); //open
  delay(500);
}

void blinkCenter() {
  delay(500);
  Serial.print(closeCenter);
  Serial.println(" center closed");
  eyelids2.write(101); //close
  delay(250);
  Serial.print(openCenter);
  Serial.println(" center open");
  eyelids2.write(87); //open
  delay(500);
}

diagram.json

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
    { "type": "wokwi-servo", "id": "servo1", "top": -88.4, "left": 76.8, "attrs": {} },
    {
      "type": "wokwi-servo",
      "id": "servo2",
      "top": -154.6,
      "left": -55.8,
      "rotate": 270,
      "attrs": {}
    }
  ],
  "connections": [
    [ "nano:GND.1", "servo1:GND", "black", [ "v-38.4", "h-76.8", "v-76.8" ] ],
    [ "nano:GND.1", "servo2:GND", "black", [ "v-38.4", "h-124.8" ] ],
    [ "nano:5V", "servo1:V+", "red", [ "v-28.8", "h-67.2", "v-76.9" ] ],
    [ "nano:5V", "servo2:V+", "red", [ "v-28.8", "h-96.1" ] ],
    [ "nano:9", "servo1:PWM", "green", [ "v9.6", "h76.8", "v-67" ] ],
    [ "nano:10", "servo2:PWM", "green", [ "v-9.6", "h-0.2" ] ]
  ],
  "dependencies": {}
}

Blink without delay is one of the examples provided with Arduino software -run it and experiment with it , then you’ll understand .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.