Pages: [1]   Go Down
Author Topic: how to use if statement  (Read 583 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Denmark
Offline Offline
God Member
*****
Karma: 15
Posts: 657
Please don't send questions as PM!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:

Code:
#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);
  }
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your if-statement is correct.  So something is causing the value to drop below 725.
Logged

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
 
Logged

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

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

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

After re-reading your code, this doesn't make sense:
Code:
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.

Logged

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
 
}
Logged

Pages: [1]   Go Up
Jump to: