const int piezo = 0;
const int LED = 13;
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
if (analogRead(piezo) >= 5) {
digitalWrite(LED, HIGH);
delay(5000);
digitalWrite(LED, LOW);
} else {
digitalWrite(LED, LOW);
}
}
// Sets Analog Pin 0 (i.e. A0) as an analog input for the piezoelectric sensor
const float piezo = A0;
// Defines pin A1A on motor control as pin digital pin 3 on the arduino
const int A1A = 3;
// Defines pin A1B on motor control as pin digital pin 4 on the arduino
const int A1B = 4;
// Variable 'times' to count how many times the piezoelectric sensor detects a value above 20
int times = 0;
void setup() {
// Defines pin 8 as an output for buzzer
pinMode(8, OUTPUT);
// Defines pin A1A (3) as output
pinMode(A1A, OUTPUT);
// Defines pin A1B (4) as output
pinMode(A1B, OUTPUT);
// Starts with the motor off
analogWrite(A1A, 0);
// Starts serial communication with a transfer rate of 115,200 bits per second
Serial.begin(115200);
}
void loop() {
while (1) {
// If the reading from the piezo sensor is greater than 20 and variable 'times'' remainder is 0 (i.e. even number), motor is signaled to run
if (analogRead(piezo) >= 20 && (times % 2) == 0) {
// Turns on motor to 'full blast' mode
analogWrite(A1A, 255);
// Turns on a buzzer to make two tones which signals to the user that the piezo has read the vibration and that the motor has been activated
int i = 0;
while (1) {
tone(8,445);
delay(200);
noTone(8);
if (i == 2) {
break;
}
i++;
}
// If reading is greater than or equal to 20 and if variable 'times' is an odd number, motor is signaled to stop
} else if (analogRead(piezo) && (times % 2) != 0) {
// Turns motor off
analogWrite(A1A, 0);
}
}
}
int i = 0;
while (1) {
tone(8,445);
delay(200);
noTone(8);
if (i == 2) {
break;
}
i++;
}
for (int i = 0; i < 2; i++) {
tone(8, 445);
delay(200)
noTone(8);
}
© 2022 Mustafa Omran