Last active
January 2, 2016 04:59
-
-
Save theapi/8253779 to your computer and use it in GitHub Desktop.
2 ping sensors with ir transmitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <NewPing.h> | |
#include <VirtualWire.h> | |
#define RF_TX_PIN 9 // Pin 12 is the default sender pin so change it here. | |
#define SONAR_NUM 2 // Number of sensors. | |
#define MAX_DISTANCE 500 // Maximum distance (in cm) to ping. | |
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo). | |
// Pins | |
const byte pingPower = 14; | |
const byte triggerLeft = 7; | |
const byte echoLeft = 8; | |
const byte triggerRight = 5; | |
const byte echoRight = 6; | |
byte pingPowerState = 1; | |
unsigned long transmitId = 0; | |
unsigned long lastTransmit = 0; | |
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor. | |
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored. | |
uint8_t currentSensor = 0; // Keeps track of which sensor is active. | |
NewPing sonar[SONAR_NUM] = { // Sensor object array. | |
NewPing(triggerLeft, echoLeft, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. | |
NewPing(triggerRight, echoRight, MAX_DISTANCE), | |
}; | |
// NB: ping returns 0 if transmitting while vw_wait_tx() so don't use it. | |
void transmit(char *buf) { | |
// Send multiple times in the hope it gets through. | |
//for (byte i=0; i<10; i++) { | |
vw_send((uint8_t *)buf, strlen(buf)); | |
//vw_wait_tx(); // Wait until the whole message is gone | |
lastTransmit = millis(); | |
//} | |
} | |
void setup() { | |
Serial.begin(115200); | |
pinMode(pingPower, OUTPUT); | |
vw_set_tx_pin(RF_TX_PIN); | |
vw_setup(2000); // Bits per sec | |
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting. | |
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor. | |
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL; | |
// For now turn on power to the ping now. | |
// @todo when pir interrupts. | |
//digitalWrite(pingPower, HIGH); | |
} | |
void loop() { | |
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors. | |
if (millis() >= pingTimer[i]) { // Is it this sensor's time to ping? | |
pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged. | |
if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results. | |
sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance). | |
currentSensor = i; // Sensor being accessed. | |
cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor. | |
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo). | |
} | |
} | |
// Other code that *DOESN'T* analyze ping results can go here. | |
if (millis() - lastTransmit > 1000) { | |
// Proof of concept to power cycle the pings | |
pingPowerState =!pingPowerState; | |
digitalWrite(pingPower, pingPowerState); | |
transmitId++; | |
char buf[50]; | |
sprintf(buf, "id=%lu", transmitId); | |
transmit(buf); | |
} | |
} | |
void echoCheck() { // If ping received, set the sensor distance to array. | |
if (sonar[currentSensor].check_timer()) | |
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM; | |
} | |
void oneSensorCycle() { // Sensor ping cycle complete, do something with the results. | |
// The following code would be replaced with your code that does something with the ping results. | |
for (uint8_t i = 0; i < SONAR_NUM; i++) { | |
Serial.print(i); | |
Serial.print("="); | |
Serial.print(cm[i]); | |
Serial.print("cm "); | |
} | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment