I have a fairly straight forward question. I want to write a digital pin for about a second or less when a temperature goes below a certain level. This digital pin will send voltage to a 555 timer and activate a Piezo buzzer. I have no clue of how to go about programming this though. All of the hardware is wired and it works. I'm just having trouble with the programming since this is not my background. Here's a snippet of code so far:
int inPin = 30; // the number of the input pin
int outPin = 32; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void loop(){
if (ftemp1 < 82)
{
reading = digitalRead(outPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == LOW)
state = HIGH;
else
state = LOW;
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}
I suppose it would probably look something like that. The only problem is that the pin is always high and I can't get it to turn off after a certain amount of time. Thank you.
Well for staters you could implement your 'setup' routine where you might use 'pinMode' to set your pins IO direction as well as 'digitalWrite' to set the initial values.
Then there is the fact that 'millis()' returns and works with 'unsigned long' values.
An then there are better names for your IO pins than 'inPin' and 'outPin'.
And then by the way why are you "reading" 'outPin'? I thought it was an output! But then again if it is an OUTPUT then reading it will returns its last written state.
Your variables holding time values need to be unsigned long, not long.
I don't understand the purpose of the 'debounce' code.
You seem to be trying to combine the starting and stopping logic into a single expression, which makes it very complicated. If you deal with these two conditions separately, it gets much easier:
if(buzzerActive == true)
{
// buzzer is currently on
if(millis() - buzzerStartTime > BuzzerDuration)
{
stopBuzzer();
buzzerActive = false;
}
}
else
{
// buzzer is currently off
if(temperature < threshold)
{
buzzerStartTime = millis();
startBuzzer();
buzzerActive = true;
}
}
What if I just wanted to make a single pin high for a second or half a second?
Then just do it. Turn it on when you want it on. When enough time has passed, using either delay() to waste time, or millis() to measure time, turn it off. What is the problem? This is basic, beginner stuff.
lloyddean:
I thought it was an output! But then again if it is an OUTPUT then reading it will returns its last written state.
Slightly pedantic, but no, reading an output pin will actually read the physical state of the pin, not its
last written state (however unless you have a short-circuit on that pin these will be the same). The
hardware that reads from a pin is blissfully unaware of whether its currently an output or not.
Also the read state is that from the output of a synchronizer circuit so it will be about 1 clock cycle delayed
(not that this matters for digitalRead() which takes many clock cycles to execute).