Go Down

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

tuxduino

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


With all those misplaced semicolons anything can happen.

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,

copachino

#22
Nov 19, 2012, 01:44 am Last Edit: Nov 19, 2012, 02:14 am by copachino Reason: 1

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:
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 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



copachino


Quote
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

tuxduino

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

Go Up