Pages: [1] 2 3   Go Down
Author Topic: help micro elevator code  (Read 3162 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 341
UNO
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 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:
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:
if(Floor==1 && Floor4==HIGH);
  {
  myStepper.step(foward);
if hallcounter==4
if(Floor==1 && Floor4==HIGH);
  {
  myStepper.step(stops);  
this can be done??
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (hall==HIGH);{
I stopped reading there.
Logged

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

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 341
UNO
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

its that a code mistake???
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, and there are lots of them. (I fibbed about stopping reading)
Logged

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

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 341
UNO
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3679
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's another classic mistake (two on one line no less!):
Code:
 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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 341
UNO
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
{if (Floor==1 && Floor1==HIGH);


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






« Last Edit: November 18, 2012, 02:40:45 pm by copachino » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 341
UNO
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 18, 2012, 02:49:44 pm by copachino » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Floor1,2,3,4 are always LOW
No, they're not.
Code:
const int  Floor1 = 2;
const int  Floor2 = 3;
const int  Floor3 = 4;
const int  Floor4 = 5;
Logged

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

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 341
UNO
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 341
UNO
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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;
« Last Edit: November 18, 2012, 03:17:51 pm by copachino » Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3   Go Up
Jump to: