I am using the ATtiny10 MCU with Arduino 1.8.9. I have managed to get everything working except for the timing aspect of my application, which involves a delay timer. I can successfully read and send analog count values through UART, but I'm facing issues with the timing calculations. Since I'm using PB0 for UART, I can't get an accurate count for PB0. To obtain this count, I need to run separate tests and then write the code. While DipReading
and Dip_State
are functioning correctly, I'm having trouble with PotReading
and the time delay calculations.
I need help in passing right values.
Assume delayValue =1000msec . This max value been set.
I have the pot which read this value and devide into range. the variable used to read the Pot is PotReading reading.
when pot position is 1 i,e means after 100Us Relay must be on.
Max delayValue =1000 when pot position is min it should give 10 value .
TimeDelay = (delayValue * PotReading)/10;
I need formula which we can handle this..
#include <avr/delay.h>
#include "debugSerial.h"
debugSerial dSerial;
// ADC channel definitions
#define POT_CHANNEL 0
#define DIP_CHANNEL 1
#define DIP4_CHANNEL 3
// Digital pin definitions
#define RELAY_PIN 2
#define DELAY_0_1MS 1 // 0.1 milliseconds
#define DELAY_1MS 1 // 1 millisecond
#define DELAY_100MS 100
#define DELAY_0_1SEC 100 // 100 milliseconds
#define DELAY_10MS 10
// Delay constants (in milliseconds)
#define DELAY_1SEC 1000
#define DELAY_10SEC 10000
#define DELAY_30SEC 30000
#define DELAY_1MIN 60000
#define DELAY_3SEC 3000
#define DELAY_3MIN 180000
#define DELAY_10MIN 600000
#define DELAY_30MIN 1800000
#define DELAY_1HR 3600000
#define DELAY_3HR 10800000
#define DELAY_10HR 36000000 // Added delay for 10 hours
#define DELAY_30HR 108000000
unsigned char PowerState = 1;
unsigned int delayValue = 0;
unsigned int PotReading = 0;
unsigned char Dip_State = 0;
int TimeDelay = 0;
void setup() {
pinMode(POT_CHANNEL, INPUT);
pinMode(DIP_CHANNEL, INPUT);
pinMode(DIP4_CHANNEL, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
}
void loop() {
// digitalWrite(RELAY_PIN, LOW);
PotReading = (analogRead(POT_CHANNEL) / 40) + 1;
Dip_State = (analogRead(DIP4_CHANNEL) >= 700) ? 1 : 0;
int DipReading = analogRead(DIP_CHANNEL);
if (DipReading < 20) {
delayValue = (Dip_State == 0) ? DELAY_1SEC : DELAY_1MIN;
} else if (DipReading < 120) {
delayValue = (Dip_State == 0) ? DELAY_10SEC : DELAY_10MIN;
} else if (DipReading < 180) {
delayValue = (Dip_State == 0) ? DELAY_3SEC : DELAY_3MIN;
} else if (DipReading < 130) {
delayValue = (Dip_State == 0) ? DELAY_30SEC : DELAY_30MIN;
} else if (DipReading < 190) {
delayValue = (Dip_State == 0) ? DELAY_1HR : DELAY_1HR;
} else if (DipReading < 40) {
delayValue = (Dip_State == 0) ? DELAY_3HR : DELAY_3HR;
} else if (DipReading < 120) {
delayValue = (Dip_State == 0) ? DELAY_10HR : DELAY_10HR;
} else if (DipReading < 230) {
delayValue = (Dip_State == 0) ? DELAY_30HR : DELAY_30HR;
}
PowerState = 0;
TimeDelay = (delayValue * PotReading)/10;
if (TimeDelay >= 100 && TimeDelay < 3000)
{
delayMicroseconds(TimeDelay);
} else
{
delay(TimeDelay);
}
// Activate the relay
digitalWrite(RELAY_PIN, HIGH);
}