Arduino Forum

Topics => Robotics => Topic started by: Brainfart14 on Sep 05, 2018, 01:59 am

Title: Eyeball Construction
Post by: Brainfart14 on Sep 05, 2018, 01:59 am
Hello! So I'm trying to create a prop for my Halloween costume this year. I want to make a "Magic Book" that's been made from dragon skin, and have one of the dragon's eyes looking around on the cover (example image provided). I've bought most of the materials I need; but now it's time to get down to business: creating the code! My goal is to have the eye- controlled by 2 servos- look up, down, left and right, while an eyelid, controlled by a 3rd servo, blink on occasion.
I'm still a relatively new hobbyist in the world of Arduino, so the sketch I've written up is not without its flaws. I'd love for someone to offer some advice/guidance on this project. I've provided the code I've put together so far. Please, feel free to make comments (constructive ones) and corrections! Any assistance is greatly appreciated!
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 05, 2018, 03:54 am
https://forum.arduino.cc/index.php?topic=563357 (https://forum.arduino.cc/index.php?topic=563357)

https://forum.arduino.cc/index.php?topic=481947 (https://forum.arduino.cc/index.php?topic=481947)

https://forum.arduino.cc/index.php?topic=478499 (https://forum.arduino.cc/index.php?topic=478499)

Is there anything new in particular you were wanting to ask? 
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 05, 2018, 04:54 pm
Yes I rewrote the sketch to make it more like a basic sweep sketch but it's still not moving in the way that I'd hoped. Now just a clarification, this current sketch isn't the final one, I just wanted to see if I was on the right path. However, it would appear that I'm far from it. When I upload it to my arduino, I end up with a very dull left right, THEN up down eye. I don't want I generic, robotic side to side movements; I'm trying to get them to work in unison. I'm not worried about the blink right now. Another problem for another day. Right now I'm focused on getting more natural-looking movement from the eyeball.
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 05, 2018, 06:35 pm
OP's code:

Code: [Select]
#include <Servo.h>
Servo hor;  // horizontal servo
Servo vert; // vertical servo
Servo lid;  //eyelid servo

int hori = 0; //variable for horizontal servo pos
int verti = 0; //variable for vertical servo pos
int lidi = 0; //variable for eyelid servo pos

void setup() {
vert.attach(9); //attach to pin 9
hor.attach(10); //attach to pin 10
lid.attach(11); //attach to pin 11

}

void loop() {
  for(pos=0; pos<180; pos+=2)
  {hor.write(hori);
  vert.write(verti);
  delay(10);
  }
for(pos=0; pos<180; pos=+1)
 {lid.write(lidi);
 delay(15);
 }
 for(pos=180; pos>=1; pos-=2)
 {hor.write(hori);
 vert.write(verti);
 delay(19);
 }
}
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 05, 2018, 06:48 pm
You seem to have some sort of misunderstanding about for loops:

Code: [Select]
for(pos=0; pos<180; pos+=2)
  {hor.write(hori);
  vert.write(verti);
  delay(10);
  }


Here you are incrementing the pos variable, but not using it.  And you are writing the same unchanging value ( hori and verti) to the servo over and over.  So this isn't sweeping anything.  It's just writing the same values over and over.  If you want to make it sweep then you need the for loop to work on hori and verti instead of pos.

I'm not sure what you actually want that to do, but something like this:

Code: [Select]
for(hori = 0, verti=0; hori<180; hori+=2, verti+=2)
  {hor.write(hori);
  vert.write(verti);
  delay(10);
  }



The other two for loops are similar in that they don't use the value that they are incrementing and just write the same values to the servos over and over a bunch of times. 
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 05, 2018, 06:49 pm
Wait, actually that code doesn't compile.  pos not declared in this scope.

Please post actual code that actually causes the actual issue and talk to us about that.  Don't waste time posting stuff that you aren't actually using. 
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 08, 2018, 04:54 am
Okay, so I just realized that that was a crap code that shouldn't have been posted. My bad. After going through my files, and doing some serious tweaking I found the correct code. This one is generally what I want to do, only I can't get it to work on my board. When I first upload the sketch to the board, the motors move- just barely- and then it's like they freeze, and I'm just looking at/listening to 3 softly buzzing servos. Am I drawing too much power?
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 08, 2018, 07:21 am

