Pages: [1] 2   Go Down
Author Topic: reading push button at any time HELP  (Read 1398 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Purpose of code:
Initially I will be monitoring one day with 3 pills. I have all subsystems working individually. SMS system works fine and LDR sensor effectively recognizes the presence of a pill, and will only send a SMS text at certain times as per the time from the RTC.
However my issue is i also want to use a normally open MOM pushbutton switch to decrement the preset value of pills in the box. eg. after the first pill is taken button is pushed and pillCount = 3 - 1 = 2 pills left to take. The code i used to read the PushButton works as a standalone however when implemented in the main code it never reads when the button is pushed, it doesnt even output the serial debug lines in the button formula/code.  

Additionally, as you can see in my code, alarm(); and ledFlash(); are additional warnings how do i make it so these dont turn off until the if statement condition is no longer met.

* FINAL.pde (11.21 KB - downloaded 15 times.)
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50929
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Initially I will be monitoring one day with 3 pills.
The words all make sense. The order of them does not.

I don't see, in setup() that you are activating the internal pullup resistor for the switch. You must, then, have an external pullup resistor. Do you? How IS the switch wired?

Code:
void loop() {
  Serial.println(" ");
  Serial.println(" ");
  Serial.println("pillcount is ");
  Serial.print(pillCount);
  Serial.println(" ");
  Serial.println(" ");
  //getDateDs1307();
  Serial.println(" ");
  Serial.println(" ");
The loop function executes millions of times per second. Do you really need all this data on EVERY pass through loop?

Code:
  if(lastButtonState != buttonState){
     if(buttonState == LOW){
 
This is not C# where variables are bound to events. buttonState doesn't have a (meaningful) value if you don't call digitalRead() to assign it one.

Code:
  if (9<=hour<=12)
The test for hour greater than or equal to 9 is performed, and the result (true or false) is compared to 12. Is that really a meaningful comparison?

It should be:
Code:
if( hour >= 9 && hour <= 12)

You have a number of other if tests like this that are also wrong.

Good thing you didn't write a bunch of code without testing any of it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Each day has three pills that are monitored in the one pill box, the val of analogRead(LDR); does change when additional pills are added but is not a significant enough between 2 and 3 to accurately determine how many pills have been taken in a given day.

Hence, the push button switch. I have put in a lot of print lines to debug my issues, to no avail.

I have adjusted the if statements, thanks.

the thing is my PB standalone program works.

buttonState = digitalRead(buttonPin); i missed this when transferring from the other standalone code. will test the system again in the AM and report back.
note: i have an external pull up resistor 10k, have attached a sketch of the switch connection.

There has been significant testing, and all parts of the code work as individual sub-systems, i am out of my depth so thanks for the help.


* MOM switch sketch.jpg (11.23 KB, 373x256 - viewed 23 times.)
Logged

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Then perhaps you need to check the implementation, and the debug statements are always useful, check at which point it still works, then see why the code stops there
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 
Code:
const int LDR = 0;       // select the input pin for the LDR
const int ledWarning = 13;   // select the pin for the LED
const int buttonPin = 4; // digital pin the PB is attached to
const int LED = 12;    // LED to flash when pill present Monday
const int speakerPin = 11; //select output pin for alarm
int sensor = 0;       // variable to store the value coming from the sensor
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int pillCount = 3;
int sms1ASent = 0;
int sms1BSent = 0;
int sms1CSent = 0;
int sms1DSent = 0;
int sms2ASent = 0;
int sms2BSent = 0;
int sms2CSent = 0;
int sms2DSent = 0;
int sms3ASent = 0;
int sms3BSent = 0;
int sms3CSent = 0;
int sms3DSent = 0;

#include "Wire.h"
#include <NewSoftSerial.h>
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address
#define MAXLINE 10
#define Mode12 1
#define Mode24 0
#define AM 0
#define PM 1

NewSoftSerial gsm(2,3); //define gsm as serial pin out
 
// Global Variables
 
 byte second, minute, hour, dayOfWeek, dayOfMonth, month, year, ampm;
 
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}
 
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

static char *dayname[] =
{ "Sun",
"Mon",   
"Tue", 
"Wed", 
"Thu",
"Fri",
"Sat"
};
 
void setup() {
  Wire.begin();
  pinMode(LDR, INPUT);       // declare the LDR as an INPUT
  pinMode(buttonPin, INPUT); //declare Pushbutton as an INPUT
  pinMode(ledWarning, OUTPUT); //declare ledWarning as an OUTPUT
  pinMode(LED, OUTPUT);  // declare LED as an OUTPUT
  pinMode(speakerPin, OUTPUT); //declare Alarm as an OUTPUT
  Serial.begin(9600);      // initialising serial at baud rate of 9600
  gsm.begin(9600);         // initialising serial gsm at baud rate of 9600
}

void loop() {
  Serial.println(" ");
  Serial.println("pillcount is ");
  Serial.print(pillCount);
  Serial.println(" ");
 
   
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year, ampm;

  getClock(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year, &ampm);
 
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.println(dayname[dayOfWeek-1]);
 
  delay(1000);
  buttonState = digitalRead(buttonPin);
  delay(1000);
  if(lastButtonState != buttonState){
     if(buttonState == LOW){
     delay(100);
     Serial.println("Button is OFF");
     //print button is off if button is not pushed and a LOW is returned

     }
     else{
        delay(500);
         Serial.println("Button was turned ON");
         buttonPushCounter++;
         pillCount--;
          delay(1000);
         Serial.print("Times button was pushed ");
         Serial.print(buttonPushCounter);
         Serial.println(" times");

       }
     }
   
   lastButtonState = buttonState;
   if(buttonPushCounter%5 == 0 && buttonPushCounter != 0){
     pillCount = pillCount - 1;
   }
 
 
 
  if( hour >= 9 && hour <= 12)
  {
    digitalWrite(LED, HIGH);   // Turn LED on
    delay(1000);
    sensor = analogRead(LDR);   // read the value from the sensor
    Serial.print(sensor,DEC);
      if ((sensor>=215 && sensor <= 900)&&(pillCount==3))
      {
        digitalWrite(LED,LOW);
        delay(100);
        alarm();
        delay(100);
        sms3();
        delay(100);
        digitalWrite(ledWarning, HIGH);
        delay(100);
      }
      if(sensor>900)
      {
        digitalWrite(LED,LOW);
        error();
        delay(100);
      }
  }
  if(hour >=  13 && hour <= 16)
  {
    digitalWrite(LED, HIGH);   // Turn LED on
    delay(1000);
    sensor = analogRead(LDR);   // read the value from the sensor
    Serial.print(sensor,DEC);
      if ((sensor>=215 && sensor <= 900)&&(pillCount==2))
      {
        digitalWrite(LED,LOW);
        delay(100);
        alarm();
        delay(100);
        sms2();
        delay(100);
        digitalWrite(ledWarning, HIGH);
        delay(100);
      }
      if(sensor>900)
      {
        digitalWrite(LED,LOW);
        error();
        delay(100);
      }
  }
  if(hour>=17 && hour<=21)
  {
    digitalWrite(LED, HIGH);   // Turn LED on
    delay(1000);
    sensor = analogRead(LDR);   // read the value from the sensor
    Serial.print(sensor,DEC);
   
    if ((sensor>=215 && sensor <= 900)&&(pillCount==1))
      {
        digitalWrite(LED,LOW);
        Serial.print("debug3");
        delay(100);
        sms1();
        delay(100);
        alarm();
        delay(100);
        digitalWrite(ledWarning, HIGH);
        delay(100);
      }
      if(sensor>900)
      {
        digitalWrite(LED,LOW);
        error();
        delay(100);
      }
  }
       
     
  delay(1000);                  // stop the program for some time
 }
   
 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void getClock(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year,
          byte *ampm)
{
  byte work;
  byte mode;          //12 or 24 hour
  byte ap_ind;       //am or pm indicator
  
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second     = bcdToDec(Wire.receive() & 0x7f);      //mask CH bit (bit 7)
  *minute     = bcdToDec(Wire.receive());

//  *hour = bcdToDec(Wire.receive());
  work = Wire.receive();                                                // get hour byte
  mode =  bitRead(work, 6);                                         // if bit  6  set,  running 12 hour mode
  if (mode == Mode12) {
    ap_ind = bitRead(work, 5);                                      // if bit 5 set,  time is PM
    *hour = bcdToDec(work & 0x1f);                             // mask bits 5 thru 7 for 12 hour clock
    if (ap_ind == PM) {
      *ampm = 'P';
    }else{
      *ampm = 'A';
    }  
  }else{  
    *hour = bcdToDec(work & 0x3f);                             // mask bits 6 and 7 for 24 hour clock
    *ampm = ' ';  
  }  

  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}
  
void playTone(int period, int duration)
{
  int pulse = period/2;
  for (long i=0; i<duration*1000L; i+= period)
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(pulse);
    digitalWrite(speakerPin,LOW);
    delayMicroseconds(pulse);
  }
}

void alarm()
{
  for(int period=15289; period >= 477; period=period/2)
  {
    playTone(period,200);
  }
}



This is my latest code, the push button decrementer works but only if pushed at certain times, if i had a typical on/off switch that stays in a position until switched again then i think i could figure it out,
anyone with any idea how to make it work with the MOM switch?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PART 3/3
Code:
void sms3(){
 getClock(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year, &ampm);
 if(hour==9 && minute==30)
  {
    for(int a=0; a<2; a++)
    {
      if(sms3ASent == 1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 3 pills to be taken warning one");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("Please take your morning medication");
        gsm.print(" ");
        gsm.print("This is your first reminder");
        gsm.println(26,BYTE);
      }
    }
  }
  if(hour==10 && minute==30)
  {
    for(int b=0; b<2; b++)
    {
      if(sms3BSent==1)
      {
        break;
      }
      else
      {    
        Serial.print("SEND SMS 3 pills to be taken warning two");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("Come on, you will feel much better if you just take your morning pill!");
        gsm.print(" ");
        gsm.print("This is your second reminder");
        gsm.println(26,BYTE);
      }
    }
  }
  if(hour==11 && minute==30)
  {
    for(int c=0; c<2; c++)
    {
      if(sms3CSent==1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 3 pills to be taken warning three");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("OK, if you dont take your morning pill in the next 30 mins im informing your family! ");
        gsm.print(" ");
        gsm.print("This is your third reminder");
        gsm.println(26,BYTE);
        sms3CSent = sms3CSent + 1;
      }
    }
  }
  if(hour==12)
  {
    for(int d=0; d<2; d++)
    {
      if(sms3DSent == 1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 3 pills to be taken family warning");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("Hi, your loved one hasn't taken their medication please see if they are ok");
        gsm.print(" ");
        gsm.println(26,BYTE);
        sms3DSent = sms3DSent + 1;
      }
    }
  }    
}

void sms2(){
  getClock(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year, &ampm);
  if(hour==13 && minute==30)
  {
    for(int a=0; a<2; a++)
    {
      if(sms2ASent==1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 2 pills to be taken warning one");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("Please take your afternoon medication");
        gsm.print(" ");
        gsm.print("This is your first reminder");
        gsm.println(26,BYTE);
        sms2ASent = sms2ASent + 1;
      }
    }
  }
    
  
  if(hour==14 && minute==30)
  {
    for(int b=0; b<2; b++)
    {
      if(sms2BSent==1)
      {
        break;
      }
      else
      {
        
          Serial.print("SEND SMS 2 pills to be taken warning two");
          gsm.println("AT+CMGF=1");
          delay(2000);
          gsm.println("AT+CMGS=+61401866134");
          delay(500);
          gsm.print("Come on, you will feel much better if you just take your afternoon pill!");
          gsm.print(" ");
          gsm.print("This is your second reminder");
          gsm.println(26,BYTE);
          sms2BSent = sms2BSent + 1;
      }
    }
  }
  if(hour==15 && minute==30)
  {
    for(int c=0; c<2; c++)
    {
      if(sms2CSent == 1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 2 pills to be taken warning three");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("OK, if you dont take your afternoon pill in the next 30 mins im informing your family! ");
        gsm.print(" ");
        gsm.print("This is your third reminder");
        gsm.println(26,BYTE);
        sms2CSent = sms2CSent + 1;
      }
    }
  }
  if(hour==16)
  {
    for(int d=0; d<2; d++)
    {
      if(sms2DSent == 1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 2 pills to be taken family warning");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("Hi, your loved one hasn't taken their medication please see if they are ok");
        gsm.print(" ");
        gsm.println(26,BYTE);
        sms2DSent = sms2DSent + 1;
      }
    }
  }
}
void sms1(){
  getClock(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year, &ampm);
  Serial.print(hour,DEC);
  if(hour==17 && minute==30)
  {
    for(int a=0; a<2; a++)
    {
      if(sms1ASent==1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 1 pill to be taken warning one");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("Please take your evening medication");
        gsm.print(" ");
        gsm.print("This is your first reminder");
        gsm.println(26,BYTE);
        sms1ASent = sms1ASent + 1;
      }
    }
   }
  if(hour==18 && minute==30)
  {
    for(int b=0; b<2; b++)
    {
      if(sms1BSent==1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 1 pill to be taken warning two");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("Come on, you will feel much better if you just take your evening pill!");
        gsm.print(" ");
        gsm.print("This is your second reminder");
        gsm.println(26,BYTE);
        sms1BSent = sms1BSent + 1;
      }
    }
  }
  if(hour==19 & minute==30)
  {
    for(int c=0; c<2; c++)
    {
      if(sms1CSent==1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 1 pill to be taken warning three");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("OK, if you dont take your evening pill in the next 30 mins im informing your family! ");
        gsm.print(" ");
        gsm.print("This is your third reminder");
        gsm.println(26,BYTE);
        sms1CSent = sms1CSent + 1;
      }
    }
  }
  if(hour==20)
  {
    for(int d=0; d<2; d++)
    {
      if(sms1DSent==1)
      {
        break;
      }
      else
      {
        Serial.print("SEND SMS 1 pill to be taken family warning");
        gsm.println("AT+CMGF=1");
        delay(2000);
        gsm.println("AT+CMGS=+61401866134");
        delay(500);
        gsm.print("Hi, your loved one hasn't taken their medication please see if they are ok");
        gsm.print(" ");
        gsm.println(26,BYTE);
        sms1DSent = sms1DSent + 1;
      }
    }
  }
}
        
void error(){
  Serial.print("ERROR");
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50929
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This is my latest code, the push button decrementer works but only if pushed at certain times, if i had a typical on/off switch that stays in a position until switched again then i think i could figure it out,
anyone with any idea how to make it work with the MOM switch?
Some test code to get this functionality working would NOT take 3 posts to get it all in. Start a new sketch to just read the switch state, and decrement a counter when the switch transitions from HIGH to LOW or LOW to HIGH, depending on how the switch is wired and whether you want the count to decrement on pressed or on released.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

paul, already done... see below.
however, this isnt my issue. the decrement works, however it is particularly sensitive to when the PB is pressed.

Code:
// this constant won't change:
const int  buttonPin = 4;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int pillCount = 3;
void setup() {
 // initialize the button pin as a input:
 pinMode(buttonPin, INPUT);
 // initialize serial communication:
 Serial.begin(9600);
}

void loop() {
 // read the pushbutton input pin:
 buttonState = digitalRead(buttonPin);

 if(lastButtonState != buttonState){
   if(buttonState == LOW){
     delay(100);
     Serial.println("Button is OFF");
     //print button is off if button is not pushed and a LOW is returned

   }
   else{
      delay(500);
     Serial.println("Button was turned ON");
     buttonPushCounter++;
     pillCount--;
      delay(1000);
     Serial.print("Times button was pushed ");
     Serial.print(buttonPushCounter);
     Serial.println(" times");
     Serial.println("pillCount is:");
     Serial.print(pillCount);

   }
 }
 lastButtonState = buttonState;
 if(buttonPushCounter%5 == 0 && buttonPushCounter != 0){
   digitalWrite(ledPin, HIGH);
 }else{
   digitalWrite(ledPin, LOW);
 }
}

Also included serial output to show system working


* SerialWindow.jpg (39.18 KB, 408x360 - viewed 23 times.)
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Those delays are your problem - while you're in delay, the arduino isn't doing anything, so the button won't be read. Get rid of them. Just use a single delay(10) after you read the button to debounce it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

which delays? the button ones? or are you referring in my entire code,
SMS for example needs some delay to allow the MODEM to respond before sending next line. 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what i need is a way to pull digital pin 4 HIGH and leave it HIGH until it is read.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50929
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
what i need is a way to pull digital pin 4 HIGH and leave it HIGH until it is read.
You need external hardware for this. Look at flip-flops.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

removing some delays did help, it isnt perfect but 'works'

Logged

New Jersey
Online Online
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

which delays? the button ones? or are you referring in my entire code,
SMS for example needs some delay to allow the MODEM to respond before sending next line. 

I was responding to the code in post #4, so just the three delays there, but I see in reply three that there are many there too. Get rid of as many as you can, then take the standard advice and look at the blink without delay example.
Logged

Pages: [1] 2   Go Up
Jump to: