Need help (urgent) to figure out a customised version of programming code.
I am working on vehicle security alarm project where vibration sensor will trigger both the siren and start a dialer (phone) which will place call to vehicle owner then vehicle owner will press buttons to cutt off engine etc.
For automatic dialer I m using this youtube tutorial Hacked Phone - Arduino GSM Auto Dialler + Free Schematic and Code - YouTube and for vibration sensor & alarm I am using this youtube tutorial How to Avoid False Positives with a High Sensitivity Piezo Vibration Sensor - YouTube. But facing difficulty in combining both the codes of these two tutorials.
I am sharing the code here for auto mobile dialer:
//Constants That Won't Change
const int call = 11; //Call Button Output
const int back = 10; //Back Button Output
const int keypad7 = 9; //No:7 Button Output
const int alarm_active_led = 8; //Alarm Triggered LED Indicator
const int power_led = 7; //Power OK LED Indicator
const int trigger_input = 6; //Alarm Trigger Input
int repeat =0;
//Variables That Will Change
int trigger_state = 0; //trigger_state = Trigger Input (pin7)
//Setup Routine Runs Once When Reset Is Pressed
void setup() {
//Initialize The Digital Pins As Inputs/ Outputs
pinMode(call, OUTPUT); //Define "Call" pinMode as Output
pinMode(back, OUTPUT); //Define "Back" pinMode as Output
pinMode(keypad7, OUTPUT); //Define "keypad7" pinMode as Output
pinMode(alarm_active_led, OUTPUT); //Define "Alarm LED" pinMode as Output
pinMode(power_led, OUTPUT); //Define "Power LED" pinMode as Output
pinMode(trigger_input, INPUT); //Define "Cancel" pinMode as Input
}
//Loop Runs Indefinately
void loop() {
trigger_state = digitalRead(trigger_input);//Read Trigger Input State
digitalWrite(power_led, HIGH); //Turn Power LED On
digitalWrite(alarm_active_led, LOW); //Turn Alarm LED Off
if (trigger_state == HIGH) { //If Trigger Input HIGH,
//Run Sequence Below
digitalWrite(alarm_active_led, HIGH);
digitalWrite(back, HIGH); //Press Back Button
delay(100); //Wait 100mS
digitalWrite(back, LOW); //Depress Back Button
delay(100); //Wait 100mS
digitalWrite(back, HIGH); //Press Back Button
delay(100); //Wait 100mS
digitalWrite(back, LOW); //Depress Back Button
delay(100); //Wait 100mS
digitalWrite(keypad7, HIGH); //Press keypad7 Button
delay(100); //Wait 100mS
digitalWrite(keypad7, LOW); //Depress keypad Button
delay(100); //Wait 100mS
digitalWrite(call, HIGH); //Press call Button
delay(100); //Wait 100mS
digitalWrite(call, LOW); //Depress call Button
delay(20000); //Wait 20S
digitalWrite(back, HIGH); //Press back Button
delay(200); //Wait 100mS
digitalWrite(back, LOW); //Depress back Button
digitalWrite(alarm_active_led, HIGH); //Flash Alarm LED
delay(100); //Flash Alarm LED
digitalWrite(alarm_active_led, LOW); //Flash Alarm LED
delay(100); //Flash Alarm LED
while(trigger_state == HIGH){ //If Trigger Input HIGH
trigger_state = digitalRead(trigger_input);//Read Trigger Input State
//Loop Sequence Below
digitalWrite(alarm_active_led, HIGH); //Flash Alarm LED
delay(100); //Flash Alarm LED
digitalWrite(alarm_active_led, LOW); //Flash Alarm LED
delay(100); //Flash Alarm LED
if (trigger_state == LOW) { //If Trigger Input Low
break; //Break
}
digitalWrite(alarm_active_led, HIGH); //Turn Alarm LED On
}
}
}
Code for vibration sensor & alarm:
#define VIBRATION_SENSOR_DIGITAL_INPUT_PIN 10
#define VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN 8
#define VIBRATION_SAMPLE_ARRAY_SIZE 100
// if we 5 or more vibration events over a five second period then a vibration alarm is triggered
#define VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT 5
#define VIBRATION_EVENTS_ALARM_THRESHOLD_PERIOD_MILLIS 5000
long vibrationEvents[VIBRATION_SAMPLE_ARRAY_SIZE];
int vibrationEventIndex;
void setup(){
Serial.begin(19200);
pinMode(VIBRATION_SENSOR_DIGITAL_INPUT_PIN, INPUT);
pinMode(VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN, OUTPUT);
vibrationEventIndex = 0;
clearVibrationEvents();
}
void loop(){
if(digitalRead(VIBRATION_SENSOR_DIGITAL_INPUT_PIN) == HIGH){
long currentMillis = millis();
addVibrationSample(currentMillis);
if(isVibrationAlarmTiggered()){
triggerAlarm();
Serial.println(String(millis()) + "\t ALARM");
}
delay(100); // wait for current vibration shock to subside
}
}
void addVibrationSample(long vibrationMillis){
vibrationEvents[vibrationEventIndex++] = vibrationMillis;
if(vibrationEventIndex >= VIBRATION_SAMPLE_ARRAY_SIZE){
vibrationEventIndex = 0; // wrap vibration sample index around when we get to end of sample array
}
}
boolean triggerAlarm(){
digitalWrite(VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN, HIGH);
delay(1000);
digitalWrite(VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN, LOW);
clearVibrationEvents();
}
void clearVibrationEvents(){
for(int i = 0; i < VIBRATION_SAMPLE_ARRAY_SIZE ; ++i){
vibrationEvents[i] = -1;
}
}
boolean isVibrationAlarmTiggered(){
long thresholdMillis = millis() - VIBRATION_EVENTS_ALARM_THRESHOLD_PERIOD_MILLIS;
if(thresholdMillis < 0) thresholdMillis = 0;
int numVibrationsSinceThreshold = 0;
for(int i = 0; i < VIBRATION_SAMPLE_ARRAY_SIZE ; ++i){
if(vibrationEvents[i] >= thresholdMillis){
++numVibrationsSinceThreshold;
}
}
Serial.println(String(millis()) + "\t# events: " + String(numVibrationsSinceThreshold));
boolean alarmTriggered = false;
if(numVibrationsSinceThreshold >= VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT){
alarmTriggered = true;
}
return alarmTriggered;
}
Combined Code:
#define VIBRATION_SENSOR_DIGITAL_INPUT_PIN 10
#define VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN 8
#define VIBRATION_SAMPLE_ARRAY_SIZE 100
// if we 5 or more vibration events over a five second period then a vibration alarm is triggered
#define VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT 5
#define VIBRATION_EVENTS_ALARM_THRESHOLD_PERIOD_MILLIS 5000
long vibrationEvents[VIBRATION_SAMPLE_ARRAY_SIZE];
int vibrationEventIndex;
//Constants That Won't Change
const int call = 11; //Call Button Output
const int back = 10; //Back Button Output
const int keypad7 = 9; //No:7 Button Output
const int alarm_active_led = 8; //Alarm Triggered LED Indicator
const int power_led = 7; //Power OK LED Indicator
const int trigger_input = 6; //Alarm Trigger Input
int repeat =0;
//Variables That Will Change
int trigger_state = 0; //trigger_state = Trigger Input (pin7)
//Setup Routine Runs Once When Reset Is Pressed
void setup() {
Serial.begin(19200);
pinMode(VIBRATION_SENSOR_DIGITAL_INPUT_PIN, INPUT);
pinMode(VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN, OUTPUT);
vibrationEventIndex = 0;
clearVibrationEvents();
//Initialize The Digital Pins As Inputs/ Outputs
pinMode(call, OUTPUT); //Define "Call" pinMode as Output
pinMode(back, OUTPUT); //Define "Back" pinMode as Output
pinMode(keypad7, OUTPUT); //Define "keypad7" pinMode as Output
pinMode(alarm_active_led, OUTPUT); //Define "Alarm LED" pinMode as Output
pinMode(power_led, OUTPUT); //Define "Power LED" pinMode as Output
pinMode(trigger_input, INPUT); //Define "Cancel" pinMode as Input
}
void loop(){
if(digitalRead(VIBRATION_SENSOR_DIGITAL_INPUT_PIN) == HIGH){
long currentMillis = millis();
addVibrationSample(currentMillis);
if(isVibrationAlarmTiggered()){
triggerAlarm();
Serial.println(String(millis()) + "\t ALARM");
}
delay(100); // wait for current vibration shock to subside
}
}
void addVibrationSample(long vibrationMillis){
vibrationEvents[vibrationEventIndex++] = vibrationMillis;
if(vibrationEventIndex >= VIBRATION_SAMPLE_ARRAY_SIZE){
vibrationEventIndex = 0; // wrap vibration sample index around when we get to end of sample array
}
}
boolean triggerAlarm(){
digitalWrite(VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN, HIGH);
delay(1000);
digitalWrite(VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN, LOW);
clearVibrationEvents();
}
void clearVibrationEvents(){
for(int i = 0; i < VIBRATION_SAMPLE_ARRAY_SIZE ; ++i){
vibrationEvents[i] = -1;
}
}
boolean isVibrationAlarmTiggered(){
long thresholdMillis = millis() - VIBRATION_EVENTS_ALARM_THRESHOLD_PERIOD_MILLIS;
if(thresholdMillis < 0) thresholdMillis = 0;
int numVibrationsSinceThreshold = 0;
for(int i = 0; i < VIBRATION_SAMPLE_ARRAY_SIZE ; ++i){
if(vibrationEvents[i] >= thresholdMillis){
++numVibrationsSinceThreshold;
}
}
Serial.println(String(millis()) + "\t# events: " + String(numVibrationsSinceThreshold));
boolean alarmTriggered = false;
if(numVibrationsSinceThreshold >= VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT){
alarmTriggered = true;
}
return alarmTriggered;
}