You really must learn to do this yourself.  When people are on phones or mobile devices they can't download your code but they can see it this way. 


Code: [Select]
#include <Servo.h>
Servo hor;  // horizontal servo
Servo vert; // vertical servo
Servo lid;  //eyelid servo

int servoPos = 0; //variable for servo pos (PWM 9-10-11)


void setup() {
vert.attach(9); //attach to pin 9
hor.attach(10); //attach to pin 10
lid.attach(11); //attach to pin 11

}

void loop() {
  for(servoPos = 0; servoPos < 180; servoPos+20)  //horizontal looks from L to R
  {hor.write(servoPos);
  delay(10);
  }
 
for(servoPos = 0; servoPos <=180; servoPos=+150)        //lid covers eye
 {lid.write(servoPos);
 delay(100);
 }

 for(servoPos = 180; servoPos > 0; servoPos--)   //lid recedes
 {lid.write(servoPos);
delay(100);
  }
 
 for(servoPos = 180; servoPos > 0; servoPos-20)  //horizontal looks from R to L
 {hor.write(servoPos);
 delay(30);
 }
 
 for(servoPos = 0; servoPos < 180; servoPos+45)  //vertical looks U and D
 {vert.write(servoPos);
 delay(30);
 }

 for(servoPos = 180; servoPos > 0; servoPos--)   //vertical looks D and U
 vert.write(servoPos);
 delay(15);
}
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 08, 2018, 07:23 am
Quote
Am I drawing too much power?
How would I be able to tell that from just the code?  Got a picture of this circuit?


Code: [Select]
for(servoPos = 180; servoPos > 0; servoPos--)   //lid recedes
 {lid.write(servoPos);
delay(100);
  }


18 seconds to open the eye?
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 08, 2018, 07:26 am
Code: [Select]
for(servoPos = 0; servoPos < 180; servoPos+20)

So what does servoPos+20 do?  Calculate a value and throw it away?  Shouldn't there be an = in there somewhere?

Code: [Select]
for(servoPos = 0; servoPos < 180; servoPos+=20)

Similarly in almost all your loops.
Title: Re: Eyeball Construction
Post by: slipstick on Sep 08, 2018, 10:06 am
I'm just looking at/listening to 3 softly buzzing servos. Am I drawing too much power?
Very likely. What type of servos are they and how are they powered? If the answer is "From the 5V pin" and/or "through a breadboard" then yes.

