I double check it is working but amp must be greater than 100, you can remove EEPROM for your safety . The strange thing is happening, when I when I divide by amp bay any number , numbers on serial monitor are not changing, without dividing is ok. //Serial.println(amp/3700); = not good
Serial.println(amp) = ok
what exactly do you mean "its working"? just that the program is running? or are you measuring the output on a scope?
You're not actually using any of the timer stuff you setup. If your timer is running, its not doing anything. You need an attachinterrupt() call & a function to be called when the ISR hits. if I understand how you set it up(CTC), that vector should be TIMER1_COMPB_vect. Then write the function to do what you want to happen when it hits. If its just a pwm, then that function is just a pin toggle(preferably by direct port manipulation). Keep the ISR short, definitely no serial prints from within.
why are you writing to eeprom so frequently? That seems like a terrible idea.
when messing with registrations, its best to use cli(); & sei() to stop & start interrupts. just add the cli before you start & the sei when your done. Not doing this can cause generally inconsistent things to happen.
interrupts() & noInterrupts() does that same thing, as i recall they are just macros for cli & sei
edit: here is a good primer on interrupts, as well as a table with all the ISR vectors incase the one I stated is incorrect: all about interrupts
ok tom, that gives me a much better idea whats going on.
as a test, try adding cli() & sei() to your epprom writes. Since you say it worked at lower frequency, I suspect writing to epprom is taking longer then the time between interrupts, thus causing it to fail. just a theory, but easy to test:
cli();
EEPROM.write(0, amp);
sei();
note that you will loose a few interrupts in doing this, if thats critical to your application, you'll have to find a work around. at least you can confirm or rule out my theory.
i thought I did in #15? I'd rather explain how it works thats just give you the code.
It looks like your epprom writes are commented out, assuming thats where where your writes were happening when it was working, you need to add cli(); or noInterrupts(); before the write line. This turns off all interrupts so you're not interrupted in the middle of a write (as I've speculated is happening) When your write is complete sei(); or interrupts(); Will resume interrupts so your program can continue
in summary: cli() the same as noInterrupts() --no arguments, just stops them
sei() is equivalent to interrupts() -no arguments, resumes interrupts.