Go Down

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

lyrical10

I am using a flex sensor to control a servo with the code below.  I would like to add an if statement so that the servo doesn't move unless the flex sensor's value is above 725.  (I am hoping that by doing this the servo will stop twitching when everything is at rest).  I don't know how to add the if statement correctly.  I tried the following but it didn't work.  Can anyone tell me how to add this if statement correctly?

#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);
  if (pinkyflex > 725) pinkyservo.write(pinkyflex);
}

Zapro

Try this:

Code: [Select]
#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);
  if (pinkyflex > 725) {
    pinkyservo.write(pinkyflex);
  }
}

lyrical10

I tried that also and it does not work.  Any other ideas or suggestions?

James C4S

Why don't you add a Serial.print() of the calculated value so you can see what the values actually are?

Both code snippets so far are exactly the same in terms of syntax.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

lyrical10

I have already used serial.print in previous code.  That's how I got 722 and 880 as my input values.  I know those values are good because if I take out the if statement and just use pinkyservo.write(pinkyflex) it works fine.

I don't understand why the if statement is not working. 

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);
}

James C4S

From the reference:
Code: [Select]
map(value, fromLow, fromHigh, toLow, toHigh)

After re-reading your code, this doesn't make sense:
Code: [Select]
pinkyflex =map(pinkyflex, 722, 880, 6, 179);
 if (pinkyflex > 725) {


If pinkyflex is 722, then it becomes 6.  So pinkyflex isn't going to be greater than 179 when your if-statement executes.  A Serial.print() statement right before the if-statement would have illustrated that.

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

lyrical10

Thank you James C4S!  That last statement is what I needed for the lightbulb to click on.  Simple solution.  I was using the original input values for the if statement instead of the new mapped values of 0 and 179.  The code below works great.  No servo twitch!

Code: [Select]
#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);
  if (pinkyflex > 15) pinkyservo.write(pinkyflex);
 
}

Go Up