Go Down

Topic: how to use if statement (Read 662 times) previous topic - next topic

James C4S

Your if-statement is correct.  So something is causing the value to drop below 725.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

lyrical10

If I use the code below the values of 722 and 880 work fine.  Could simply uploading new code with the if statement added make those values change?

#include <Servo.h>

Servo pinkyservo;

int pinkyflex = 6;


void setup()
{
  pinkyservo.attach(9);
}

void loop()
{
  pinkyflex = analogRead(6);
  pinkyflex =map(pinkyflex, 722, 880, 6, 179);
  pinkyservo.write(pinkyflex);
 
}

James C4S


If I use the code below the values of 722 and 880 work fine. 

Absolutely no idea what "work fine" means.  Describe the behavior you are seeing and the behavior you don't want to see.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

lyrical10

By that I mean with those values the servo acts accordingly based on the sensors flex when mapped as   "pinkyflex =map(pinkyflex, 722, 880, 6, 179);"  I wanted to add the if statement because the servo twitches a little when everything is at rest.  The flex sensor's resting value is ruffly 722 but it does fluctuate a little, which I believe is what is causing the servo to twitch.

I was hoping that by adding if (pinkyflex > 725) it would allow for the slight fluctuation in the sensor's resting value. 

Hopefully that makes sense.

WizenedEE

Might you want to move the servo if the sensor was either above 725 OR below 720?

Code: [Select]

if (pinkyflex > 725 && pinkyflex < 720) {
  pinkyservo.write(pinkyflex);
}

Go Up