I am trying to assign values to an integer ‘move_time’ in the void ‘get_move_time’ by reading the status of four digital inputs. I have created an array for testing and cannot get the ‘if’ statements to assign the correct value to ‘move_time’.
I will appreciate your help.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
#include <TM1637Display.h>
#define start_f 6
// define input pins with internal pull-up
#define s90 10
#define s120 11
#define s180 12
#define Test_s 13
int time_scale = 4;
int time_pins[4] = { s90, s120, s180, Test_s };
int move_time;
boolean start_flag = false;
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0);
int w = 0;
while (w < time_scale) { // sets the time setting ports with internal pull-up
pinMode(time_pins[w], INPUT_PULLUP);
w++;
}
}
void loop() {
lcd.clear(); // clear display
get_move_time();
lcd.setCursor(9, 1);
lcd.print("M = ");
lcd.print(move_time);
Start_Button();
} // end of main loop
void get_move_time() {
move_time = 20; // set default value for testing
lcd.print("Status ");
int time_status[] = { 1, 0, 1, 1 }; // simulate time settings for testing
for (int z = 0; z < time_scale; z++) {
// time_status[z] = digitalRead(time_pin[z]); read switch settings
if (time_status[z] == 0) {
move_time = 90;
} else if (time_status[z] == 0) {
move_time = 120;
} else if (time_status[z] == 0) {
move_time = 180;
}
lcd.print(time_status[z]);
lcd.print(" ");
}
}
void Start_Button() {
lcd.setCursor(0, 0);
//lcd.print("Press to Start");
while (digitalRead(start_f) == false) start_flag = digitalRead(start_f); // remain here until button is pressed
}
I have tried the ‘time_status[z]’ to be boolean or integer and the condition ‘if’ test to be ‘==0’, ‘==false’, ‘<1’ all with the same result – no assignment for each test, generally assigns ‘180’
Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You are using the same condition in if-else statement which is wrong, I think what you want to accomplish, by default the first statement will be executed which means move_time is 90 all the time.
if (time_status[z] == 0) {
move_time = 90;
} else if (time_status[z] == 0) {
move_time = 120;
} else if (time_status[z] == 0) {
move_time = 180;
}
You can for example tweak the code so that you have more conditions and hence different move_time values are shown, for example change the time_status[] array to have different values that you can loop in, like
int time_status[] = { 0, 1, 2, 3 }; // simulate time settings for testing
And then you can use these values in your conditions.
if (time_status[z] == 0) {
move_time = 10;
} else if (time_status[z] == 1) {
move_time = 20;
} else if (time_status[z] == 2) {
move_time = 30;
}
else if (time_status[z] == 3) {
move_time = 40;
}
As you are trying to print the value of move_time, you should also add some delay so that you notice the values being changed in your LCD, you also need to move the lcd.print(move_time)
from the loop() to your for loop inside get_move_time
.
I tried to test the code in Wokwi which would help you simulate before you use your code on an Arduino, this can be accessed via https://wokwi.com/projects/403549466180697089 .
5