Notifications
Clear all

Trouble with millis() why is this not working

24 Posts
4 Users
2 Likes
1,099 Views
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

@danevilsΒ 

Hope you don't mind but I can't help dabbling in the problem πŸ™‚

Here is how I turn each relay (actually a LED in my setup test) one at a time.

My LEDs are actually connected to a Mega so the pin numbers are not the ones you are using.

Β 

int relay[4] = {34, 35, 36, 37};  // LED PINS


unsigned long previousTime;
unsigned long currentTime;


int currentRelay = 0;


void setup() {
  Serial.begin(115200);
  // initialize pin numbers
  for (int i = 0; i<4; i++){
    pinMode(relay[i],OUTPUT);
  }

  previousTime = millis();
  digitalWrite(currentRelay,HIGH);  // turn on first relay

}


void loop() {

  unsigned long currentTime = millis();


  if (currentTime - previousTime > 3000) {

    previousTime = millis();
    digitalWrite(relay[currentRelay], LOW);  // turn off current LED
    currentRelay = currentRelay + 1;  // next relay
    if (currentRelay > 3){
      currentRelay = 0;               // reset to zero
    }

    digitalWrite(relay[currentRelay],HIGH);  // turn on next LED

  }
  
}

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@danevils Ok, I was afraid of that. So you are NOT building a battery charger, you are just turning off and on a commercial charger. I assume you understand that turning off a proper battery maintainer (NOT a so called trickle charger) is a bad idea. If you left the charger on each battery for 1 or 2 weeks then it may not be so bad, but not ideal.

The best solution would be to 'read' the state of charge then you could use that to know when to turn a charger off.Β 

If there is a light that indicates FULL, mounting a detector above it would be one crude solution, better would be to tap into the circuit that drives the light. I would still build in some sort of delay, say 2 weeks to a month between connecting the charger.

Since you will be connecting and disconnecting DC leads possibly under heavy load, consider using Solid State Relays or MOSFET's rather than mechanical relays as arcing may be a problem.

Good luck.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

Posted by: @zander

@danevils Ok, I was afraid of that. So you are NOT building a battery charger, you are just turning off and on a commercial charger.

Β 

He did state that the setup was essentially to replace manually using the charger one at a time for his four batteries. It would make sense to read up about charging lead acid batteries to work out the correct protocol even if it was being done manually using that particular charger.

The modern type of batteries used in hand tools and electric scooters and so on all use electronics to prevent damage or fires unless cheap rubbish is purchased as they have caught alight when left on a charger often resulting in burning down a house if left inside! With my hand drill the electronics is in the battery itself to prevent the voltage dropping too far during use and when plugged into the wall for charging.

Β 

Β 

Β 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@robotbuilder Can you show me where he said that in the original post?

Working on a DIY project to charge Multiple battery's Lawnmower, Motorcycle etc...Β  The problem I am Having is the Serial.print only refreshes after relays run through it's loop. boardΒ  UNO Rev3 looking it my code Serial.print should refresh "printDelay = 50; the relays are batteryDetectTime = 300; my concept: in a if Control Structure the UNO Rev3 will set relay HIGH a acs712 sensor will detect if battery is connected if "true" it will charge battery if "false" it will go to next relay and I want to print status to a screen (type unknown at this time) I can't have screen refreshing every four min if four battery's are connected

CanΒ  someone point me in the right direction?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2528
 

@zanderΒ 

It's not in the original post but shows up here ...

https://forum.dronebotworkshop.com/postid/37469/

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@will Somehow I didn't see that. But now I see he talks about taking the charger off every hour!!! Maintainers are designed to be put on and left until the battery goes back into service. Constantly putting it on charge then off is bad for the battery.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
DanEvils reacted
ReplyQuote
DanEvils
(@danevils)
Member
Joined: 2 years ago
Posts: 5
Topic starter  

Thanks Everyone I resolved my problem,Β  I change how my relays cycle and threw millis() to the curb.

for (int relay = 2; relay < 10; relay++)
Β  Β digitalWrite(relay, HIGH);
Β  Β Serial.print(" Relay Value I NeedΒ  ");
Β  Β delay();
Β  Β digitalWrite(relay, LOW);
ETCETERA, ETCETERA, ETCETERA
Β 

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@danevils You still don't understand, a battery maintainer which is what you need to overwinter your batteries is supposed to stay connected the entire season, not be repeatedly taken off and put on. Buy one maintainer for each battery, it will be a lot cheaper in the long run.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
DanEvils
(@danevils)
Member
Joined: 2 years ago
Posts: 5
Topic starter  

Ok Thanks


   
ReplyQuote
Page 2 / 2