help micro elevator code

Well, you need to fix the problems already pointed out.
Then you need to run it, and figure out what it is that it does that you didn't expect it to, and what it didn't do that you did expect it to.
Debug prints are useful for this.

yes i now, but i can test it till tomorrow when the stepper driver its ready, but i need to know now how can i make the stepper to run without tellihn how many steps i need to move it

reading the state of the pins would be like this right

Yes, but...

Later in your code, what does val1 mean? Could you infer what floorSwitchState1 meant? Which is easier to understand?

Now, it makes no difference to the compiler. Everything is translated to a memory address as far as the compiler is concerned. But, long before the compiler has spent a few seconds with your code, you (and we) will have spent hours trying to understand what it is doing. Why not make it easy on yourself and use meaningful names?

Could this be a case of the elevator not going all the way to the top?

Bob

Could this be a case of the elevator not going all the way to the top?

With all those misplaced semicolons anything can happen.

val1 its the variable for reading the pin status.....

this its taken from this tutorial for push buttomshttp://www.arduino.cc/en/Tutorial/Pushbutton....

Floor ist obvious the floor counter, for arduino to "know" which floor its,

copachino:
val1 its the variable for reading the pin status.....

this its taken from this tutorial for push buttomshttp://www.arduino.cc/en/Tutorial/Pushbutton....

Floor ist obvious the floor counter, for arduino to "know" which floor its,

the semicolosn where remove from the code...

this its the code for now:

/* 
 Stepper elevator Control - 
 
 This program drives a unipolar or bipolar stepper motor. 
 The motor is attached to digital pins 10-11 of the Arduino.
 
 The push buttoms selects the floor and the stepper spin till 
 the hall sensor its trigger enough ties o reach the floor the push buttoms selects. 
 
 
 
 
 Created 18 Nov. 2012
 by Cesar flores
 
 */

#include <Stepper.h>
    
const int stepsPerRevolution = 200;  
const int  Floor1 = 2;
const int  Floor2 = 3;
const int  Floor3 = 4;
const int  Floor4 = 5;
int val1= 0;
int val2= 0;
int val3= 0;
int val4= 0;

// initialize the stepper library on pins 10 11:
Stepper myStepper(stepsPerRevolution, 10,11);            
int hallcount=0;
int Floor = 1;         // the floor the elvator its

void setup() {
  attachInterrupt(0, hall_count, RISING);// initialize the hall effect sensor pin as an interrupt
 myStepper.setSpeed(30);

 pinMode(Floor1, INPUT);
 pinMode(Floor2, INPUT);
 pinMode(Floor3, INPUT);
 pinMode(Floor4, INPUT);
}

void loop() {
  val1=digitalRead(Floor1);
  val2=digitalRead(Floor2);
  val3=digitalRead(Floor3);
  val4=digitalRead(Floor4);
  
  


  switch (Floor)
  {
  case 1:
  {if (Floor==1 && val1==HIGH)
  {
  
    myStepper.step(0);
   
   Floor=Floor; 
  }
  if(Floor==1 && val2==HIGH)
  {
  myStepper.step(400);//here its my problem i want to move the stepper till its reach the hall sensor
  Floor++;  
  
  }
  if(Floor==1 && val3==HIGH)
  {
   myStepper.step(400); 
  Floor=Floor+2;
  
  }
  if(Floor==1 && val4==HIGH)
  {
    myStepper.step(400);
   Floor=Floor+3; 
  
}
  }
break;


  
  
  
  case 2:
  {if (Floor==2 && val1==HIGH)
  {
   myStepper.step(400);
    
    Floor--; 
  }
  if(Floor==2 && val2==HIGH)
  {
  myStepper.step(400);
  Floor=Floor;
  }
  if(Floor==2 && val3==HIGH)
  {
   myStepper.step(400); 
  Floor++;
  }
  if(Floor==2 && val4==HIGH)
  {
    myStepper.step(400);
   Floor=Floor+2; 
  }
  }
  break;
  
  
  case 3:
  {if (Floor==3 && val1==HIGH)
  {
   myStepper.step(400);
   
   Floor=Floor-2; 
  }
  if(Floor==3 && val2==HIGH)
  {
  myStepper.step(400);
  Floor--;  
  }
  if(Floor==3 && val3==HIGH)
  {
   myStepper.step(400); 
  Floor=Floor;
  }
  if(Floor==3 && val4==HIGH)
  {
    myStepper.step(400);
   Floor++; 
  }
  }
  break;
  
  case 4:
  {if (Floor==4 && val1==HIGH)
  {
  myStepper.step(400); 
   
   Floor=Floor-3; 
  }
  if(Floor==4 && val2==HIGH)
  {
  myStepper.step(400);
  Floor=Floor-2;
  }
  if(Floor==4 && val3==HIGH)
  {
   myStepper.step(400); 
    
    
  Floor=Floor-1;
  }
  if(Floor==1 && val4==HIGH)
  {
  myStepper.step(400);  
   Floor=Floor; 
  }
  }
  break;
  
 
  }  
}

