Maker Pro
Maker Pro

OP amp guidance for newbie

brian connolly

Jan 31, 2018
19
Joined
Jan 31, 2018
Messages
19
pic of code which will work once but then millis goes above 8000 and the led just blinks for a millisecond if I activate sensor.
 

Attachments

  • Capture.PNG
    Capture.PNG
    20.5 KB · Views: 68

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
There simple way is to have no delays, but to record in a variable the time the LED is turned on (store the current value of millis())., Then, instead of unconditionally turning off the LED if the result is less than 1, also check that the current millis () value is at least 8000 more than the stored value.

This relies on the fact that it is perfectly ok to turn the LED on even if it is already on, but it will also have the side effect of setting a new stored time which will extend the in time by another 8 seconds.
 

brian connolly

Jan 31, 2018
19
Joined
Jan 31, 2018
Messages
19
There simple way is to have no delays, but to record in a variable the time the LED is turned on (store the current value of millis())., Then, instead of unconditionally turning off the LED if the result is less than 1, also check that the current millis () value is at least 8000 more than the stored value.

This relies on the fact that it is perfectly ok to turn the LED on even if it is already on, but it will also have the side effect of setting a new stored time which will extend the in time by another 8 seconds.
Thanks Steve. I'll try figure out the code to that tonight. If you had any links to something similar that would be great
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
It would help a lot if you didn't post an image of your code.

try something like this:

Code:
long lastMilli = -8000;
int diff;

init() {
  digitalWrite(ledPin, LOW);
}

loop() {
    diff = AnalogRead(A1) - AnalogRead(A0);

    if (diff > 1) {
        digitalWrite(ledPin, HIGH);
        lastMilli = Millis();
    } else {
        if (lastMilli < Millis()) {
            digitalWrite(ledPin, LOW);
        }
    }
}
 

brian connolly

Jan 31, 2018
19
Joined
Jan 31, 2018
Messages
19
apologies and thanks again

with that code my led comes on every time I activate sensor, but goes straight back low again. I think its the ELSE statement doing this?
 

brian connolly

Jan 31, 2018
19
Joined
Jan 31, 2018
Messages
19
got it working at last steve!

I changed the ELSE statement to

if (millis()-lastMillis>8000)
{digitalWrite(ledPin,LOW);

:)
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Yeah, I knew I missed an 8000 there somewhere :)

Good job
 

brian connolly

Jan 31, 2018
19
Joined
Jan 31, 2018
Messages
19
Yeah, I knew I missed an 8000 there somewhere :)

Good job
Hi steve.

I have a quick question regarding coding with a 8X8 LED matrix. I have the code figured out for the info I want to display and this works fine. what I want to do is only display the info when I receive an input on a pin.

my matrix is connected as follows;
VCC to 5v
GND to GND
DIN to digital in 12
CS to digital in 10
CLK to digital in 11

I only want the info to display when I get a 5v input on pin 7.

any advice on how to code that.i would have thought it was straightforward but I cant seem to get it. cheers
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Yeah, read the logic value on pin 7 before you display. If it's high, go ahead an display. Otherwise, blank the display.
 
Top