/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:
Tutorial: Building a project to count the number of prayer rak'ahs using Arduino!
The purpose of this sketch is to count the number of rak'ahs of prayer
performed by the worshiper through an ultrasonic sensor that senses the
person's approach to the ground, counts the number of rak'ahs, and
displays them on a crystal screen.
Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line
Connection from the LCD:
• VCC pin → breadboard 5v line
• GND pin→ breadboard GND line
• SDA pin→ Arduino analog pin A4
• SCL pin → Arduino analog pin A5
Connections from the switch:
• The left terminal of the switch → Arduino pin 7
• The right terminal of the switch → Breadboard ground line
Connections from the ultrasonic sensor:
• ultrasonic sensor VCC pin → Breadboard 5V line
• ultrasonic sensor GND pin → Breadboard ground line
• ultrasonic sensor Trig pin → Arduino pin 3
• ultrasonic sensor Echo pin → Arduino pin 4
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 by Teach Me Something
int xx=0;
int yy=0;
byte arrow[8] = {B00000, B00000, B00000, B00000, B11100, B01111, B11100, B00000,};
const int trigPin = 3;
const int echoPin = 4;
const int button = 7;
// Variables
int prostrations = 0; // سجود
int rakats = 0; // ركعات
bool isProstrating = false; // حالة السجود
// Threshold distance in cm for detecting prostration
const float prostrationDistance = 30.0;
// the 8 arrays that form each segment of the custom numbers
byte bar1[8] =
{
B11100,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11100
};
byte bar2[8] =
{
B00111,
B01111,
B01111,
B01111,
B01111,
B01111,
B01111,
B00111
};
byte bar3[8] =
{
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111
};
byte bar4[8] =
{
B11110,
B11100,
B00000,
B00000,
B00000,
B00000,
B11000,
B11100
};
byte bar5[8] =
{
B01111,
B00111,
B00000,
B00000,
B00000,
B00000,
B00011,
B00111
};
byte bar6[8] =
{
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111
};
byte bar7[8] =
{
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00111,
B01111
};
byte bar8[8] =
{
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
void custom0(int col)
{
lcd.setCursor(col, 0);
lcd.write(2);
lcd.write(8);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(2);
lcd.write(6);
lcd.write(1);
}
void custom1(int col)
{
lcd.setCursor(col,0);
lcd.write(32);
lcd.write(32);
lcd.write(1);
lcd.setCursor(col,1);
lcd.write(32);
lcd.write(32);
lcd.write(1);
}
void custom2(int col)
{
lcd.setCursor(col,0);
lcd.write(5);
lcd.write(3);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(2);
lcd.write(6);
lcd.write(6);
}
void custom3(int col)
{
lcd.setCursor(col,0);
lcd.write(5);
lcd.write(3);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(7);
lcd.write(6);
lcd.write(1);
}
void custom4(int col)
{
lcd.setCursor(col,0);
lcd.write(2);
lcd.write(6);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(32);
lcd.write(32);
lcd.write(1);
}
void custom5(int col)
{
lcd.setCursor(col,0);
lcd.write(2);
lcd.write(3);
lcd.write(4);
lcd.setCursor(col, 1);
lcd.write(7);
lcd.write(6);
lcd.write(1);
}
void custom6(int col)
{
lcd.setCursor(col,0);
lcd.write(2);
lcd.write(3);
lcd.write(4);
lcd.setCursor(col, 1);
lcd.write(2);
lcd.write(6);
lcd.write(1);
}
void custom7(int col)
{
lcd.setCursor(col,0);
lcd.write(2);
lcd.write(8);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(32);
lcd.write(32);
lcd.write(1);
}
void custom8(int col)
{
lcd.setCursor(col, 0);
lcd.write(2);
lcd.write(3);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(2);
lcd.write(6);
lcd.write(1);
}
void custom9(int col)
{
lcd.setCursor(col, 0);
lcd.write(2);
lcd.write(3);
lcd.write(1);
lcd.setCursor(col, 1);
lcd.write(7);
lcd.write(6);
lcd.write(1);
}
void printNumber(int value, int col)
{
if (value == 0) {
custom0(col);
} if (value == 1) {
custom1(col);
} if (value == 2) {
custom2(col);
} if (value == 3) {
custom3(col);
} if (value == 4) {
custom4(col);
} if (value == 5) {
custom5(col);
} if (value == 6) {
custom6(col);
} if (value == 7) {
custom7(col);
} if (value == 8) {
custom8(col);
} if (value == 9) {
custom9(col);
}
}
void setup()
{
lcd.init();
lcd.backlight();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(button, INPUT);
digitalWrite(button, HIGH);
lcd.createChar(1, arrow);
for (yy=0; yy<2; yy++)
{
for (xx=0; xx<16; xx++)
{
lcd.setCursor(xx, yy);
lcd.print(char(1));
delay(130);
lcd.setCursor(xx, yy);
if (yy==0)
lcd.print(" Subscribe "[xx]);
else
lcd.print(" Voltaat "[xx]);
}
}
delay(2000);
lcd.clear();
lcd.createChar(1,bar1);
lcd.createChar(2,bar2);
lcd.createChar(3,bar3);
lcd.createChar(4,bar4);
lcd.createChar(5,bar5);
lcd.createChar(6,bar6);
lcd.createChar(7,bar7);
lcd.createChar(8,bar8);
lcd.setCursor(0, 0);
lcd.print("Rakat Count: ");
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 1);
printNumber(rakats, 13);
}
void loop()
{
long duration = measureDistance();
float distance = duration * 0.034 / 2; // Convert time to distance in cm
// Check for prostration (if the distance is less than 30 cm)
if (distance < prostrationDistance) {
if (!isProstrating) {
isProstrating = true; // Start prostration
prostrations++;
Serial.print("Prostrations: ");
Serial.println(prostrations);
// Calculate rakats
if (prostrations % 2 == 0) { // Every two prostrations count as one rakat
rakats++;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 1);
printNumber(rakats, 13);
}
}
} else {
isProstrating = false; // End prostration
}
if (digitalRead(button) == LOW) {
prostrations = 0;
rakats = 0;
isProstrating = false;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
printNumber(rakats, 13);
}
delay(100); // Delay to avoid false readings
}
long measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration;
}