Pages: [1]   Go Down
Author Topic: help im not geting hoe the eles statments are use corectly .  (Read 410 times)
0 Members and 1 Guest are viewing this topic.
NH USA
Offline Offline
Full Member
***
Karma: 0
Posts: 104
NEVER ENOUGH TIME TO BUILD
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i was makeing a  stepper motor pincher finger with s stepper motors with gear reduction running 2 fingers that could clamp down on some thing and hold it then relase it when i want to ... i ran in to a problem useing the stepper libairy that came with the arduino 1.0 version  useing a pot i set it so 0 to 512 was opening the clamp and 550 was closeing the clamp useing an else statment but i tryed to use an if else and else if  to have it stop and hold between 512 and 550 say it was reading 530 i wanted it to stop moveing but it just kept closeing  then binding the gears under the pressure. Not good ! so i looked in to the arduino examples of if and else if witch i think i under stood it should work the way i want for this but code some thing like this

void loop {

if pot is 512
close stepper finger
}
else{
pot val is 550
open finger
}
else if {
do nothing
}
this is just an example of the top of my head of how it tryed to word it not the actul code but eaither way i cold not get the format of the 3 place if the else then eles if to work i could only get the  if then the else to copile and work but stepper would just keep moving with out the 3rd eles if comand

any one want to explin how to use the eles and if statments multpile times ... ? _thanks !
cl
Logged

EZ8MMPRO

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Spend a day or three in the tutorial section reading example programs you will learn a lot from that.

this is how the loop() could look to compare analog reads...
Code:
void loop
{
  pot = analogRead();
  if (500 < pot && pot < 520)    // as you can expect noise on the analogread
  {
    // close stepper finger
  }
  else if (540 < pot && pot < 560)
  {
    // open finger
  }
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

NH USA
Offline Offline
Full Member
***
Karma: 0
Posts: 104
NEVER ENOUGH TIME TO BUILD
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you that make it alot better setup like that i think ... awsome .. now i just gotta figuer out what does what and how my setup should be thank you ...!
Logged

EZ8MMPRO

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


as said:
Quote
Spend a day or three in the tutorial section reading example programs you will learn a lot from that.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

robtillaart:

While your example is technically correct and will work, the "standard" method is to place the variable before the comparison rather than after:

Code:
void loop() {
  pot = analogRead(pin);

  if (pot > 500 && pot < 520) {   // as you can expect noise on the analogread
    // close stepper finger
  }
  else {
     if (pot > 540 && pot < 560) {
       // open finger
     }
   }
}

As you can see, I also prefer a different bracing style - but that is up to the implementer... smiley-wink

Where your style of variable placement does come in handy, though, is with the "==" comparison operation; should you have a statement where you meant:

Code:
if (x == 50) {
}

...and you instead put:

Code:
if (x = 50) {
}

...the compiler won't complain (since it is a valid statement, and evals to TRUE) - however, if you did the following (what you meant):

Code:
if (50 == x) {
}

...but typed:

Code:
if (50 = x) {
}

...the compiler would complain immediately; but without explaining why to a beginner, they won't know why (has to do with not being able to set a constant value to the value of a variable or something like that) - your posting initially looked odd to me, but then I remembered the above "dodge" which is probably what you have gotten into the habit of for all variable comparisons (just a guess)...

smiley


« Last Edit: February 24, 2012, 11:58:03 am by cr0sh » Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Cr0sh
Quote
While your example is technically correct and will work, the "standard" method is to place the variable before the comparison rather than after:

yes but my way "reads more like proze", as you can directly see that pot is between two values. The two operators are the same which makes reading easier

four statements, semantically equal and which one is more readable? it is a matter of taste.
Code:
if (500 < pot && pot < 520) 
if (pot > 500 && pot < 520)
if (pot < 520 && pot > 500)
if (520 > pot && pot > 500)

If the test is complexer you can reduce the visual complexity by using only < and <= and == 
In the same way if the boolean expression includes x,y,z and some values you would keep the x's near each other and the y 's just to keep the visual complexity low?

On the other hand in a real app I would use an if then else ladder if there was more than 1 range to test, Please note they all have the same comparison operator! Such a ladder is quite easy for the compiler to optimize. OK you can argue that the most often occuring should be on top for performance .. ! but you will probably get complexer boolean tests.

Code:
pot = analogRead()
if (pot < 500)
{
  // nothing
}
else if (pot < 520)
{
  // something
}
else if (pot < 540)
{
 // nothing
}
else if (pot < 560)
{
  // something else
}
else
{
  // ok you got it
}

Quote
As you can see, I also prefer a different bracing style
fine as long as used consequently --> see - http://astyle.sourceforge.net/astyle.html -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

yes but my way "reads more like proze", as you can directly see that pot is between two values. The two operators are the same which makes reading easier

That's definitely an interesting and valid addendum to the argument for it; something I'll have to think about... smiley

fine as long as used consequently --> see - http://astyle.sourceforge.net/astyle.html -

Oh - definitely; I'm kinda set in my ways coding-wise - been doing it for almost 25 years now! But keeping things consistent is of the most importance to pass on to those unfamiliar or new to coding, and that should be made clear...

smiley-grin
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

0
Online Online
Shannon Member
****
Karma: 162
Posts: 10495
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In mathematics you write ranges like:  20 < var < 40, so my vote is definitely in favour of
Code:
  if (20 < var && var < 40)
  ...

Unfortunately C does not support the mathematical style of range comparisons, but we can at least make it look a bit more like it.
Logged

[ I won't respond to messages, use the forum please ]

NH USA
Offline Offline
Full Member
***
Karma: 0
Posts: 104
NEVER ENOUGH TIME TO BUILD
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok this is what i tried just for reference feel free to tell me any thing i did wrong or will not work ive only started this c type code a month ago and i am only use to some g code and some hard ware and how things work soo i guess i need all the help i can get  i can build a cool robot or cnc in lil time with a purpose and or objective but programing it some times makes me give up and move on to a hardware solution lol... thank you for all you help be it works out or not i appreciate it  !



#include <Stepper.h>

const int stepsPerRevolution = 100;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);           


int stepCount = 0;         // not sure where this applys ?

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
}

void loop(){

  int sensorValue = analogRead(A0);  // this is my pot i never caled it out as a pot
  Serial.println(sensorValue);       // maybe i should have
  // read the sensor value:
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 500, 100, 0);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution/100);

  }
  if else{// k this setup is just wrong the more i look it over i dont know where to begin
    int sensorReading = analogRead(A0);
    // map it to a range from 0 to 100:
    int motorSpeed = map(sensorReading, 550, 1023, 0, 100);
    // set the motor speed:
    if (motorSpeed > -0)
      myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(-stepsPerRevolution/100);

  }

  else{
    int sensorReading = analogRead(A0);
    // map it to a range from 0 to 100:
    int motorSpeed = map(sensorReading, 501, 549, 0, 0); //motor speed of zero dose this work as a stop
    // set the motor speed:
    if (motorSpeed > -0)
      myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(-stepsPerRevolution/100);
 delay(10);
  }
}


Logged

EZ8MMPRO

Pages: [1]   Go Up
Jump to: