Can someone please help me discover why the digitalRead appears to test high sometimes without the button being pressed? Maybe some transient voltage appearing? Do I need to ground it somehow? Or do I need a more reliable way of testing the button?
Thanks.
Code: Select all
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define RelayPin 1 // raspberry pin 18
#define ButtonPin 0 // raspberry pin 17
int main(void)
{
if(wiringPiSetup() == -1) return 1; // exit immediately if setup fails
int min = 120; // number of minutes until time lapses
time_t starting = time(NULL); // time stamp (seconds) when program starts
time_t ending = starting + min*60; // time when program will end if button not pressed
pinMode (ButtonPin, OUTPUT); // set button to output temporarily
digitalWrite(ButtonPin, LOW); // make sure button pin is low
pinMode (ButtonPin, INPUT); // reset button to input
pinMode (RelayPin, OUTPUT); // set relay pin to output
digitalWrite (RelayPin, HIGH); // close relay (turn lights on) before loop
while (digitalRead(ButtonPin) == LOW && time(NULL) < ending)
{
delay (1); // loop on 1ms until button pressed (pin 17 goes high) or time lapses
}
digitalWrite (RelayPin, LOW); // open the relay (turn lights off) before exit
return 0;
}

