Blinking Animatronic Eye

Here is the wiring link:

I would expect problems as the motors appear to be powered with the Arduino. This is assumed as there is no power to the motors as shown.

Hello! I am trying to make a blinking eye controlled by a joystick and I have got the x and y axis but I cant get the blink to work. Here is the code:

#include <Servo.h>  

int Motor = 12;
int ButtonPush = 11;
int ServoHorizontalPin = 9;   
int ServoVerticalPin =  10;   
int HorizontalPotPin = A0;  
int VerticalPotPin = A1;  
int ServoH_Min = 0;  
int ServoH_Max = 180; 
int ServoV_Min = 0; 
int ServoV_Max = 180; 
int ButtonP_min = 0;
int ButtonP_max = 180;

Servo HorizontalServo;  
Servo VerticalServo;    
Servo Blink;

int HorizontalPotValue;         
int HorizontalServoPosition;    
int VerticalPotValue;         
int VerticalServoPosition;    
int Blinker;
int BlinkerPV;

void setup()   
{
  HorizontalServo.attach(ServoHorizontalPin);   
  VerticalServo.attach(ServoVerticalPin);         
  Blink.attach(Motor);
  pinMode(ButtonPush, HIGH);
}

void loop()  
{
  Blink.write(0);
  HorizontalPotValue  = analogRead(HorizontalPotPin); 
  VerticalPotValue  = analogRead(VerticalPotPin);  
  HorizontalServoPosition = map(HorizontalPotValue, 0, 1023, ServoH_Min , ServoH_Max); 
  VerticalServoPosition  = map(VerticalPotValue, 1023, 0, ServoH_Max , ServoH_Min);   
  HorizontalServo.write(HorizontalServoPosition);       
  VerticalServo.write(VerticalServoPosition);       
  delay(20);
  {
    if(ButtonPush == LOW) {
    Blink.write(180);   
    delay(10); 
  }
  else {
    Blink.write(0);
  }
  }
}

That is what I expected check your other post.

Auto-format (ctrl+h) in the IDE will help.

@flatgull624, please do not cross-post. Threads merged.

sorry, I'm not sure what you mean. Please can you give an example?

Welcome to the forum.

You are checking whether the pin number is LOW or HIGH. Anything not zero is HIGH, basically, so this is always false.

You need to check

  if (digitalRead(ButtonPush) == LOW) {

This is just wrong:

  pinMode(ButtonPush, HIGH);

Pin modes are INPUT, INPUT_PULLUP and OUTPUT.

INPUT is the default. Lose the pinMode line of code in setup().

Depending on how your switch is wired, you might want to do this in setup():

  pinMode(ButtonPush, INPUT_PULLUP);

It is the most common and convenient input mode.

Lastly, ButtonPush might better be called ButtonPin, to let readers know you talking about the pin, not the button.

HTH

a7

1 Like

Ok, Thank you!

sorry, I've tried but it doesn't work:

You didn't change anythings..
The stick is wired wrong..
Check here..

good luck.. ~q

Maybe you did, but the wokwi simulation you link does not appear to have benefited from my advices.

Put the code you tried into the wokwi and let's see why it does not work.

a7

It's ok, I've got it working but my servo is too slow (micro servo 9g SG90) is there a faster one?

here is the working code:

Go shopping. The specification will be something like degrees per second. Obvsly I hope, the higher that mumbled the faster the servo.

In my experience inexpensive common servos move about that fast, or slow if you think they are slow.

a7

Thank you! :slight_smile:

1 Like

:slight_smile: Will do

I have some new code to upgrade the eyes and am trying to use a servo driver board but it isnt working could you tell me what i am doing wrong? (i am uploading to arduino uno) :


 
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN  140 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  520 // this is the 'maximum' pulse length count (out of 4096)

// our servo # counter
uint8_t servonum = 0;

int xval;
int yval;

int lexpulse;
int rexpulse;

int leypulse;
int reypulse;

int uplidpulse;
int lolidpulse;
int altuplidpulse;
int altlolidpulse;

int trimval;

const int analogInPin = A0;
int sensorValue = 0;
int outputValue = 0;
int switchval = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("8 channel Servo test!");
  pinMode(analogInPin, INPUT);
  pinMode(2, INPUT);
 
  pwm.begin();
  
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  delay(10);
}

// you can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
void setServoPulse(uint8_t n, double pulse) {
  double pulselength;
  
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= 60;   // 60 Hz
  Serial.print(pulselength); Serial.println(" us per period"); 
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit"); 
  pulse *= 1000000;  // convert to us
  pulse /= pulselength;
  Serial.println(pulse);

}

void loop() {

  xval = analogRead(A1);
    lexpulse = map(xval, 0,1023, 220, 440);
    rexpulse = lexpulse;

    switchval = digitalRead(2);
    
    
  yval = analogRead(A0);
    leypulse = map(yval, 0,1023, 250, 500);
    reypulse = map(yval, 0,1023, 400, 280);

  trimval = analogRead(A2);
    trimval=map(trimval, 320, 580, -40, 40);
     uplidpulse = map(yval, 0, 1023, 400, 280);
        uplidpulse -= (trimval-40);
          uplidpulse = constrain(uplidpulse, 280, 400);
     altuplidpulse = 680-uplidpulse;

     lolidpulse = map(yval, 0, 1023, 410, 280);
       lolidpulse += (trimval/2);
         lolidpulse = constrain(lolidpulse, 280, 400);      
     altlolidpulse = 680-lolidpulse;
  
    
      pwm.setPWM(0, 0, lexpulse);
      pwm.setPWM(1, 0, leypulse);


      if (switchval == HIGH) {
      pwm.setPWM(2, 0, 400);
      pwm.setPWM(3, 0, 240);
      pwm.setPWM(4, 0, 240);
      pwm.setPWM(5, 0, 400);
      }
      else if (switchval == LOW) {
      pwm.setPWM(2, 0, uplidpulse);
      pwm.setPWM(3, 0, lolidpulse);
      pwm.setPWM(4, 0, altuplidpulse);
      pwm.setPWM(5, 0, altlolidpulse);
      }

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