Pages: [1]   Go Down
Author Topic: [SOLVED] Trying to Randomly Move a Servo  (Read 701 times)
0 Members and 1 Guest are viewing this topic.
USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 689
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Basically I am trying to "blink an eye."  The servo is connect to an eyelid and I want it to blink all the open or closed.  I have a randomized code that moves the servo to random locations.  How would I go about coding this to move it randomly from closed to open without stopping in the middle?

Code:
#include <Servo.h>

 Servo myservo;
 
 int pos = 0;
 int delay1 = 0;
 
 void setup()
 {
 myservo.attach(9);
 }
 void loop()
 {
   pos=random(65,115);
 delay1=random(50, 500);
 myservo.write(pos);
 delay(delay1);
 }
« Last Edit: June 11, 2012, 11:06:50 am by codlink » Logged

//LiNK

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
bool open = random(2); // open is 0 or 1 ie false or true
// Three ways to do it with varying levels of verboseness
// 1:
if (open) {
  eye.write(120);
}
else {
  eye.write(30);
}
// 2:
eye.write(open? 120 : 30);
// 3:
eye.write(90*open + 30);
Logged

USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 689
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just don't know enough about writing a program to implement your code.  I guess I will have to keep studying...
Logged

//LiNK

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <Servo.h>

Servo myservo;
boolean state;

void setup()
{
  myservo.attach(9);
}
void loop()
{
  state = random(2);
  if (state) myservo.write(115); //open
  else myservo.write(65); //close
  delay(random(50, 500));
}
This will randomly either close, or open the eye, and wait a random delay. If you want the numbers to be very random, use http://code.google.com/p/tinkerit/wiki/TrueRandom.
Logged


USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 689
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks dkl65

Haven't looked at boolean yet.  That is where I was stumped. 
Logged

//LiNK

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@WizEE....

I intuit that the syntax:

Code:
(open? 120 : 30)

is a test for open, and if it's true return 120, else return 30.

But I can't find the syntax anywhere in the Arduino Reference so I'm intrigued as to where you found out about it. Is it lurking in the reference somewhere that I didn't see?
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@WizEE....

I intuit that the syntax:

Code:
(open? 120 : 30)

is a test for open, and if it's true return 120, else return 30.

But I can't find the syntax anywhere in the Arduino Reference so I'm intrigued as to where you found out about it. Is it lurking in the reference somewhere that I didn't see?

Trick from C, not specific to Arduino:
http://en.wikipedia.org/wiki/Ternary_operation
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can also try www.cplusplus.com for C++ code reference. The Arduino reference focuses on functions in the Arduino library, rather than C/C++ itself.
Logged


Pages: [1]   Go Up
Jump to: