Very! Basic counter

Hey!

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:

  1. I cannot for the life of me get the counter to work correctly.
  2. I cannot find the correct code for the "UP" and "DOWN" motor oriantation.
  3. What am I supposed to use to run the "?????????????" part?

Greatly appriciate all help! :slight_smile:

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);

??????????????????????????????


}

Classic one...
You probably should use ==.

Where do you set up?
Same for down...

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.

"Where do you set up", that part I did not understand.

To be more clear: use == in an if ().
Otherwise the value one is stored in the variable up. That is valid c++, but almost certainly not what you want.

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

Ah, now I see.

Yes, you are absolutely correct as I have not gotten to that part into the code correctly (not sure what statements to use)

if ((digitalRead(5) == LOW)  && (counter < 1)) {
 ?????????? (Down = 1);
??????????????????????????????"

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.

  1. Counter not working
  2. "up" and "down" not working (not sending as I am missing the statement).

This line has one-too-few close parenthesis:

  if ((digitalRead(13) && (down == 1)) {
     12           3  2    3         21x

Should be:

  if ((digitalRead(13) && (down == 1))) {
     12           3  2    3         210

Sending the image of thinkercad. Bouth "options" :slight_smile:

Wrong line... and I guess you found/fixed it.

Yea, added that. Just a type'o as I am running on my 10th hour with the damn script xD

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

Awsome!

So that is proboly the issue with the motor running then. Changing it to "motorUP" and "motorDOWN".

Any suggestion on the counter tho?

Code 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 motorUP;                             		// Setting for running the motor UP
int motorDOWN;                           		// 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){
counter1 = counter1 + 1;}
}

Right now, with no debounce on pin 13, you get about 100 counts per button press.

It should count like this...
The debouncing might indeed be a problem...

I get 0 tho.

How can you know?
There is no serial.print in your code...