Hi guys. I am working on a simple pill bottle. The concept is I will text the pill bottle 1 PILL and 1 led will turn on or 2 PILLS and 2 leds will turn on. Then once the pill container is opened, there are magnets under the container cap and it will turn off the led once it is opened. Then, once 1 PILL is taken the pill bottle will send an sms to me that 1 pill is taken or 2 pills if 2 pills is taken. I already have codes for this but I can't seem to combine them. Here are the codes
This is for the taking of pills
#include <SoftwareSerial.h>
#include <HX711.h>
SoftwareSerial gsm(9, 10);
HX711 scale(A1, A0);
long val = 0;
long quantity = 0;
float count = 0;
long newquantity = 0;
void setup() {
Serial.begin(9600);
gsm.begin(9600);
delay(2000);
gsm.println("AT+CMGF=1");
delay(1000);
gsm.println("AT+CNMI=2,2,0,0,0");
delay(1000);
}
void loop() {
count = count + 1;
val= scale.read ();
quantity = newquantity;
newquantity = ((((((val - 107400)/305890.0f) * 50))-35.9)/0.555);
delay (1000);
if (newquantity == quantity - 1)
{
gsm.println("AT+CMGS=\"09561345333\"\r");
delay(1000);
gsm.println("1 PILL WAS TAKEN");
delay(500);
gsm.println(char(26));
}
else if (newquantity == quantity + 1)
{
gsm.println("AT+CMGS=\"09561345333\"\r");
delay(1000);
gsm.println("1 PILL WAS ADDED");
delay(500);
gsm.println(char(26));
}
else if (newquantity == quantity - 2)
{
gsm.println("AT+CMGS=\"09561345333\"\r");
delay(1000);
gsm.println("2 PILLS WERE TAKEN");
delay(500);
gsm.println(char(26));
}
else if (newquantity == quantity + 2)
{
gsm.println("AT+CMGS=\"09561345333\"\r");
delay(1000);
gsm.println("2 PILLS WERE ADDED");
delay(500);
gsm.println(char(26));
}
else if(newquantity == quantity - 3)
{
gsm.println("AT+CMGS=\"09561345333\"\r");
delay(1000);
gsm.println("3 PILLS WERE TAKEN");
delay(500);
gsm.println(char(26));
}
else if (newquantity == quantity +3)
{
gsm.println("AT+CMGS=\"09561345333\"\r");
delay(1000);
gsm.println("3 PILLS WERE ADDED");
delay(500);
gsm.println(char(26));
}
delay (5000);
}
This is for texting the quantity and cap sensor code
#include <SoftwareSerial.h>
SoftwareSerial gsm(9, 10);
const int ReedSwitchPin = 12;
const int LEDPin1 = 5;
const int LEDPin2 = 6;
const int LEDPin3 = 7;
boolean PreviousReedSwitchState;
boolean LEDState1 = LOW;
boolean LEDState2 = LOW;
boolean LEDState3 = LOW;
void setup () {
pinMode (ReedSwitchPin, INPUT);
PreviousReedSwitchState = digitalRead(ReedSwitchPin);
pinMode (LEDPin, OUTPUT);
digitalWrite(LEDPin, LEDState);
Serial.begin(9600);
gsm.begin(9600);
delay(2000);
gsm.println("AT+CMGF=1");
delay(1000);
gsm.println("AT+CNMI=2,2,0,0,0");
delay(1000);
}
void loop () {
int reedSwitchState = digitalRead (ReedSwitchPin);
// Look for LEDon message from GSM
if (gsm.available()) {
String a = gsm.readString();
if (a.indexOf("1 PILL") >= 0)
{
GSMSendMessage("Take 1 PILL");
LEDState1 = HIGH;
digitalWrite (LEDPin1, LEDState1);
}
else if (a.indexOf("2 PILLS") >= 0)
{
GSMSendMessage("Take 2 PILLS");
LEDState1 = HIGH;
LEDState2 = HIGH;
digitalWrite (LEDPin1, LEDState1);
digitalWrite (LEDPin2, LEDState2);
}
else if (a.indexOf("3 PILLS") >= 0
{
GSMSendMessage("Take 3 PILLS");
LEDState1 = HIGH;
LEDState2 = HIGH;
LEDState3 = HIGH;
digitalWrite (LEDPin1, LEDState1);
digitalWrite (LEDPin2, LEDState2);
digitalWrite (LEDPin3, LEDState3);
}
}
// Look for reed switch input going HIGH
// Note: Debounce might be needed here
if (reedSwitchState != PreviousReedSwitchState) {
// State has changed.
PreviousReedSwitchState = reedSwitchState;
if (reedSwitchState == HIGH) { // Magnet moved away
LEDState1 = LOW;
LEDState2 = LOW;
LEDState3 = LOW;
digitalWrite (LEDPin1, LEDState1);
digitalWrite (LEDPin2, LEDState2);
digitalWrite (LEDPin3, LEDState3);
}
}
}
void GSMSendMessage(const char *message) {
gsm.println("AT+CMGS=\"09xxxx\"\r");
delay(1000);
gsm.println(message);
delay(500);
gsm.println(char(26));
}
Can you please help me combine them in a single code. I am just a newbie in coding so please teach me step by step on what to do. If you can see a flaws in my code please point it out. Thank you so much guys!