help micro elevator code

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)

If you don't need to know the distance, then why do you care about how many pulses the hall sensor does ? Why not just watching the floor switches ?

because i dont need to know the distance i need to know how many pulse the sensor gives, because in each floor there is a magnet, that magnet "sense" the elevator, so if i want to go from the first floor to the 4th i just need to start the stepper and stop it untill the the the hallcounter its 3, i dont need to measure distance or count steps, i just need to know in which direction i want the stepper to run

copachino:
because i dont need to know the distance i need to know how many pulse the sensor gives, because in each floor there is a magnet, that magnet "sense" the elevator, so if i want to go from the first floor to the 4th i just need to start the stepper and stop it untill the the the hallcounter its 3, i dont need to measure distance or count steps, i just need to know in which direction i want the stepper to run

That seems reasonable enough - and straight forward to code. My main concern with that arrangement would be whether the sensor is accurate enough to let you stop the lift accurately aligned with the floor every time.

Given that you are using a stepper motor, though, this would enable you to achieve a much simpler and more consistent result just by measuring how many steps were needed to move the lift between two floors, and then just step the stepper that number of steps in order to move the lift.

i was planning to count steps first but, it involves too much time because i need to regulate and meassure or maybe calculate steps, and its very annoying to do....

as for de code the truth its that the only way a found myself to program its with a "for" for every "if" in the switch sentence, and it will be very long to programm...

In a real elevator, you'd have a switch (your magnet and hall effect sensor would work fine) that said you were getting near the floor, and a switch that said that you were at the floor. When you get near the floor, slow down and sneak up on the floor.

Or, get over it and count the steps needed to get from floor to floor. Keep in mind that in a real building, the floors are not all the same distance apart.

well i think i will count the steps,cos its more easy to program for a noob like me....

maybe later will post results on the code once the stepper driver its ready i will start will try the code..

thanks a lot to everyone