I have looked trough countess guides but most of the answers posted seems to not quite fit my situation. An elevator.
I am struggeling to set a counter that relays on the current floor reading (counter) as well as "current floor" and after countless hours of tinkering, I do apologise for my code beeing "a bit of a mess". Please bear with me.
The idea is to start the code with the floor beeing "1", and then count the floor (push button 13) to the correct floor. Either +1 (motor UP) or -1 (motor DOWN).
Setup:
I am using a push button to simulate the changing of floors (later, magnetic sensor).
I am using a push button to "call" the elevator, either up or down on your current floor.
I am also using an H-bridge to change the motor direction (will change later) but I am struggeling with the code.
The issue its self:
I cannot for the life of me get the counter to work correctly.
I cannot find the correct code for the "UP" and "DOWN" motor oriantation.
What am I supposed to use to run the "?????????????" part?
Greatly appriciate all help!
int levelOneUp = 0; // Button UP floor 1
int levelTwoUp = 0; // Button UP floor 2
int levelThreeUp = 0; // Button UP floor 3
int levelTwoDown = 0; // Button DOWN floor 2
int levelThreeDown = 0; // Button DOWN floor 3
int levelFourDown = 0; // Button DOWN floor 4
int counter1 = 1; // Counter floor
int counter2 = 1; // Button location
int up; // Setting for running the motor UP
int down; // Setting for running the motor DOWN
void setup()
{
Serial.begin (9600);
Serial.print("Floor ");Serial.println(counter); delayMicroseconds(300);
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
pinMode (5, INPUT_PULLUP);
pinMode (6, INPUT_PULLUP);
pinMode (7, INPUT_PULLUP);
pinMode (8, INPUT_PULLUP);
pinMode (9, INPUT_PULLUP);
pinMode (10, INPUT_PULLUP);
pinMode (13, INPUT_PULLUP);
}
void loop()
{
digitalWrite(2, HIGH);
levelOneUp = digitalRead(5);
levelTwoUp = digitalRead(6);
levelTwoDown = digitalRead(7);
levelThreeUp = digitalRead(8);
levelThreeDown = digitalRead(9);
levelFourDown = digitalRead(10);
---------------------------------- Counter function ----------------------------------
if ((digitalRead(13) == LOW) && (up == 1)){
counter = counter + 1;}
if ((digitalRead(13) && (down == 1)){
counter = counter - 1;}
---------------------------------- Motor direction ----------------------------------
if (up = 1){
digitalWrite (4, HIGH);
digitalWrite (5, LOW);}
else{
digitalWrite (4, LOW);
digitalWrite (5, LOW);}
if (down = 1){
digitalWrite (4, LOW);
digitalWrite (5, HIGH);}
else{
digitalWrite (4, LOW);
digitalWrite (5, LOW);}
----------------------------------Programming 1st floor up----------------------------------
if ((digitalRead(5) == LOW) && (counter < 1)) {
?????????? (Down = 1);
??????????????????????????????
}
Yea, that was my first guess aswell.
I have tryed bouth " if (up == LOW)", " (if up = LOW)", "if (up == 1)", "if (up = 1)", if "(up == 0)" and "if (up = 0)".
neither seems to fix the issue.
Up and down are variables...
Upon reading the buttons, you should derive whether the elevator should go up or down.
If the elevator should go up, you should set the variable up to true or one or whatever. You do not do that... so nothing will ever happen...
That is the part that is supposed to do the "up" and "down" command, but without a functioning counter the entire code is sitting still (sending floor 1).
I have tryed breaking the code down mutiple ways but I get stuck at the same two issues.
Counter not working
"up" and "down" not working (not sending as I am missing the statement).
Remove all the ????, write Down with small d and add a }...
Maybe it is easier to have one variable named direction.
Set to 1 if up, set to -1 if down, set to 0 if nothing needs to happen...
You are correct. Thouse is an issue in the code as typed.
This was done to show where I am having issues (missing the statement) as well as to show the code of the counter I a having issued with.
The raw code is as following:
int levelOneUp = 0; // Button UP floor 1
int levelTwoUp = 0; // Button UP floor 2
int levelThreeUp = 0; // Button UP floor 3
int levelTwoDown = 0; // Button DOWN floor 2
int levelThreeDown = 0; // Button DOWN floor 3
int levelFourDown = 0; // Button DOWN floor 4
int counter1 = 1; // Counter floor
int counter2 = 1; // Pressed button location
int up; // Setting for running the motor UP
int down; // Setting for running the motor DOWN
void setup()
{
Serial.begin (9600);
Serial.print("Floor ");Serial.println(counter1); delayMicroseconds(300);
pinMode (2, OUTPUT); // Chip positive (H-chip)
pinMode (3, OUTPUT); // Positive when motor going UP
pinMode (4, OUTPUT); // Negative when motor going UP
pinMode (5, INPUT_PULLUP); // 1st floor UP
pinMode (6, INPUT_PULLUP); // 2nd floor DOWN
pinMode (7, INPUT_PULLUP); // 2nd floor UP
pinMode (8, INPUT_PULLUP); // 3rd foor UP
pinMode (9, INPUT_PULLUP); // 3rd floor DOWN
pinMode (10, INPUT_PULLUP); // 4th floor UP
pinMode (13, INPUT_PULLUP); // Sensor (Push button)
}
void loop()
{
digitalWrite(2, HIGH); // H-Chip on
levelOneUp = digitalRead(5); // 1st Floor UP
levelTwoUp = digitalRead(6); // 2nd floor DOWN
levelTwoDown = digitalRead(7); // 2nd floor UP
levelThreeUp = digitalRead(8); // 3rd floor DOWN
levelThreeDown = digitalRead(9); // 3rd floor UP
levelFourDown = digitalRead(10); // 4th floor DOWN
if ((digitalRead(13) == LOW) && (up == 1)){ // Counter going UP
counter1 = counter1 + 1;}
if ((digitalRead(13) && (down == 1))){ // Counter going DOWN
counter1 = counter1 - 1;}
}
Up is a global variable.
You are lucky. Global variables are initialized by default.
You have bad luck. It is initialized by default to 0.
Zero is not equal to 1 and therefore your counter will never count...