Okay, this is my third update of my thread, and I think it's going to be my last. I have more or less solved the problem of me not being able to update an integer every 60 seconds, and I guess I'm just trying to make a small explanation of how I fixed it and some small sketches of code that can help you fix the same problem if you're somehow stuck in the same situation.
This was the specific bit of code I was having problems with. And the code below is my current version of the code, which I think works correctly.
if (millis() + runtime >= runtime + 60000 ) {
runtime = runtime + millis();
Serial.println("runtime update");
}
if (millis() - runtime >= timeupdatedelay ) {
runtime += millis()
Serial.println("runtime update");
}
How I solved the problem was instead of doing plus, I used minus, and then added another integer. And I also found out that if I ever want to add two valuables together, which was the thing I was having problems with in the beginning, I should just do this thing +=
This thing basically takes itself plus another integer as the new value. If you want more information, you could go to the Arduino reference sheet.
If you're interested, my new code will be at the bottom of the page. Thank you for watching.
Anything that is written below this line is my initial explanation of the problem I was having, which actually isn't a very good explanation of the problem I was having, I realize now, much later.
I have this small problem in my code when I want runtime to be equal to runtime plus how long the device has been turned on. But I don't know how to add the variables together. This is my full code. Any help is appreciated
Just one more question when I'm already creating a thread. Is there a way to get a bar graph in the serial monitor in the Arduino IDE? Because the example bar graph sketch that they give you requires you to use different software, and it doesn't work like I want it to. I want it to just be like the bar graph you use on an LCD display?
const int analogWriteSpeed = 55;
const int motorpin1 = 5; // Example valid pin number
const int motorpin2 = 6; // Example valid pin number
const int buttonPin = 2; // Pin where the button is connected
unsigned long shortPressCount = 0;
unsigned long runtime = 0;
unsigned long buttonPressTime = 0;
const long timeupdatedelay = 60000;
bool motorstart = false;
int bitch;
void shortPressAction();
void longPressAction();
void setup() {
Serial.begin(9600);
while (Serial) {}
Serial.println("hello world");
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Using internal pull-up resistor
}
void loop() {
bool buttonState = digitalRead(buttonPin);
//Serial.println(buttonState);
delay(100);
if (buttonState == HIGH ) {
bitch = millis();
while(digitalRead(buttonPin) == HIGH){
delay(100);
Serial.println("help");
}
if(millis() - bitch >= 3000) {
//Serial.println("Long press triggered?");
longPressAction();
}
if (millis() - bitch >= 50 ) {
motorstart = true;
}
}
//This is the bottom of void loop
}
void loop1(){
if (millis() - runtime >= timeupdatedelay ) {
runtime += millis()
Serial.println("runtime update");
}
if (motorstart == true) {
shortPressAction();
}
delay(100);
}
void longPressAction() {
Serial.begin(9600);
Serial.println("Long press action triggered");
analogWriteSpeed += 10;
if (Serial) {
unsigned long totalMinutes = runtime / 60000;
unsigned long minutes = totalMinutes % 60;
unsigned long totalHours = totalMinutes / 60;
unsigned long hours = totalHours % 24;
unsigned long days = totalHours / 24;
Serial.print("Device run time: ");
Serial.print(days);
Serial.print(" days, ");
Serial.print(hours);
Serial.print(" hours, ");
Serial.print(minutes);
Serial.println(" minutes");
Serial.print("Short press action triggered ");
Serial.print(shortPressCount);
Serial.println(" times");
}
}
void shortPressAction(){
Serial.println("shortPressAction triggered");
shortPressCount ++;
for (int i = 0; i < 10; i++) {
digitalWrite(LED_BUILTIN, LOW);
analogWrite(motorpin1, analogWriteSpeed);
delay(2000);
digitalWrite(motorpin1, LOW);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
analogWrite(motorpin2, analogWriteSpeed);
delay(2000);
digitalWrite(motorpin2, LOW);
delay(1000);
}
motorstart = false;
digitalWrite(LED_BUILTIN, LOW);
}