void hall_count()
 {
   hallcount++;}
   //, this interrupt function is run once when the elevator passes the magnets on each floor

PaulS:

reading the state of the pins would be like this right

Yes, but...

Could you infer what floorSwitchState1 meant? Which is easier to understand?

do you mean the state of the floor??? its the floor number there its only 4 floor, so for the first time the floor will be on floor one, the counter Floor will let decide which option will take the elevator depending on the state of the push buttons, the idea its that when depending on wich floor the elvator its, and also depending on which push button its pressed the program will decide the direction and how many times the hall sensor needs to be passed

You are testing (all?) the combinations between the target floor and the current floor. I suggest you write code that calculates the current floor based on val1..4 values. Then you can just look at the difference between where you are and where you want to be.

Like:

if val1 == HIGH
currFloor = 1
etc.

distance = currFloor - targetFloor;

if distance == 0
stop
else
set motor direction based on the sign of distance
move motor of (stepsPerFloor * abs(distance)) steps
endif

in fact as you can see i dont need the if(Floor==1) i can be only testing the digital read as if(val1==HIGH).....
that was the main doubt i had, am used to program in c++(and this its not c++), so im not sure tha if i select the variable Floor for the switch sentence its ok, or its the biggest mistake on the code, because i am using the memory of the counter Floor for the program to decide de case,
for example in case 1: the counter Floor values 1, and depending on the state of the buttons i get the direction of the stepper..

{
  case 1:
  {if (val1==HIGH)
  {
  
    myStepper.step(0);
   
   Floor=Floor; 
  }
  if(val2==HIGH)
  {
  myStepper.step(400);//here its my problem i want to move the stepper till its reach the hall sensor
  Floor++;  
  
  }
  if(val3==HIGH)
  {
   myStepper.step(400); 
  Floor=Floor+2;
  
  }
  if(val4==HIGH)
  {
    myStepper.step(400);
   Floor=Floor+3; 
  
}
  }
break;

now you're not testing Floor==1. You were in the last code you posted.

Oh, btw, that looks like (terrible) C++ to me. Would you please enlighten me as to which language it really is instead ? :stuck_out_tongue:

it was supposed on c++, but i said i am terrible programming...

but i dont need to test the Floor am in because that its the job of the counter Floor, as you can see it increments or decrements depending on haw many floors the elvator moves.... but the main problem its to implement a routine, that when the elevator its on move, untill this routine finish the elevator wont change the direction when a buttom pin changes its state...

why don't you keep the motor stepping in small increments until the switch for the target floor activates?

Like:

while switch of target floor is off
step motor for 10 steps
end while

or maybe there's a "continuous move" function in the stepper library... I don't know that lib very well still...

i know there was a mod stepper library tha includled the STOP, CCW and CW movement, but i cant find it now jejeje... the steper moves really slow...

also do you think that for this moment, the switch(Floor) and the "if" inside the cases are well programmed??, i suck so much that i can even tell that.... the whole idea with the sensor its also use a counter hall, as i said sometimes it need tobe triggered more than once to reach a floor, so that what i need the hall_counter, but, im havng problems with that because i need that the hall_counter reset to zero every time the stepper moves

i suck so much

I think you just need to take some time to review the C syntax and the Arduino API (arduino.cc => Reference).

About the hall sensor... You need to know when the elevator reached a particular floor. Isn't the floor switch enough ?

copachino:

  Floor++;  

Floor=Floor+2;
   Floor=Floor+3;

I doubt this will end up with the correct value in Floor. I haven't reviewed the whole sketch, but I'd expect to see Floor assigned to the value corresponding to the floor it has just reached (Floor = 3; etc) or incremented / decremented by one each time is passes a floor ( Floor++; Floor--; ).

PeterH:

copachino:

  Floor++;  

Floor=Floor+2;
   Floor=Floor+3;

I doubt this will end up with the correct value in Floor. I haven't reviewed the whole sketch, but I'd expect to see Floor assigned to the value corresponding to the floor it has just reached (Floor = 3; etc) or incremented / decremented by one each time is passes a floor ( Floor++; Floor--; ).

the problem comes when the elvator moves more than one floor but i dont think thats a problem, still i can just use Floor= 2 directly instead or just increments o decrements

About the hall sensor... You need to know when the elevator reached a particular floor. Isn't the floor switch enough ?
[/quote]

the whole idea of the hall sensor, its no count steps, i dont want to counr how many steps i need to reach a floor,i just want to count how many times the hall sensor changes from low to high...

To make the hall sensor counts useful you still have to know the distance covered by the elevator for each hall sensor transition. That's not very different from knowing how many cm you elevator travels per motor step.

i dont need to know the distance, i just need to know when they canhge the state, from low to high, and how many times it changes,

using something like

for(hallcount=0,hallcount<=3,hallcaount++)
stepper(CW) or (CCW)