Modifying a value based on another value.

I want to use the up/down value to modify the eyelid positions, but I don’t want it to be 1:1 movement. I also don’t want it to modify the eyelid values inside a certain range. Am I looking at several map functions? or another function type I should look into?

My terminology may be incorrect, so if something doesn’t sound right I’ll do my best to explain it properly.

Thanks!

#include <Servo.h> 
 // create servo object to control a servo 
Servo servo1;  // upper left eyelid 
Servo servo2;  // lower left eyelid 
Servo servo3;  // upper right eyelid
Servo servo4;  // lower right eyelid
Servo servo5;  // left eye horizontal
Servo servo6;  // right eye horizontal
Servo servo7;  // left eye vertical 
Servo servo8;  // right eye vertical 


int pospot1 = A0;
int lrpos = A1;
int udpos = A2;
int blinksw = 52; 
int lidpos;
int lefteyeud;
int lefteyelr;
int righteyeud;
int righteyelr;



void setup() 
{ 
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  pinMode(52,INPUT);
  servo1.attach(9);  // attaches the servos to the servo object 
  servo2.attach(8);
  servo3.attach(7);
  servo4.attach(6);
  servo5.attach(5);
  servo6.attach(4);
  servo7.attach(3);
  servo8.attach(2);
  Serial.begin(38400);
  
} 
 
void loop() 
{ 
  lidpos = analogRead(pospot1);
  lidpos = map(lidpos, 0, 1023, 1370, 1800);

 //left eye code
   lefteyelr = analogRead(lrpos);
   lefteyeud = analogRead(udpos);
   lefteyelr = map(lefteyelr, 0, 1023, 580, 2370);
   lefteyeud = map(lefteyeud, 0, 1023, 550, 2400);

 //right eye code
   righteyelr = analogRead(lrpos);
   righteyeud = analogRead(udpos);
   righteyelr = map(righteyelr, 0, 1023, 580, 2370);
   righteyeud = map(righteyeud, 0, 1023, 550, 2400);
   
 // eye position write
   servo5.writeMicroseconds(lefteyelr);             
   servo6.writeMicroseconds(lefteyeud);
   servo7.writeMicroseconds(righteyelr);             
   servo8.writeMicroseconds(righteyeud);


  // eye lid position and blink function
    if (digitalRead(blinksw) == HIGH)
    {
   servo1.writeMicroseconds(1800);              // tell servos to blink 
   servo2.writeMicroseconds(1800);
   servo3.writeMicroseconds(1800);
   servo4.writeMicroseconds(1800);
   Serial.println("Blink");
   delay(400); 
 }
   else (digitalRead(blinksw) == LOW);
   { 
     servo1.writeMicroseconds(lidpos);              // set eyelid position 
     servo2.writeMicroseconds(lidpos);
     servo3.writeMicroseconds(lidpos);
     servo4.writeMicroseconds(lidpos);
     
     delay(25);
 }

  // debug serial info
 Serial.print(lidpos);
 Serial.print(','); 
 Serial.print(lefteyelr);
 Serial.print(',');
 Serial.println(lefteyeud);
 Serial.print(','); 
 Serial.print(righteyelr);
 Serial.print(',');
 Serial.println(righteyeud);
 delay(20);
}

Taking this example :

righteyelr = analogRead(lrpos);
righteyelr = map(righteyelr, 0, 1023, 580, 2370);

So you read an analog value in the range 0 to 1023 and put that in the variable righteyelr. You then want to change that so instead of being in the range 0 to 1023, it is converted linearly to the range 580 to 2370. Is that what you want to achieve ?

A shorter way of writing it would be:

righteyelr = map( analogRead(lrpos), 0, 1023, 580, 2370);

A good time to modify the eyelid position would be after you read the base position from 'pospot1' (bad name for the eyelid position input pin) and the time you write the eyelid position to the four eyelid servos.

What I would do is calculate the the vertical eye position relative to center (1500 microseconds?) and add a portion of that value to the eyelid position:

  int eyelidPosition = map(analogRead(pospot1), 0, 1023, 1370, 1800);
    int eyeVerticalPosition = map(analogRead(udpos), 0, 1023, 550, 2400);
    eyelidPosition = constrain(eyelidPosition + ((eyeVerticalPosition-1500)/5), 1000, 2000);

The 'constrain' function is to keep the eyelid position from going out of the normal range of 1000 to 2000 microseconds.

I think this is what I am looking for. I'll try it out and see if I can use it properly. And thanks to you and 6v6gt for the shorter code!

Oh, one thing I just thought of. Given that if the eye is moving up, the value on the upper lid will decrease, and the lower would increase, do I just add another line and switch the -/+ for the lower? I'll try it and see...

johnwasser: A good time to modify the eyelid position would be after you read the base position from 'pospot1' (bad name for the eyelid position input pin) and the time you write the eyelid position to the four eyelid servos.

What I would do is calculate the the vertical eye position relative to center (1500 microseconds?) and add a portion of that value to the eyelid position:

  int eyelidPosition = map(analogRead(pospot1), 0, 1023, 1370, 1800);
    int eyeVerticalPosition = map(analogRead(udpos), 0, 1023, 550, 2400);
    eyelidPosition = constrain(eyelidPosition + ((eyeVerticalPosition-1500)/5), 1000, 2000);

The 'constrain' function is to keep the eyelid position from going out of the normal range of 1000 to 2000 microseconds.

MrDoggss: Oh, one thing I just thought of. Given that if the eye is moving up, the value on the upper lid will decrease, and the lower would increase, do I just add another line and switch the -/+ for the lower? I'll try it and see...

That should work.