Go Down

Topic: Arduino elevator using the side kick basic kit servo (Read 1 time) previous topic - next topic

MorganS

Quote
Code: [Select]
    digitalWrite(liftArray[0], verdiepingNulState);
    digitalWrite(liftArray[1], verdiepingEenState);
    digitalWrite(liftArray[2], verdiepingTweeState);
 
What is going on here? You are writing to pins numbered with booleans? There is no pin TRUE on the Arduino circuit board.

You do not need interrupts so long as you only expect to press a button when the lift is stationary. Or you re-write without delay().
"The problem is in the code you didn't post."

Piring

Hello!

I thought about my problem and i think i found it. This is my code :

Code: [Select]
#include <Servo.h>

//Alles voor het display
int a = 6;
int b = 5;
int c = 3;
int d = 2;
int e = 1;
int f = 7;
int g = 8;

//Alles voor de drukknoppen
int verdiepingNul = 14;
int verdiepingEen = 15;
int verdiepingTwee = 16;

//Alles voor de servo
Servo myServo;
int pos=0;
int currentFloor;
//Array voor de wachtrij
boolean floorRequested[3];

void setup() {
  Serial.begin(9600);
 pinMode(a, OUTPUT);
 pinMode(b, OUTPUT);
 pinMode(c, OUTPUT);
 pinMode(d, OUTPUT);
 pinMode(e, OUTPUT);
 pinMode(f, OUTPUT);
 pinMode(g, OUTPUT);

 pinMode(9, INPUT);
 pinMode(10, INPUT);
 pinMode(11, INPUT);

 myServo.attach(12);
 myServo.write(0);
 int currentFloor=myServo.read();

 Serial.begin(9600);
 }

void displayDigit(int digit){
  if(digit ==0){
    digitalWrite(a,HIGH);
    digitalWrite(b,HIGH);
    digitalWrite(c,HIGH);
    digitalWrite(d,HIGH);
    digitalWrite(e,HIGH);
    digitalWrite(f,HIGH);
    digitalWrite(g,LOW);
  }
  else if(digit ==1) {
    digitalWrite(a,LOW);
    digitalWrite(b,HIGH);
    digitalWrite(c,HIGH);
    digitalWrite(d,LOW);
    digitalWrite(e,LOW);
    digitalWrite(f,LOW);
    digitalWrite(g,LOW);
  }
  else if(digit ==2){
    digitalWrite(a,HIGH);
    digitalWrite(b,HIGH);
    digitalWrite(c,LOW);
    digitalWrite(d,HIGH);
    digitalWrite(e,HIGH);
    digitalWrite(f,LOW);
    digitalWrite(g,HIGH);
  }

}
 
  void loop() {
  if(digitalRead(verdiepingNul)==HIGH){
    floorRequested[0]=true;
  }
 
  if(digitalRead(verdiepingEen)==HIGH){
    floorRequested[1]=true;
  }

  if(digitalRead(verdiepingTwee)==HIGH){
    floorRequested[2]=true;
  }

  for(int i=0; i<3; i++){
    int floorCalculation = i * 90;
    if(floorRequested[i]==true){
      if(currentFloor > floorCalculation){
        for(currentFloor ; currentFloor>= floorCalculation; currentFloor--){
          myServo.write(currentFloor);
          delay(30);
        }
      }
      if(currentFloor < floorCalculation){
        for(currentFloor; currentFloor <= floorCalculation; currentFloor++){
          myServo.write(currentFloor);
          delay(30);
          }
        }
      }
      delay(30);
      floorRequested[i]=false;
    }
    Serial.println(floorRequested[1]);
  }


Now i have the other problem that i don't know how i can make a queue that can save the steps in which the buttons were pressed. Any thoughts on that?


Robin2

i don't know how i can make a queue that can save the steps in which the buttons were pressed. Any thoughts on that?
Elevators don't usually work like that. If they are going up they usually respond to higher calls before starting back down even if the lower call came first. They only start going down if there are no higher calls.

It looks like you are a good distance into the correct solution but I don't think your code is there yet. And it is certainly more complicated than it needs to be. Break your code into functions. As an example, I don't think you should be using FOR because an elevator just moves one place to another in any move. It will not make several successive moves as a group. Also it should be able to go from 1 to 3 without stopping at 2.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Piring

Hey Robin, which for loop do you mean?

If i delete the for loop with the motor movement, the motor will turn quick. This for will actually care for the slower movement of the motor.

Robin2

If i delete the for loop with the motor movement, the motor will turn quick. This for will actually care for the slower movement of the motor.
I guess I misunderstood your code. I thought it was effectively doing for (floor = 0; floor <= max; floor ++)

Perhaps another reason for writing your code more clearly :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MorganS

if( the_elevator_is_moving_up() && it_has_been_more_than_30ms_since_last_move()) {
  pos++;
  myServo.write(pos);
  last_move = millis();
}

Simple. Now the servo moves slowly.
"The problem is in the code you didn't post."

Robin2

Simple. Now the servo moves slowly.
Yeah. But it should be in a little function and separate from the main code - I think I would call the function moveOneFloorSlowly()

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up