Go Down

Topic: help micro elevator code (Read 3868 times) previous topic - next topic

copachino

hi all i want write a code for a micro elevator moved with a stepper, but i have many doubts
for example a believe that my code using the switch sentence its wrong, but im not so sure, for example i want to control the elevator using the 4 buttoms attached to the digital pins 2-5, but using a counter called Floor to let me know in which floor the elevatr its, and depending on that if any buttom its trigger then the stepper will move on the direcction to reach the floor in which the elevator was called, and also decrementing or adding floor to the counter Floor[/b

here its my code untill now:
Code: [Select]
/*
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 hall = 12;     // the number of the hall effect sensor pin
const int stepsPerRevolution = 200;  
const int  Floor1 = 2;
const int  Floor2 = 3;
const int  Floor3 = 4;
const int  Floor4 = 5;

// 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() {
 // initialize the hall effect sensor pin as an input:
myStepper.setSpeed(30);
pinMode(hall, INPUT);
pinMode(Floor1, INPUT);
pinMode(Floor2, INPUT);
pinMode(Floor3, INPUT);
pinMode(Floor4, INPUT);
}

void loop() {
 if (hall==HIGH);{
 hallcount++;}
 switch (Floor)
 {
 case 1:
 {if (Floor==1 && Floor1==HIGH);
 {
 
   myStepper.step(0);
 
  Floor=Floor;
 }
 if(Floor==1 && Floor2==HIGH);
 {
 myStepper.step(400);//here its my problem i want to move the stepper till its reach the hall sensor
 Floor++;  
 
 }
 if(Floor==1 && Floor3==HIGH);
 {
  myStepper.step(400);
 Floor=Floor+2;
 
 }
 if(Floor==1 && Floor4==HIGH);
 {
   myStepper.step(400);
  Floor=Floor+3;
 
}
 }
break;


 
 
 
 case 2:
 {if (Floor==2 && Floor1==HIGH);
 {
  myStepper.step(400);
   
   Floor--;
 }
 if(Floor==2 && Floor2==HIGH);
 {
 myStepper.step(400);
 Floor=Floor;
 }
 if(Floor==2 && Floor3==HIGH);
 {
  myStepper.step(400);
 Floor++;
 }
 if(Floor==2 && Floor4==HIGH);
 {
   myStepper.step(400);
  Floor=Floor+2;
 }
 }
 break;
 
 
 case 3:
 {if (Floor==3 && Floor1==HIGH);
 {
  myStepper.step(400);
 
  Floor=Floor-2;
 }
 if(Floor==3 && Floor2==HIGH);
 {
 myStepper.step(400);
 Floor--;  
 }
 if(Floor==3 && Floor3==HIGH);
 {
  myStepper.step(400);
 Floor=Floor;
 }
 if(Floor==3 && Floor4==HIGH);
 {
   myStepper.step(400);
  Floor++;
 }
 }
 break;
 
 case 4:
 {if (Floor==4 && Floor1==HIGH);
 {
 myStepper.step(400);
 
  Floor=Floor-3;
 }
 if(Floor==4 && Floor2==HIGH);
 {
 myStepper.step(400);
 Floor=Floor-2;
 }
 if(Floor==4 && Floor3==HIGH);
 {
  myStepper.step(400);
   
   
 Floor=Floor-1;
 }
 if(Floor==1 && Floor4==HIGH);
 {
 myStepper.step(400);  
  Floor=Floor;
 }
 }
 break;
 

 }  
}


as you can see there its also a hall effect sensor, the sensor its on the elevator and on the floor are magnets, but im not so sure how to increment or decrement the hallcounter, so the stepper spin on the direction it needs, but when the counter reches the floor its was called the sterpper stops..

for example if i move the elevator from the floor 1 till the 4, the hallcounter need to reach 4 for the stepper to stops, in the code says
Code: [Select]
if(Floor==1 && Floor4==HIGH);
 {
 myStepper.step(400);

but i want to change the 400 for something that only moves the stepper on the CCW direction and when the hall counter reach for the stepper stops
Code: [Select]
if(Floor==1 && Floor4==HIGH);
 {
 myStepper.step(foward);
if hallcounter==4
if(Floor==1 && Floor4==HIGH);
 {
 myStepper.step(stops);  

this can be done??

AWOL

Code: [Select]
if (hall==HIGH);{
I stopped reading there.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

copachino


AWOL

Yes, and there are lots of them. (I fibbed about stopping reading)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

copachino

im rally noob on arduino programming, and i suck on programmig in c as well, so if you can point the mistakes, you can help me a lot....

the idea on that part its that always when the hall sensor gets high increment the hall counter, so i can stop the stepper right on the for its needed

wildbill

Here's another classic mistake (two on one line no less!):
Code: [Select]
 if (hall==HIGH)
You're testing the pin number against HIGH. Hall is 12, it'll never be HIGH. You need to be using digitalRead. It might help to change hall to hallpin to remind you.

AWOL

Another potential problem is that even  when you read the Hall effect device, the HIGH may persist for several "loop()" cycles.
You're better of detecting when the state changes (high to low or low to high), not when it is in a single state.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

copachino

#7
Nov 18, 2012, 08:31 pm Last Edit: Nov 18, 2012, 08:40 pm by copachino Reason: 1
Code: [Select]
hall=digitalRead;
  if digitalRead==HIGH;
hallcount++;


or i am still in the same?? maybe im dumb...

also in the cases those Floor==HIGH are wrong... or not
Code: [Select]
{if (Floor==1 && Floor1==HIGH);


the best would be to use interrupts on the hall pin
Code: [Select]
attachInterrupt(0, count, RISING);
void count{
hallcount++








AWOL

What is the value of Floor1?
Is it ever going to be equal to HIGH?

Have you ever seen a semicolon at the end of an "if" in any of the example code?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

copachino

#9
Nov 18, 2012, 08:46 pm Last Edit: Nov 18, 2012, 08:49 pm by copachino Reason: 1
Floor1,2,3,4 are always LOW, its activaded with a push buttom so when it changes from LOW to HIGH the if code can be executed

and if i dont use a semicolon afer the "if" the IDE ask for it.... that why i used then

AWOL

Quote
Floor1,2,3,4 are always LOW

No, they're not.
Code: [Select]
const int  Floor1 = 2;
const int  Floor2 = 3;
const int  Floor3 = 4;
const int  Floor4 = 5;
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

copachino

that what it makes me doubt when i write it,

i didnt know if arduino recognize the state of the push buttom or the pin the buttom its attached to..

AWOL

Quote
i didnt know if arduino recognize the state of the push buttom or the pin the buttom its attached to.
you don't know the state of a pin until you read it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

copachino

#13
Nov 18, 2012, 08:58 pm Last Edit: Nov 18, 2012, 09:17 pm by copachino Reason: 1
i see, but its better to read the buttoms in the case sentence or before the switch???

reading the state of the pins would be like this right
Code: [Select]
  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;

tuxduino

Have you tried to compile and run the examples included with the Arduino IDE ?

Go Up