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;
}
=======