I'm looking for sample Arduino-based code that reads an analog value from a potentiometer and blinks an LED based on the analog input. The blink intervals should be as follows:
Less than 100: 1000 ms
100 to 250: 5000 ms
250 to 500: 10 seconds
500 to 750: 20 seconds
750 to 1000: 50 seconds
Greater than 1000: 1 minute
1 Like
Look at the blink example; it uses a constant 1000ms in the delay() calls but nothing stops you from using a variable.
void loop() {
uint32_t delayTime = 1000;
digitalWrite(LED_BUILTIN, HIGH);
delay(delayTime);
digitalWrite(LED_BUILTIN, LOW);
delay(delayTime);
}
For you to add the analogRead() so you can change the delayTime variable.
Note that if you have a long delayTime, the above code will become unresponsive. You should therefore look at the blink-without-delay example.
Note:
No experience with attiny10 so I have no idea if it supports e.g. analogRead()
Differentiating between the upper level of one to the lower level of the other will be almost impossible.
gcjr
July 23, 2024, 10:18am
5
using delay would prevent reading the pot and changing the timer period in less than the delay period
this code uses shorter periods to allow faster testing
const byte PinLed = LED_BUILTIN;
const byte PinPot = A0;
unsigned long msecPeriod;
unsigned long msecLst;
void loop()
{
unsigned long msec = millis ();
if (msec - msecLst >= msecPeriod) {
msecLst += msecPeriod;
digitalWrite (PinLed, ! digitalRead (PinLed));
}
int pot = analogRead (PinPot);
if (100 > pot)
msecPeriod = 100;
else if (250 > pot)
msecPeriod = 500;
else if (500 > pot)
msecPeriod = 1000;
else if (750 > pot)
msecPeriod = 2000;
else if (1000 > pot)
msecPeriod = 5000;
else
msecPeriod = 6000;
Serial.print (pot);
Serial.print (" ");
Serial.println (msecPeriod);
}
void setup() {
Serial.begin (9600);
pinMode (PinLed, OUTPUT);
}
I followed instruction mentioned in above website . i am currently not able to flash any example code to attiny10. Can you tell me right setup and right board manager to flash code. i am using arduino uno as TPI programmer. and also i have USBasp for programming both are having same issue.
it showing it could detect the attiny10 MCU.
but not able to flash any sample code or bare minimum code.
I have microchip studio installed. there also i could able to write and compile code. But not able to flash the Hex file. Can someone suggest working circuit.
if circuit i build is correct . let me know fix i need to follow.
i want simple ledblink working here
@LarryD
below code written in microchip studio . it willcompile without error.Need to flash on attiny10 module. But not able to flash Can someone help
#define F_CPU 1000000UL // Define CPU frequency as 1MHz
#include <avr/io.h>
#include <util/delay.h>
// ADC channel definitions
#define POT_CHANNEL 0 // PB0
void InitADC(uint8_t channel) {
ADMUX = channel;
if (channel == 0) {
DIDR0 = (1 << ADC0D);
} else if (channel == 1) {
DIDR0 = (1 << ADC1D);
}
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0);
}
uint16_t ReadADC(uint8_t channel) {
ADMUX = (ADMUX & 0xF0) | (channel & 0x0F);
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
uint16_t adcResult = ADCL;
return adcResult;
}
void InitIO() {
DIDR0 = (1 << ADC0D); // Disable digital input buffer for PB0
DDRB |= (1 << PB2); // Set PB2 as output (Relay)
}
void RelayOn() {
PORTB |= (1 << PB2); // Set PB2 (Relay on)
}
void RelayOff() {
PORTB &= ~(1 << PB2); // Clear PB2 (Relay off)
}
void DelayTime(uint32_t time_ms) {
while (time_ms >= 1000) {
_delay_ms(1000); // Delay for 1000 milliseconds
time_ms -= 1000;
}
while (time_ms > 0) {
_delay_ms(1); // Delay for remaining milliseconds
time_ms--;
}
}
int main() {
InitIO();
InitADC(POT_CHANNEL);
uint8_t relayOnFlag = 0;
while (1) {
if (!relayOnFlag) {
uint16_t potValue = ReadADC(POT_CHANNEL);
uint32_t timeValue;
if (potValue < 102) {
timeValue = 5000; // 5 seconds
} else if (potValue < 204) {
timeValue = 10000; // 10 seconds
} else if (potValue < 306) {
timeValue = 15000; // 15 seconds
} else if (potValue < 408) {
timeValue = 20000; // 20 seconds
} else if (potValue < 510) {
timeValue = 25000; // 25 seconds
} else if (potValue < 612) {
timeValue = 30000; // 30 seconds
} else if (potValue < 714) {
timeValue = 35000; // 35 seconds
} else if (potValue < 816) {
timeValue = 40000; // 40 seconds
} else if (potValue < 918) {
timeValue = 45000; // 45 seconds
} else {
timeValue = 60000; // 60 seconds
}
DelayTime(timeValue); // Wait for the specified delay
RelayOn(); // Turn the relay on after the delay
relayOnFlag = 1; // Set the flag to indicate the relay is on
}
// Keep the relay on indefinitely
}
return 0;
}
b707
July 24, 2024, 12:03am
8
@AJITnayak and @Ajit_nayak
are you the same person?
@AJITnayak and @Ajit_nayak ,
Please read and respond to the PM I have sent to you.
Thank you.
rsmls
July 24, 2024, 7:15am
10
That's Serial monitor output.
If you want to program with e.g. USBASP, you must use the 'Upload using programmer' option.
Also select USBasp as the programmer in the Tools->Programmer setting.