I use this code for a processing-sketch, which is playing a video when the value is “1”.
Sometimes this kind of measurement with the sensor has some inaccuracies. Which means sometimes the video is interrupted, because between the measurement there is a “0” instead of a “1”. So the video stops for a few seconds and starts a few seconds later again.
I thought it would be possible to only send a value when “0” or “1” is read three times in a row. With that I would be absolutely sure that there is no more contact and the video has to stop.
I can’t seem to find a way to get a working code for this. Can somebody please help me here?
Have a look at arduino IDE examples section, analog->smoothing:
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// read from the sensor:
long distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
distance = pulseIn(echoPin, HIGH);
readings[readIndex] = distance; // <- in your case is distance, put your code above
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings)
// ...wrap around to the beginning:
readIndex = 0;
int GuessL = 1; // guess that all 10 values lower than limit.
for (int thisReading = 0; thisReading < numReadings; thisReading++) { // Search
if ( readings[thisReading] > 200)
{
GuessL= 0; // wrong, at least one is higher
break;
}
}
int GuessU = 1; // guess that all 10 values higher than limit.
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
if ( readings[thisReading] < 200)
{
GuessU= 0; // wrong, at least one is higher
break;
}
}
if ( GuessL ) Serial.print("0");
if ( GuessU ) Serial.print("1");
// send it to the computer as ASCII digits
Serial.println(average);
delay(100); // delay in between reads for stability, 100 millisec x 10 reading = 1 sec data stable.
}
Well, this one isn't working. I don't get it to give me back "1" or "0" and it seems like it doesn't smoothen the whole process at all. Isn't there a simpler way?
#include <NewPing.h> //you need to download this lib for beter result
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 9 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // max sensor value 500 means 5 meter
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int sensor;
int cm ;
int height1;
void setup (){
Serial.begin (9600);
}
void loop (){
cm = sonar.ping();
if(cm >0)height1 = cm;
if (height1 < 200){
Serial.print("1");
}
else if (height1 > 200) {
Serial.print("0");
}
delay(500);
}
this one uses a other lib newping lib , works lot better
and yours bellow i use a buffer to check if readings are above 0 , when using newping when there is nothing to read it wil give 0 , reading so it wil not read a 0 ,
I put the NewPing library in it and tried to get a average value through the code. Did I do something wrong? Because it doesn’t seem to change that much.
i did if (distance > 0) sensor = always above a 0 reading ,, this happens when there is faulty readings ,
but when you not take that for notice , then you wil have good readings and a 0 disturbing it , because a 0 is always below your treshold ,, this wil give wrong switching effects ,,
so what i use is this method ,, to always have a positive number to work with ,,
;D
this is why i do this , hope you understand ,
i think you need , bigger working space , try this one
#include <NewPing.h>
#define trigPin 8
#define echoPin 9
int sensor;
int sensorbf;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
distance = pulseIn(echoPin, HIGH);
if (distance > 0) sensor = distance; //if some read it wil fill distance value in sensor mem
if (sensor => 1000 )sensor ==1000; // for not exceeding limts of sensor readings when so it wil stay on 1000
if (sensor >= 1 || sensor <= 200 )sensorbf = sensorbf +1; // if sensor reading is between 1 and 200 it wil start adding numbers to sensorbf
if (sensor >= 200 || sensor <= 1000 )sensorbf = sensorbf -1; // if sensor reading is between 200 and 1000 it wil start adding numbers to sensorbf
if (sensorbf < 1 )sensorbf ==1; // for not exceeding limts sensor buffer
if (sensorbf > 50 )sensorbf ==50; // how large the numbere the bigger the treshold
if (sensorbf >= 1){ // you can change the 1 to higer number for holding the treshold longer , takes more steps to reach it when example use 10
Serial.print("working");
delay (500);
}
else if (sensorbf <= 1 ){ // maybe you need to reverse the "0" and "1"
Serial.print("not working");
delay (500); // these delays wil hold down the countdown of sensorbf ,, so now you have 2 tresholds to work with
}
}
#include <NewPing.h>
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 9 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // max sensor value 500 means 5 meter
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int sensor;
int sensorbf;
int cm;
int distance;
void setup() {
Serial.begin (9600);
}
void loop() {
cm = sonar.ping();
distance = cm/ US_ROUNDTRIP_CM; // convert it to cm
if (distance > 0 ) sensor == distance; //if some read it wil fill distance value in sensor mem
if (sensor > 1000 )sensor ==1000; // for not exceeding limts of sensor readings when so it wil stay on 1000
if (sensor >= 1 || sensor <= 200 )sensorbf = sensorbf +1; // if sensor reading is between 1 and 200 it wil start adding numbers to sensorbf
if (sensor >= 200 || sensor <= 1000 )sensorbf = sensorbf -1; // if sensor reading is between 200 and 1000 it wil start adding numbers to sensorbf
if (sensorbf < 1 )sensorbf ==1; // for not exceeding limts sensor buffer
if (sensorbf > 50 )sensorbf ==50; // how large the numbere the bigger the treshold
if (sensorbf >= 1){ // you can change the 1 to higer number for holding the treshold longer , takes more steps to reach it when example use 10
Serial.print("working");
delay (500);
}
else if (sensorbf <= 1 ){ // maybe you need to reverse the "0" and "1"
Serial.print("not working");
delay (500); // these delays wil hold down the countdown of sensorbf ,, so now you have 2 tresholds to work with
}
}
i know ,,LOL i try to find a funky way to do this ,, butt smoking and sketching is not working :-*
now i stil forgot to look if it switches below 200 or above ,LOL
i gone take my funky duino ,, and the sensor , and test this s***
if it works it works ,, my sketch did not give a error ,maybe i overlooked some things ,,
and i try to keep it simple so he still understand ,
i now other ways butt i thinks this wil be less understand able ,,
i always make trashy sketches but they wil work on the end ,,LOL
#include <NewPing.h>
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 9 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // max sensor value 500 means 5 meter
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int sensor;
int sensorbf;
int cm;
int distance;
void setup() {
Serial.begin (9600);
}
void loop() {
cm = sonar.ping();
Serial.println(distance);
Serial.print("sensorbf = ");
Serial.println(sensorbf);
if (cm > 0 ) distance = cm/ US_ROUNDTRIP_CM; // convert it to cm //if some read it wil fill distance value in sensor mem
sensor =distance ;
if (sensor > 100 )sensor ==100; // for not exceeding limts of sensor readings when so it wil stay on 1000
if (sensor >= 40 )sensorbf = sensorbf -2; //.. walk away treshold
if (sensor <= 40 )sensorbf = sensorbf +2;
if (sensorbf <= 1 )sensorbf =1; // for not exceeding limts sensor buffer
if (sensorbf >= 30 )sensorbf =30; // how large the numbere the bigger the treshold
if (sensorbf < 2){
Serial.println("NOT working");
delay (100);
}
else if (sensorbf > 2 ){
Serial.println("working");
delay (500);
}
}
Ok i tested it ,i changed it , i made a walk away treshold ,
starts building when infront of sensor , and building up
so when there is no signal it has to subtract the bulding up number before reaching to 1 again switching of , OK