I currently have an issue where I am trying to replace a proximity sensor, with a ping sensor... this is mostly due to distance issues (not enough)
The following code works for proximity...
const int sensorPin1 = 0; //analog pin 0
const int ledPin1 = 9; // LED1, connected to digital pin 9
const int sensorPin2 = 1; //analog pin 0
const int ledPin2 = 10; // LED2, connected to digital pin 9
const int sensorPin3 = 2; //analog pin 0
const int ledPin3 = 11; // LED3, connected to digital pin 9
int sensorValue1, sensorValue2, sensorValue3;
int ledVal1 = 0;
int ledVal2 = 0;
int ledVal3 = 0;
unsigned long lastFullBrightnessTimeL1 = 0;
const unsigned long fullBrightnessIntervalL1 = 5000;
unsigned long lastFadeTimeL1 = 0;
unsigned long lastFullBrightnessTimeL2 = 0;
const unsigned long fullBrightnessIntervalL2 = 5000;
unsigned long lastFadeTimeL2 = 0;
#include <NewPing.h>
#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
//const unsigned long fadeInterval = 1;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);// sets the pins as output
// pinMode(s3Pin, INPUT);
Serial.begin(9600); // ...set up the serial ouput in 0004 format
}
// Main program
void loop() {
unsigned long currentMillis = millis();
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
sensorValue3 = analogRead(sensorPin3);
// Set 1
if (sensorValue1 >= 150) { // Person in range
if (ledVal1 <= 254)
ledVal1++;
else
lastFullBrightnessTimeL1 = currentMillis;
}
else { // Person has moved away
if (ledVal1 >= 1 && currentMillis - lastFullBrightnessTimeL1 > fullBrightnessIntervalL1)
ledVal1--;
}
if (sensorValue1 <= 150 && ledVal1 <= 1)
{ ledVal1 = 0;
analogWrite(ledPin1, ledVal1);
}
analogWrite(ledPin1, ledVal1);
//Set 2
if (sensorValue2 >= 90) { // Person in range
if (ledVal2 <= 254)
ledVal2++;
else
lastFullBrightnessTimeL2 = currentMillis;
}
else { // Person has moved away
if (ledVal2 >= 1 && currentMillis - lastFullBrightnessTimeL2 > fullBrightnessIntervalL2)
ledVal2--;
}
if (sensorValue2 <= 90 && ledVal2 <= 1)
{ ledVal2 = 0;
analogWrite(ledPin2, ledVal2);
}
analogWrite(ledPin2, ledVal2);
//Set3
// delay(50);
//Serial.println(sensorValue1);
Serial.println(sensorValue1);
}
I moved over to the Arduino base ping sensor and was getting to many fluctuations and problems with the built in delay... so I tried your version. Much better, but I am having issues with the LEDs. The fade kinda works however it is incredibly slow, and flickers and locks on and never goes off sometimes. The original fade, builds in fairly quickly and builds out after a set time if no ones around.
The whole installation works like this, as some one approaches the LED is triggered and it fades up, if you don't get inside the set distance then it fades back out, if you get inside or stand for a brief period of time the LED locks on for a set amount of time then if the sensor goes back to reading outside of the set distance the LED fades out but only after a set amount of time.
I tried this code, Side note, I need to run several sensors and several different lights at the same time all doing their own thing. I am running all power externally (conditioned block).
#include <NewPing.h>
#define SONAR_NUM 3
#define MAX_DISTANCE 200
#define PING_INTERVAL 33
unsigned long pingTimer[SONAR_NUM];
uint8_t currentSensor = 0;
NewPing sonar[SONAR_NUM] = {
NewPing(7, 7, MAX_DISTANCE),
NewPing(6, 6, MAX_DISTANCE),
NewPing(5, 5, MAX_DISTANCE)
};
const int sensorPin1 = 0; //analog pin 0
const int ledPin1 = 9; // LED1, connected to digital pin 9
//int sensorValue1, sensorValue2, sensorValue3;
int ledVal1 = 0;
unsigned long lastFullBrightnessTimeL1 = 0;
const unsigned long fullBrightnessIntervalL1 = 5000;
unsigned long lastFadeTimeL1 = 0;
unsigned long lastFullBrightnessTimeL2 = 0;
const unsigned long fullBrightnessIntervalL2 = 5000;
unsigned long lastFadeTimeL2 = 0;
void setup() {
Serial.begin(115200);
pinMode(ledPin1, OUTPUT);
//pinMode(ledPin2, OUTPUT);
// pinMode(ledPin3, OUTPUT);// sets the pins as output
pingTimer[0] = millis() + 75;
for (uint8_t i = 1; i < SONAR_NUM; i++)
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) {
if (millis() >= pingTimer[i]) {
pingTimer[i] += PING_INTERVAL * SONAR_NUM;
sonar[currentSensor].timer_stop();
currentSensor = i;
sonar[currentSensor].ping_timer(echoCheck);
}
}
// Other code that *DOESN'T* analyze ping results can go here.
}
void echoCheck() {
if (sonar[currentSensor].check_timer())
pingResult(currentSensor, sonar[currentSensor].ping_result / US_ROUNDTRIP_CM);
}
void pingResult(uint8_t sensor, int sensor1) {
unsigned long currentMillis = millis();
if (sensor1 <= 20) { // Person in range
if (ledVal1 <= 254)
ledVal1++;
else
lastFullBrightnessTimeL1 = currentMillis;
}
else { // Person has moved away
if (ledVal1 >= 1 && currentMillis - lastFullBrightnessTimeL1 > fullBrightnessIntervalL1)
ledVal1--;
}
if (sensor1 >= 20 && ledVal1 <= 1)
{ ledVal1 = 0;
analogWrite(ledPin1, ledVal1);
}
analogWrite(ledPin1, ledVal1);
// The following code would be replaced with your code that does something with the ping result.
Serial.println(sensor1);
//Serial.print(" ");
//Serial.print(cm);
//Serial.println("cm");
}