Steve
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 08, 2018, 04:34 pm
Very likely. What type of servos are they and how are they powered? If the answer is "From the 5V pin" and/or "through a breadboard" then yes.
[/quot

That is how I have them set up. But also tried to add a battery into the mix. So, I guess I either need a bigger battery (I used a 9V), or I have to redo my setup to make sure that the battery was actually part of the equation and not just there 'looking pretty'.
The servos I'm using are Emakefun MG90S Mini Metal Geared Micro Servos. I saw them in a few video tutorials where they were praised, and the site I bought them from also had good reviews for them; so I assumed they'd be great.
*Quick note, I do have 2 older servos that I bought from RadioShack a few years ago that still work great. I tried my sketch with them too and got the same result. So I don't think it's the motors.
Title: Re: Eyeball Construction
Post by: slipstick on Sep 08, 2018, 07:17 pm
If you power the servos with their own battery it is more likely to work. 4 x AA rechargeable NiMH would be ideal but 3 x 1.5V Alkaline AAs would also probably get you going. The breadboard may be o.k. but some of them can't handle the current that a servos need.

Steve
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 09, 2018, 12:09 am
The breadboard may be o.k. but some of them can't handle the current that a servos need.

Steve
Alright. Any suggestions for better breadboards or alternatives?
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 09, 2018, 02:40 am
So I tried the sketch again- this time powering the servos with their own battery. Confusedly, *nothing* happened this time. I'm worried that I overpowered my arduino board last night. I did add a battery last night too, but even then all I got was a soft buzzing from the motors. Really, really hoping that I didn't kill my board :smiley-confuse: and I'm just hoping that it's something else preventing my sketch from working. I've added a picture of my wiring for a second opinion/analysis. 

(https://apis.mail.yahoo.com/ws/v3/mailboxes/@.id==VjN-aHfl7shsU1ErdfDNql8KRfhw524wBabjUfBAf9tFXwLVWZ5ut1mM7AKYNoGNzUXq_tmbZU_QP6bd5QzJLzvu9w/messages/@.id==AJT5YPsZzi3pW5Rr7QAfQOynl4s/content/parts/@.id==2/thumbnail?appId=YMailNorrin&downloadWhenThumbnailFails=true&pid=2)
Title: Re: Eyeball Construction
Post by: slipstick on Sep 09, 2018, 10:48 am
It would be a lot more useful to see a photo of the actual components and wiring. Also please post the code you are currently using because I've lost track of what changes you've made.

Steve
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 09, 2018, 05:36 pm
Did you fix those for loops?  Post the code as it is now.  You've been around long enough to know to do that. 

Post a wiring schematic if you're still concerned you have something wired wrong.  Asking us to evaluate your wiring without showing it to us is just plain stupid.  Come on now, you can think at least a little here. 
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 09, 2018, 06:20 pm
Did you fix those for loops?  Post the code as it is now.  You've been around long enough to know to do that. 

Post a wiring schematic if you're still concerned you have something wired wrong.  Asking us to evaluate your wiring without showing it to us is just plain stupid.  Come on now, you can think at least a little here. 
Okay, I've had enough of you and you're piss poor attitude. I don't want your help anymore. I'm sure there are other people who are will to put up with you, but I'm not. Maybe your lack of patience or understanding for someone who's trying to learn will work for someone else on this site. But I've had enough. Good bye and good riddance.
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 09, 2018, 06:37 pm
It would be a lot more useful to see a photo of the actual components and wiring. Also please post the code you are currently using because I've lost track of what changes you've made.

Steve
Okay, I've managed to find a way to 'draw it out' if you will. I hope this is what you meant. And the code is added at the bottom. (Super-long post ahead)

(https://apis.mail.yahoo.com/ws/v3/mailboxes/@.id==VjN-aHfl7shsU1ErdfDNql8KRfhw524wBabjUfBAf9tFXwLVWZ5ut1mM7AKYNoGNzUXq_tmbZU_QP6bd5QzJLzvu9w/messages/@.id==AGlkuJQwno_PW5VK6A8fGJAdbSM/content/parts/@.id==2/thumbnail?appId=YMailNorrin)

Code: [Select]
#include <Servo.h>
Servo hor;  // horizontal servo
Servo vert; // vertical servo
Servo lid;  //eyelid servo

int servoPos = 0; //variable for servo pos (PWM 3-6-11)


void setup() {
vert.attach(3); //attach to pin 3
hor.attach(6); //attach to pin 6
lid.attach(11); //attach to pin 11

}

void loop() {
  for(servoPos = 0; servoPos < 180; servoPos+=20)  //horizontal looks from L to R
  {hor.write(servoPos);
  delay(10);
  }
  
for(servoPos = 0; servoPos <=180; servoPos+=150)        //lid covers eye
 {lid.write(servoPos);
 delay(100);
 }

 for(servoPos = 180; servoPos > 0; servoPos-=30)   //lid recedes
 {lid.write(servoPos);
delay(100);
  }
  
 for(servoPos = 180; servoPos > 0; servoPos-=20)  //horizontal looks from R to L
 {hor.write(servoPos);
 delay(30);
 }
 
 for(servoPos = 0; servoPos < 180; servoPos+=45)  //vertical looks U and D
 {vert.write(servoPos);
 delay(30);
 }

 for(servoPos = 180; servoPos > 0; servoPos-=30)   //vertical looks D and U
 vert.write(servoPos);
 delay(15);
}
Title: Re: Eyeball Construction
Post by: slipstick on Sep 09, 2018, 06:40 pm
Good luck - with that attitude to one of the more helpful members you're gonna need it cus you aren't likely to get much help.

Steve
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 09, 2018, 07:20 pm
With all due respect, if the only responses I'm going to get are mean-spirited and negative then it's not really help. Now I'm sorry if my previous posts have proven frustrating, but if their responses to my questions and comments are only going to make me second-guess my competence and if my questions are even worth it, then that's not the type of help that I want. I'm more than open to Delta_G's help, but only if the cruel attitude stays behind.
I still greatly appreciate your help and patience, Mr. Steve (if I may call you that?), and I hope that it may continue because I feel like I've already gotten so much more done speaking with you than I have previously. And for that, I thank you.
Title: Re: Eyeball Construction
Post by: slipstick on Sep 09, 2018, 10:11 pm
After Delta_G told you exactly how to change your loops you've done something different. And that's why nothing works. See if you can spot the difference.

Steve

Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 09, 2018, 11:26 pm
I was shown an example of how to fix it, and I incorporated it into my sketch. Delta_G gave me an example code a little while back in the discussion and I changed what I'd originally had. It appeared that I had left out the equal sign with my values for the servo movement. I'll try it again, wouldn't be surprised if I still missed it; but my outburst was in regards to the way Delta_G's responses were given. I get it, this is frustrating, I'm frustrated too, but if it's getting annoying to you [Delta_G], then perhaps it's time to take a step back and regain your composure before responding. The same can be said in my end as well.
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 10, 2018, 02:01 am
Okay, I've had enough of you and you're piss poor attitude. I don't want your help anymore. I'm sure there are other people who are will to put up with you, but I'm not. Maybe your lack of patience or understanding for someone who's trying to learn will work for someone else on this site. But I've had enough. Good bye and good riddance.
Nothing I said was mean spirited.  None of it was cruel.  It's just that you've been asked many times over now to provide a schematic of some sort.  It doesn't matter how newbie you are or how delicate a flower you might be, you can at least answer the questions you are being asked if you want help.  Instead of waving your arms around and wondering why you're getting nowhere, how about you work WITH the people who can and will help you and get somewhere with this. 

Get real.  Or show me what was so cruel and hateful.
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 10, 2018, 02:09 am
Was it because I said what you were doing was stupid? 

You are asking us if you burned out your servos.  And you want us to answer based on what exactly?  What do we actually know about your project other than it is supposed to make a creepy book?  We ask for a schematic, but you won't show us one.  Instead you say your servos are shaking and you don't know why. 

Well it could be the code you you might have burned something up.  How can anyone tell without seeing at least the code or the wiring?  How?  How newbie do you have to be to not understand that you have to show it to someone?  Even my little 2 year old daughter, when her toy breaks, she brings it and shows it to daddy.  She doesn't just come in and tell me it's broke.  She shows it to me and says, "daddy fix it".  She is 2.  You want me to believe that I should go easy on you for repeatedly not getting that point?  In all your other threads?  In all the time you've been here.  You can't understand something that a 2 year old gets.  And you think I'm being too hard on you to point that out? 

If you really are that helpless then I'm sorry.  I appologize.  But if you're not really that helpless, then take it for what it is.  A clue to wake up and start putting in a little thought and effort.  If you can see it for that then maybe you make it one day as a coder.  If not, then pick another hobby or field of study.  This one isn't a good fit for such a delicate flower.
Title: Re: Eyeball Construction
Post by: Delta_G on Sep 10, 2018, 02:09 am
Now if you want to show that schematic I'll tell you whether or not you've done anything bad to your Arduino.

No hard feelings. 
Title: Re: Eyeball Construction
Post by: slipstick on Sep 10, 2018, 10:10 am
Oh and another thing I don't like is when someone points out a problem and then you go back to an earlier post and make changes to it without ever acknowledging that you got it wrong in the first place, so making my comment look pointless.

When you change something post the changed version in a new post.

Steve
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 10, 2018, 04:24 pm
Oh and another thing I don't like is when someone points out a problem and then you go back to an earlier post and make changes to it without ever acknowledging that you got it wrong in the first place, so making my comment look pointless.

When you change something post the changed version in a new post.

Steve
What? I don't understand. I mean, I know what you're saying, but where/when did I do that? I never tried to make your posts look pointless. Now, I did go back and fix some code in earlier posts, but only so I don't confuse myself. If you thought I was trying to pull something on either of you, I don't want you to feel that way. I was reading through everything to see where/what I screwed up in those for loops. I changed them, both in here and in the actual sketch to make sure I don't make that mistake in the code again. Sorry if you thought I was trying to be a smarta**
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 10, 2018, 04:43 pm
No hard feelings :)
Quote
Now if you want to show that schematic I'll tell you whether or not you've done anything bad to your Arduino.
Okay, so here is my current circuitry for the arduino. I posted one earlier, only to realize that the wiring for the power and ground were incorrect. So I fixed that, plus, in speaking to someone on facebook, I also learned that if I planned to power the sketch with a battery, then I should insert the power wire into VIN instead of 5V because that could blow my board. I tried it this way, but my servos still seemed to do nothing but buzz and move very, very slowly if at all.

(https://apis.mail.yahoo.com/ws/v3/mailboxes/@.id==VjN-aHfl7shsU1ErdfDNql8KRfhw524wBabjUfBAf9tFXwLVWZ5ut1mM7AKYNoGNzUXq_tmbZU_QP6bd5QzJLzvu9w/messages/@.id==APp6999iy_vcW5aAcwFP8FXrbNc/content/parts/@.id==2/thumbnail?appId=YMailNorrin)

I decided to retry everything but only use 1 servo at a time- like a regular sweep. In doing so, the servos still didn't respond; so I tried one of my older micro servos which responded perfectly. So i decided that the servos I was originally using were a part of the problem. So I won't be using them anymore.
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 10, 2018, 04:49 pm
As for the code, this is what I've got so far.
Code: [Select]
#include <Servo.h>
Servo hor;  // horizontal servo
Servo vert; // vertical servo
Servo lid;  //eyelid servo

int servoPos = 0; //variable for servo pos (PWM 3-6-11)


void setup() {
vert.attach(3); //attach to pin 3
hor.attach(6);  //attach to pin 6
lid.attach(11); //attach to pin 11
}

void loop() {
  for(servoPos = 0; servoPos <= 180; servoPos += 20){      //horizontal looks from L to R
    hor.write(servoPos);
    delay(10);
   }
 
  for(servoPos = 0; servoPos <= 180; servoPos += 100){     //lid covers eye
    lid.write(servoPos);
    delay(100);
   }

   for(servoPos = 180; servoPos > 0; servoPos -= 100){    //lid recedes
     lid.write(servoPos);
     delay(100);
    }
 
   for(servoPos = 180; servoPos > 0; servoPos -= 20){     //horizontal looks from R to L
     hor.write(servoPos);
     delay(30);
    }
 
   for(servoPos = 0; servoPos < 180; servoPos += 45){     //vertical looks U and D
     vert.write(servoPos);
     delay(30);
    }

   for(servoPos = 180; servoPos > 0; servoPos -= 45){   //vertical looks D and U
     vert.write(servoPos);
     delay(15);
   }
}


Regrettably, I haven't been able to really try it out yet because I only have 1 working servo (like I said, the other 3 that I have are crap, so I've got to get some more).
Title: Re: Eyeball Construction
Post by: nielyay on Sep 14, 2018, 05:37 am
Quote
My goal is to have the eye- controlled by 2 servos- look up, down, left and right, while an eyelid, controlled by a 3rd servo, blink on occasion.
So you need to start learn about servo code or any motors. Check this page (https://www.instructables.com/id/Animatronic-Eyes-With-Servo-Motors-Arduino/), may can help
Title: Re: Eyeball Construction
Post by: Brainfart14 on Sep 22, 2018, 04:22 pm
Thank you. I have looked at tutorials like this one, though admittedly, this is the only one to actually provide their code. It does help, but I'm not using a joystick to control the movement of the eye. I want it to move/look around on its own. I'm going to compare their code to my own, to see what I did/didn't do.