I have the code to drive 2 independent PWM channels using an ATMEGA2560 board. Each channel is controlled by a pot, analog read then analog write output. Using a 7-segment library two dual digit displays show the duty cycle from 10-99%. It works perfectly for my needs, except for the first 2 seconds after the board has booted up I require the two PWM outputs to be 99% duty cycle and the displays to show 99%, no matter what position the pots are set to, after 2 seconds the outputs and displays will revert to how the pots are set. Anybody any ideas?
[code]
#include <SevSeg.h>
#include "SevSeg.h"
// Using ATMEGA2560
SevSeg sevsegA; // Create display A controller object
SevSeg sevsegB; // Create display B controller object
const int inputPinA = A0; // Pin reading potentiometer A analog voltage
const int inputPinB = A1; // Pin reading potentiometer B analog voltage
const int pwmPinA = 11; // PWM A output pin
const int pwmPinB = 12; // PWM B output pin
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 100; // Update duty cycle value every 100ms
void setup() {
byte numDigitsA = 2; // number of digits display A
byte numDigitsB = 2; // number of digits display B
byte digitPinsA[] = {9, 10}; // Pins for Digits 1, 2,
byte digitPinsB[] = {39, 40}; // Pins for Digits 3, 4.
byte segmentPinsA[] = {2, 3, 4, 5, 6, 7, 8}; // Pins for segments A, B, C, D, E, F, G,
byte segmentPinsB[] = {32, 33, 34, 35, 36, 37, 38}; // Pins for segments A, B, C, D, E, F, G,
pinMode(pwmPinA, OUTPUT);
pinMode(pwmPinB, OUTPUT);
bool resistorsOnSegments = true; // 'true' if resistors are on segment pins
byte hardwareConfig = COMMON_ANODE; // CHANGE THIS to COMMON_CATHODE if your display is
// common cathode
bool updateWithDelays = false; // Highly recommended 'false' for clean multiplexing
bool leadingZeros = false; // Set to true if you want '0050%' instead of ' 50%'
bool disableDecPoint = true; // We don't need decimal points for simple percentages
// Initialise the 7-segment display A
sevsegA.begin(hardwareConfig, numDigitsA, digitPinsA, segmentPinsA, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevsegA.setBrightness(90);
// Initialise the 7-segment display B
sevsegB.begin(hardwareConfig, numDigitsB, digitPinsB, segmentPinsB, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevsegB.setBrightness(90);
}
void loop() {
unsigned long currentTime = millis();
// Non-blocking timer to sample the duty cycle
if (currentTime - lastUpdateTime >= updateInterval) {
lastUpdateTime = currentTime;
int analogValA = analogRead(inputPinA);
int dutyCycleA = map(analogValA, 0, 1023, 26, 253);
int analogValB = analogRead(inputPinB);
int dutyCycleB = map(analogValB, 0, 1023, 26, 253);
analogWrite(pwmPinA, dutyCycleA);
int dutyPercentA = (dutyCycleA * 100 + 128) / 255; // calculate duty cycle A percentage
analogWrite(pwmPinB, dutyCycleB);
int dutyPercentB = (dutyCycleB * 100 + 128) / 255; // calculate duty cycle B percentage
sevsegA.setNumber(dutyPercentA);
sevsegB.setNumber(dutyPercentB);
}
// CRITICAL: This must run continuously in the loop to keep the display illuminated!
sevsegA.refreshDisplay();
sevsegB.refreshDisplay();
}
[/code]