Hi all,
I'm having trouble understanding the following command in the sketch pasted in full below:
if (measurement < setPoint){
pulseWidth += stepSize;
if (pulseWidth > 255) pulseWidth = 255; }
It's a PWM battery chargin sketch, and obviously the PWM increases in duty cycle when the battery drops below a setpoint. I'm having trouble unpacking the nested if statements. Are they independent, or does the second qualify the first? And where is the original pulseWidth value defined, since it seems the first if statement requires a pulseWidth to complete the += command..?
Thanks very much for any help with this,
Brian
FULL SKETCH:
//PWM Solar charge controller using Julian Iletts design and code (http://256.uk)
//but adapted to work on the ATTiny25/45/85 by Adam Welch
//You will need to add the following address to your 'Additional Boards Manager URLs'
//within 'Preferences' in your Arduino IDE:
//https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json
//http://adamwelch.co.uk/2015/12/attiny85-pwm-solar-charge-controller
/*
Reset -1 8- 5 Volts
AI 3 - Pin 3 -2 7- AI 1, SCK, Pin 2
AI 2 - Pin 4 -3 6- Pin 1, PWM, MISO
Ground -4 5- Pin 0, PWM, AREF, MOSI
*/
#include <avr/wdt.h>
const int Rev =2;
//Rev 1 Inital Setup
//Rev 2 Added WatchDog at 4 Seconds
volatile uint8_t* Port[] = {&OCR0A, &OCR0B, &OCR1B};
//const int setPoint = 13.45 * 20 / (20+82) * 1024 / 4.9;
const int setPoint = 13.45 * 20 / (20+82) * 1024 / 5;
int LED = 2;
int measurement = 0;
int pulseWidth = 0;
int difference = 0;
int stepSize = 0;
void setup() {
//WatchDog
watchdogSetup();
//PWM Stuff
TCCR0A = 2<<COM0A0 | 3<<COM0B0 | 3<<WGM00;
TCCR0B = 0<<WGM02 | 1<<CS00;
GTCCR = 1<<PWM1B | 2<<COM1B0;
TCCR1 = 0<<PWM1A | 0<<COM1A0 | 1<<CS10;
/*
// Configure counter/timer0 for fast PWM on PB0 and PB1
TCCR0A = 3<<COM0A0 | 3<<COM0B0 | 3<<WGM00;
TCCR0B = 0<<WGM02 | 3<<CS00; // Optional already set
// Configure counter/timer1 for fast PWM on PB4
GTCCR = 1<<PWM1B | 3<<COM1B0;
TCCR1 = 3<<COM1A0 | 7<<CS10;
*/
analogWrite(0, 117); // OC0A
analogWrite(1, 137); // OC1B
pinMode(LED, OUTPUT); //Pin 2
}
void loop() {
//Reset WatchDog
wdt_reset(); // reset the WDT timer
//Read Battery Voltage
measurement = analogRead(A3);
difference = abs(setPoint - measurement);
stepSize = difference;
if (measurement < setPoint){
pulseWidth += stepSize;
if (pulseWidth > 255) pulseWidth = 255; }
if (measurement > setPoint){
pulseWidth -= stepSize;
if (pulseWidth < 0) pulseWidth = 0; }
analogWrite(4, pulseWidth);
//Led Control
if (pulseWidth > 99){ //Charging
digitalWrite(LED, LOW); //Yellow LED
}
else { //Charged
digitalWrite(LED, HIGH); //Green LED
}
}
void watchdogSetup(void){
cli(); // disable all interrupts
wdt_reset(); // reset the WDT timer
/*
WDTCSR configuration:
WDIE = 1: Interrupt Enable
WDE = 1 :Reset Enable
WDP3 = 0 :For 2000ms Time-out
WDP2 = 1 :For 2000ms Time-out
WDP1 = 1 :For 2000ms Time-out
WDP0 = 1 :For 2000ms Time-out
// Enter Watchdog Configuration mode:
WDTCSR |= (1<<WDCE) | (1<<WDE);
// Set Watchdog settings
//4 seconds at 5.0 volts
WDTCSR = (0<<WDIE) | (1<<WDE) | (1<<WDP3) | (0<<WDP2) | (0<<WDP1) | (0<<WDP0);
//8 seconds at 5 volts
//WDTCSR = (0<<WDIE) | (1<<WDE) | (1<<WDP3) | (0<<WDP2) | (0<<WDP1) | (1<<WDP0);
*/
//Copy and paste from here http://www.re-innovation.co.uk/web12/index.php/en/blog-75/306-sleep-modes-on-attiny85
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
WDTCR = 8;
//WDTCR |= _BV(WDIE);
//WDTCR = (0<<WDIE) | (1<<WDE) | (1<<WDP3) | (0<<WDP2) | (0<<WDP1) | (0<<WDP0);
sei();
}