Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there, I am a complete newbie to arduino and using c/c++. For this project I had to make up a use case and based on that use case get three arduino sensors for it.
My use case is a smart cane which assists the visually impaired, sensors include a PIR Motion Sensor, Ultrasonic Sensor and a piezoelectric sensor (disk shaped) and a buzzer.

How it should work :
1. Click the piezoelectric sensor
2.PIR sensor checks for any moving objects
3.Whilst the PIR is checking for moving objects, the ultrasonic sensor and buzzer are activated.
4. If a moving object is picked up by the PIR and comes within a specific distance of the ultrasonic sensor the buzzer should alert the user.

I have found code for the ultrasonic sensor/buzzer, PIR sensor and Piezoelectric Sensor.

I need some help on how to combine the three Arduino sketches for the sensors.
I also need help and advice on how I can go about getting the three sensors to work in they way I want them to which is mentioned above.

CODE for PiezoElectric Sensor:
/* Knock Sensor

 * ----------------

 *

 * Program using a Piezo element as if it was a knock sensor.

 *

 * We have to basically listen to an analog pin and detect 

 * if the signal goes over a certain threshold. It writes

 * "knock" to the serial port if the Threshold is crossed,

 * and toggles the LED on pin 13.

 *

 * (cleft) 2005 D. Cuartielles for K3

 * edited by Scott Fitzgerald 14 April 2013

 */



int ledPin = 13;

int knockSensor = 0;               

byte val = 0;

int statePin = LOW;

int THRESHOLD = 100;



void setup() {

 pinMode(ledPin, OUTPUT); 

 Serial.begin(9600);

}



void loop() {

  val = analogRead(knockSensor);     

  if (val >= THRESHOLD) {

    statePin = !statePin;

    digitalWrite(ledPin, statePin);

    Serial.println("Knock!");



  }

  delay(100);  // we have to make a delay to avoid overloading the serial port

}


CODE for PIR Motion Sensor:
int calibrationTime = 30;        

long unsigned int lowIn;         
long unsigned int pause = 5000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 3;    //the digital pin connected to the PIR sensor's output

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){  
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       if(!lockLow && millis() - lowIn > pause){  
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }



CODE for Ultrasonic Sensor and Buzzer:
#define trigPin 13
#define echoPin 12
int buzzer = 6;

void setup()
{ pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer,OUTPUT);
}

void loop()
{ long duration, distance;
digitalWrite(trigPin, LOW); 
delayMicroseconds(2); 
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); 
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance < 100) // Checking the distance, you can change the value
{ 
// When the the distance below 100cm
digitalWrite(buzzer,HIGH);
} else
{
// when greater than 100cm
digitalWrite(buzzer,LOW); 
} delay(500);
}


What I have tried:

YouTube Tutorials, Websites and Discussed with colleagues.
Posted
Updated 27-Mar-18 5:19am

You should visit this website with the Built in Examples.

Your sensors must interact in some Multithreading and Concurrency to work.

Plain spoken means it that some routines are running at the same time. The biggest problem is some syncronization and data exchange. The simplest method is polling and global data. More sophicated is signalling via system resources as handles or mutexes. I found some example project on Github.

Arduino is a great way to have fun while learn programming ;-)
 
Share this answer
 
Here are a couple of quick points and suggestions to determine if I'm going down the correct path to help you.

Combining the Sketsches
First, I'm assuming you understand that each sketch which runs is basically made up of :
1) Setup (runs one time each time the device is powered up)
2) Loop (runs / loops while the device is powered)

Pins and Ports
Ok, next thing is that Pins are the Arduino pins which are reading input.
Ports are pins that are writing output (setting voltage high for example).

Starting with that basic understanding we can continue thinking about this in the following way.

Insure Each Pin/Port Used For Only One Thing
Insure that no pin (D1, D2, etc) is used for two different things in each of your sketches.
Again this is basic but just setting up the idea here.

If we can guarantee that each Pin/Port is only used for one thing then we can easily combine the SETUP and LOOP portions of the 3 sketches and they _should_* all work the same way as when only one is running.

*However, there could be challenges with speed of processing inputs causing later code in the loop to not be processed until after the earlier parts of the code in the loop.
IE - if D1 is getting data in the script and then doing something and keeps the processor busy then the lower parts of the sketch will not be run until the D1 sketch portion is done. Hope that makes sense.

Here I will now attempt to combine your first two sketches:

C++
// ### SKETCH 1 items ####
int ledPin = 13;
int knockSensor = 0;               
byte val = 0;
int statePin = LOW;
int THRESHOLD = 100;

// ### SKETCH 2 items ####
int calibrationTime = 30;        

long unsigned int lowIn;         
long unsigned int pause = 5000;  
boolean lockLow = true;
boolean takeLowTime;  
int pirPin = 3;    //the digital pin connected to the PIR sensor's output


void setup() {
 pinMode(ledPin, OUTPUT); 
 Serial.begin(9600);

  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
}


void loop() {
  val = analogRead(knockSensor);     

  if (val >= THRESHOLD) {
    statePin = !statePin;
    digitalWrite(ledPin, statePin);
    Serial.println("Knock!");
  }

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){  
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       if(!lockLow && millis() - lowIn > pause){  
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }

  delay(100);  // we have to make a delay to avoid overloading the serial port
}


Conflicts
I don't see any major conflicts in those two sketches -- meaning that you _should_ be able to combine them and run them as I've done. NOTE:I may have copy/pasted code improperly so please examine it more closely.

You may want to put a different LED on an output pin (port) and do a digitalWrite to that led when you do the second portion of the script so you can tell which one is lighting the appropriate LED.

Summary
Maybe you knew all of this, but this is my attempt to get you started so you can see that you should be able to easily combine the scripts with just a little work and keeping in mind that portions of the script may slow the other portions down.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900