Hello,
I'm a newbie in Arduino. I am currently trying to build a smart bowl for a design project and I am using an Arduino Genuino UNO R3 board.
This bowl should function as a reminder to the user to pick some healthy food from it every 24 hours to keep a good lifestyle.
The circuit is composed as follows:
- 1 PIR sensor inside the bowl (PIRA)
- 1 PIR sensor outside the bowl (PIRB)
- 2 LEDs (1 green and 1 red)
- SD card module with 2 songs to play (1.wav and 2.wav)
- speaker
The desired function would be: when the bowl gets switched on (i.e. when the Arduino starts) a timer starts counting in seconds (for a short demonstration, let's suppose 1h=1sec).
If the user does not pick anything from the bowl (i.e. if the PIRA == LOW) for more than 24h, the red led turns on. Also, if the user approaches the bowl (i.e. if the PIRB == HIGH) without picking anything from it (i.e. if the PIRA == LOW) after those 24h, the speaker plays song 2.wav to attract their attention.
If the user picks something form the bowl (i.e. if the PIRA == HIGH), the green led turns on and stays on for 24h, the speaker plays song 1.wav, and the timer starts again from zero.
I managed to make the code work only for the relation between the PIR sensors and the LEDs, as well as to make the speaker play the songs.
I have been searching in the forums, online, from the references but I still cannot manage to include the time factor in the code. I have tried copying some pieces of examples that involved time measuring but the PIRs stop functioning properly and the speaker restarts the same song for three or more times (as if it gets stuck in the beginning of the song).
I cannot figure out how to measure time from the start of the program and how to verify every second the conditions imagined, as well as to restart the timer if one condition is verified.
This is the code that works but without any time measuring:
#include <SD.h>
#include <PIR.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"
TMRpcm tmrpcm;
int ledPinA = 6; // choose the pin for the LED A
int inputPinA = 2; // choose the input pin (for PIR sensor A)
int pirStateA = LOW; // we start, assuming no motion detected by A
int valA = 0; // variable for reading the pin status for A
int ledPinB = 7; // choose the pin for the LED B
int inputPinB = 3; // choose the input pin (for PIR sensor B)
int pirStateB = LOW; // we start, assuming no motion detected by B
int valB = 0; // variable for reading the pin status for B
void setup() {
// put your setup code here, to run once:
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin))
{
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(3);
pinMode(ledPinA, OUTPUT); // declare LED as output
pinMode(inputPinA, INPUT); // declare sensor as input
pinMode(ledPinB, OUTPUT); // declare LED as output
pinMode(inputPinB, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop() {
{
// put your main code here, to run repeatedly:
// read the state of the sensor/digital input
pirStateA = digitalRead(2);
// check if sensor pin is HIGH. if it is, set the
// LED on.
if (pirStateA == HIGH) {
digitalWrite(6, HIGH);
Serial.println("Sensor activated!");
tmrpcm.setVolume(3);
tmrpcm.play("1.wav");
} else {
digitalWrite(6, LOW);
}
delay(1); // Delay a little bit to improve simulation performance
}
{
// read the state of the sensor/digital input
pirStateB = digitalRead(3);
if (pirStateB == HIGH) {
digitalWrite(7, HIGH);
Serial.println("Sensor activated!");
tmrpcm.play("2.wav");
} else {
digitalWrite(7, LOW);
}
delay(1); // Delay a little bit to improve simulation performance
}
}
Attached you can find a photo of the circuit.
Thank you in advance to anyone willing to help.
