我已經開始用arduino編程三層電梯。到目前爲止,我可以設法從每個樓層呼叫電梯,並將轎廂移向選定的樓層。現在,我試圖將按下的按鈕存儲在一個陣列中,並按照呼叫順序將電梯移動到選定的樓層。然而,汽車僅移動到一個樓層並停在那裏,並且不會繼續到下一個選定的樓層。這是我所做的。Arduino電梯
const int maxfloors = 3;
byte carcall_buttons[maxfloors] = {22,24,46};
byte floor_sensors[maxfloors] = {25,26,47};
int buttonstate[3];
int sensorstate[3];
boolean registered[3] = {false, false, false};
int lastbuttonstate0 = LOW;
int lastbuttonstate1 = LOW;
int lastbuttonstate2 = LOW;
int lastsensorstate0 = LOW;
int lastsensorstate1 = LOW;
int lastsensorstate2 = LOW;
const byte led1 = 27;
const byte led2 = 23;
const byte led3 = 48;
const int motor_up = 41;
const int motor_down = 42;
const int En = 40;
enum state {down,stop};
state elevator_state;
void setup()
{
for (int i = 0; i<maxfloors; i++)
{
pinMode (carcall_buttons[i], INPUT);
pinMode (floor_sensors[i], INPUT);
}
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode(motor_up, OUTPUT);
pinMode(motor_down, OUTPUT);
pinMode(En, OUTPUT);
Serial.begin (9600);
}
void loop()
{
ReadButtons();
ReadSensors();
FloorSelection();
static int elevator_state = stop;
switch (elevator_state)
{
case down:
if (registered[0] == true || registered[1] == true)
{
if (sensorstate[2] == LOW)
{
movedown();
elevator_state = stop;
}
}
break;
case stop:
if (registered[1] == true && sensorstate[1] == LOW)
{
motor_stop();
registered[1] = false;
for (int i=0;i<maxfloors;i++)
{
Serial.println (registered[i]);
}
}
if (registered[0] == true && sensorstate[0] == LOW)
{
motor_stop();
registered[0] = false;
for (int i=0;i<maxfloors;i++)
{
Serial.println (registered[i]);
}
}
break;
}
}
void ReadButtons()
{
for (int i=0;i<maxfloors;i++)
{
buttonstate[i] = digitalRead (carcall_buttons[i]);
}
}
void ReadSensors()
{
for (int i=0;i<maxfloors;i++)
{
sensorstate[i] = digitalRead (floor_sensors[i]);
}
}
void FloorSelection()
{
for (int i=0;i<maxfloors;i++)
{
if (buttonstate[0] != lastbuttonstate0)
{
if (buttonstate[0] == HIGH)
{
registered [0] = true;
delay(100);
Serial.println (registered [i]);
}
}
if (buttonstate[1] != lastbuttonstate1)
{
if (buttonstate[1] == HIGH)
{
registered [1] = true;
delay(100);
Serial.println (registered [i]);
}
}
if (buttonstate[2] != lastbuttonstate2)
{
if (buttonstate[2] == HIGH)
{
registered [2] = true;
delay(100);
Serial.println (registered [i]);
}
}
}
}
void motor_stop()
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
Serial.println ("idle");
digitalWrite(motor_up, LOW);
digitalWrite(motor_down, LOW);
digitalWrite(En, LOW);
}
void moveup()
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
Serial.println ("up");
digitalWrite(motor_up, LOW);
digitalWrite(motor_down, HIGH);
digitalWrite(En, HIGH);
}
void movedown()
{
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
Serial.println ("down");
digitalWrite(motor_up, LOW);
digitalWrite(motor_down, HIGH);
digitalWrite(En, HIGH);
}