hi the purpose of this code is to turn on Pin 12 at 400 on the potentiometer and turn off at 800 and not turn on again until it drops down to 400 however i wold like a reading of <400 for 8sec before (digitalWrite (12,1) ;) however with the delay function if it is <400 and gets to >800 before 8 secs it does not (digitalWrite(12,0)) imidiatly. Any alternative ideas would be greatly appreciated.
int var = 0;
void setup() {
// put your setup code here, to run once:
pinMode(12, OUTPUT);
pinMode(A0, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
var = analogRead(A0);
if (var < 400)
digitalWrite(12, 1);
if (var>800)
digitalWrite(12,0);
}`Use code tags to format code for the forum`
sorry kinda new to this fourm here is my question
hi the purpose of this code is to turn on Pin 12 at 400 on the potentiometer and turn off at 800 and not turn on again until it drops down to 400 however i wold like a reading of <400 for 8sec before (digitalWrite (12,1) ;) however with the delay function if it is <400 and gets to >800 before 8 secs it does not (digitalWrite(12,0)) imidiatly. Any alternative ideas would be greatly appreciated.
hi the purpose of this code is to turn on Pin 12
at 400 on the potentiometer and turn off at 800
and not turn on again until it drops down to 400
however i wold like a reading of <400 for 8sec before
(digitalWrite (12,1)however with the delay function
if it is <400 and gets to >800 before 8 secs it does
not (digitalWrite(12,0)) imidiatly. Any alternative
ideas would be greatly appreciated.
Non-blocking timing tutorials:
Blink without delay().
Blink without delay detailed explanation
Beginner's guide to millis().
Several things at a time.
I have difficulties trying to understand your requirements.
Maybe this will help.
You can digitalWrite the pin HIGH (or LOW) in setup(),
and have your 8 second delay right after that (both in setup).
The code continues then in loop, where you can read the analogue pin without delay().
Leo..
void setup() {
pinMode(12, OUTPUT);
digitalWrite (12, HIGH);
delay(8000);
}
Hello kylan
Consider and check this small sketch:
#define ProjectName "Led controlled with potentiometer and delay"
//variables
constexpr uint8_t LedPin {12};
constexpr uint8_t PotPin {A0};
constexpr uint32_t DelayTime {8000};
constexpr uint16_t SwitchThreshold {400};
void setup()
{
Serial.begin(115200);
Serial.println(ProjectName);
pinMode(LedPin, OUTPUT);
}
void loop()
{
analogRead(PotPin) < SwitchThreshold ? digitalWrite(LedPin, LOW) : digitalWrite(LedPin, HIGH);
delay(DelayTime);
}
Have a nice day and enjoy coding in C++.
for some context the purpose of this code is to controll an air compressor for air ride on a pickup.
There will be an angle sensor on the suspention linkage and when it drops below threshold(400) for 8 sec i need it to turn on the compressor untill threshold(800) and if i put the delay in the setup it only runs when powered up. i think something with millis is what im looking for but having dificulties figuring it out
I would make a state machine to do this.
Did you look at any of the tutorials that I linked in post #3?
thank you i will check into that here is my current code but it only delays on start up and i need it to delay before turning on evey time
< `const int LevelSensor = A0;
const int Compressor = 12;
int var = 0;
unsigned long previousMillis = 0;
unsigned long interval = 8000;
void setup() {
// put your setup code here, to run once:
pinMode(LevelSensor, INPUT);
pinMode(Compressor, OUTPUT);
}
void loop() {
var = analogRead(A0);
unsigned long currentMillis = millis();
if ((var < 400) && currentMillis - previousMillis > interval){
digitalWrite(Compressor, 1);
previousMillis = currentMillis;}
if (var > 800)
digitalWrite(Compressor, 0);
}`>/
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.