Timing is way off : 1 sec, instead of 1/10 sec

After testing using analogWrite's PWM to discharge a 5w 1 ohm resistor at different rates (100ma-1500ma) -- It works --, then my arduino nano freezes!!!! and afterward, the timing is way off. << if((millis()%100) == 0)count++; >> gives me around 1 second, instead of 1/10 sec.

I connect PWM pin 3 to the gate of an n-channel mosfet (I don't think I need a resistor here); drain to my battery + terminal (totally isolated from my board); Source to common ground.

what could possibly be the problem here? My nano still works, but timing is way off.


uploaded the sample "blink" code; timing is fine, and my board is good. It blinks at 1 sec

and here is my simple testing sketch:

#include <Wire.h> 
#include "ClickButton.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address/ pins


ClickButton ba(9, LOW, CLICKBTN_PULLUP);
ClickButton bb(8, LOW, CLICKBTN_PULLUP);

int count=0;
int test=0;
int beep=0;
int pwm=0;

//==========

void setup()
{
  
    Serial.begin(9600); 
  pinMode(2, OUTPUT);
  lcd.begin(16,2);               // initialize the lcd   
  ba.multiclickTime = 50;
  bb.multiclickTime = 50;    

}

//==========
void loop()
{
  ba.Update();
  bb.Update();  
  
    Serial.println(count); 
    
    
  test=constrain (test, 0, 100);
  pwm= map (test, 0,100, 0, 255);

 if(beep==1)analogWrite(3, pwm);
 else analogWrite(3, 0);
 
 lcd.setCursor (0,1); 
 lcd.print (beep); 
 
 lcd.setCursor (12,1); 
 lcd.print (count); 
 
 char x[3];
 sprintf(x, "%03d", test);
 lcd.setCursor (13,0); 
 lcd.print (x);  
 
 if((millis()%1000) == 0)count++;
 
 if(ba.clicks==1) test+=10;
 if(bb.clicks==1) test-=10;  
 
 if(ba.clicks==-1) beep=1;
 if(bb.clicks==-1) beep=0;     
  
}

=======

  test=constrain (test, 0, 100);
  pwm= map (test, 0,100, 0, 255);

Why? You haven't done anything to test.

 char x[3];
 sprintf(x, "%03d", test);

Are three characters AND a terminating NULL really expected to fit in a three element array? Not in any three element array I've ever allocated they won't.

How do you discharge a 5w 1 ohm resistor?

PaulS:

  test=constrain (test, 0, 100);

pwm= map (test, 0,100, 0, 255);



Why? You haven't done anything to test.



char x[3];
sprintf(x, "%03d", test);



Are three characters AND a terminating NULL really expected to fit in a three element array? Not in any three element array I've ever allocated they won't.

I agree with you half. Test is the percentage that I use for PWM, and I don't want it to go up more than 100 or lower than 0; since I have buttons to change the "test" value.

I will change to [4], to see if this is the main problem. Thank you for spotting this for me.

timing is still off; this give me 1 sec, again.

 char x[4];
 sprintf(x, "%03d", test);
 lcd.setCursor (13,0); 
 lcd.print (x);  
 
 if(millis()%100 == 0)count++;

I agree with you half. Test is the percentage that I use for PWM, and I don't want it to go up more than 100 or lower than 0; since I have buttons to change the "test" value.

So, you test that it is in range and you map it, then you diddle with the value. You've got the cart on the wrong end of that horse.

steinie44:
How do you discharge a 5w 1 ohm resistor?

actually it's a lithium battery that I was trying to discharge.

PaulS:

I agree with you half. Test is the percentage that I use for PWM, and I don't want it to go up more than 100 or lower than 0; since I have buttons to change the "test" value.

So, you test that it is in range and you map it, then you diddle with the value. You've got the cart on the wrong end of that horse.

I don't think the horse will mind for now, and I will figure it out later. but I want to know why my timing is off that much?

I don't think the horse will mind for now, and I will figure it out later.

Whatever. It requires moving three lines of code, one of which is a blank line.

but I want to know why my timing is off that much?

Have you fixed the memory corruption problem? Nothing is guaranteed to work if you are corrupting memory.

PaulS:

I don't think the horse will mind for now, and I will figure it out later.

Whatever. It requires moving three lines of code, one of which is a blank line.

but I want to know why my timing is off that much?

Have you fixed the memory corruption problem? Nothing is guaranteed to work if you are corrupting memory.

I removed that part of code that you think are the trouble. Same result. timing is off. 1/10 sec timing takes around 1 sec.

Here is the reduced code:

#include <Wire.h> 
#include "ClickButton.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address/ pins


ClickButton ba(9, LOW, CLICKBTN_PULLUP);
ClickButton bb(8, LOW, CLICKBTN_PULLUP);

int count=0;
int test=0;
int beep=0;
int pwm=0;

//==================

void setup()
{
  
  pinMode(2, OUTPUT);
  lcd.begin(16,2);               // initialize the lcd   

}

//==================
void loop()
{
  ba.Update();
  bb.Update();  


 if(beep==1)analogWrite(3, test);
 else analogWrite(3, 0);
 
 lcd.setCursor (15,1); 
 lcd.print (beep); 
 
 lcd.setCursor (0,0); 
 lcd.print (count); 
 
 char x[4];
 sprintf(x, "%03d", test);
 lcd.setCursor (13,0); 
 lcd.print (x);  
 
 if(millis()%100 == 0)count++;
 
 if(ba.clicks==1) test+=10;
 if(bb.clicks==1) test-=10;  
 
 if(ba.clicks==-1) beep=1;
 if(bb.clicks==-1) beep=0;     
  
}
if(millis()%100 == 0)

And if you miss just one?

if(millis()%100 == 0)count++;

This is a stupid piece of code.

It assumes that your code will get a result from millis( ) of an exact multiple of 100, every time. It won't. You might even get two.

if(millis()%100 == 0)count++; Is a problem.
I tried a smaller loop, and I get 8 or 10 at a time.
Then a longer loop and I get missing counts.

michinyon:

if(millis()%100 == 0)count++;

This is a stupid piece of code.

It assumes that your code will get a result from millis( ) of an exact multiple of 100, every time. It won't. You might even get two.

I got that from a book. I am not smart enough to have come up with this. I tried this line of codes many times without error. Guess that is the problem.