Hi Arduino lovers
I have one question about merging sketches, is there any program will do that ?, because i always end up getting too many errors especially in the loop part.
i got this sketch from Instructable form and it's using LED as motion sensor, the problem is instead of using only one led sensor with one output, I'm planning to use 3 LEDs with different 3 digital outputs
please help me do that because i tried but with no benefit.
int led = 13; // the pin where you will put the LED
int sensorpin = A3; // the analog pin where you put your sensorLED
int resetteller = 0; // the rest are counters and variables to calculate with
int sens = 0;
int teller = 0;
int basis = 1024;
int test = 1024;
int test2 = 1024;
int test3 = 1024;
// this are the values to play with to get better (or worse) results
int marge = 5; // the space between a positive and negative reading
int vertraging = 1; // the speed of the readings; a lower number is a higher speed
int samples = 70; // the amount of samples to compare to make one reading
int resetsamples = 30; // how many cycles to run the light on before you don't trust the value anymore
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600); // no real part of the program, just for debugging
for(teller =0; teller < samples; teller++) {// remember the lowest value out of many readings
sens = analogRead(sensorpin);
if (sens < basis){basis = sens;}
delay(vertraging); // the sensor needs a delay here to catch its breath
}
}
void loop() {
for(teller =0; teller < samples; teller++) {// remember the lowest value out of many readings
sens = analogRead(sensorpin);
delay(vertraging); // the sensor needs a delay here to catch its breath
if (sens < test){
test3 = sens; // remember the 3 lowest readings
test2 = test3;
test = test2;}
}
if (test < basis-marge && test2 < basis-marge && test3 < basis-marge){//all 3 low readings mus be < the basis reading
digitalWrite(led, HIGH);
resetteller++; // count how long the LED stays on
}
else{
digitalWrite(led, LOW);
basis = test; // if the lowest test reading is higher than the basis, basis will be reset
resetteller = 0;
}
if (resetteller > resetsamples){basis = test;}//if LED stays on to long, we don't trust it and reset basis
Serial.print(basis);Serial.print(" ");Serial.print(test);Serial.print(" ");Serial.println(sens);//just for debugging
test = 1024;
}
Thanks
is there any program will do that ?
The one between your ears.
please help me do that because i tried
What did you try?
i got this sketch from Instructable form
You misspelled crap.
thanks for replay anyway
I've tried this one
int led = 13; // the pin where you will put the LED
int sensorpin = A3; // the analog pin where you put your sensorLED
int resetteller = 0; // the rest are counters and variables to calculate with
int sens = 0;
int teller = 0;
int basis = 1024;
int test = 1024;
int test2 = 1024;
int test3 = 1024;
int led22 = 12; // the pin where you will put the LED
int sensorpin22 = A2; // the analog pin where you put your sensorLED
int resetteller22 = 0; // the rest are counters and variables to calculate with
int sens22 = 0;
int teller22 = 0;
int basis22 = 1024;
int test22 = 1024;
int test222 = 1024;
int test322 = 1024;
// this are the values to play with to get better (or worse) results
int marge = 6.2; // the space between a positive and negative reading
int vertraging = 1; // the speed of the readings; a lower number is a higher speed
int samples = 70; // the amount of samples to compare to make one reading
int resetsamples = 30; // how many cycles to run the light on before you don't trust the value anymore
int marge22 = 6.2; // the space between a positive and negative reading
int vertraging22 = 1; // the speed of the readings; a lower number is a higher speed
int samples22 = 70; // the amount of samples to compare to make one reading
int resetsamples22 = 30; // how many cycles to run the light on before you don't trust the value anymore
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600); // no real part of the program, just for debugging
for(teller =0; teller < samples; teller++) {// remember the lowest value out of many readings
sens = analogRead(sensorpin);
if (sens < basis){basis = sens;}
delay(vertraging); // the sensor needs a delay here to catch its breath
}
{
pinMode(led22, OUTPUT);
Serial.begin(9600); // no real part of the program, just for debugging
for(teller22 =0; teller22 < samples; teller++) {// remember the lowest value out of many readings
sens22 = analogRead(sensorpin22);
if (sens22 < basis22){basis22 = sens22;}
delay(vertraging22); // the sensor needs a delay here to catch its breath
}
}
void loop() {
for(teller =0; teller < samples; teller++)(teller22 =0; teller22 < samples; teller22++) {// remember the lowest value out of many readings
sens = analogRead(sensorpin);
sens22 = analogRead(sensorpin22);
delay(vertraging);
delay(vertraging22); // the sensor needs a delay here to catch its breath
if (sens < test){
test3 = sens; // remember the 3 lowest readings
test2 = test3;
test = test2;}
}
if (test < basis-marge && test2 < basis-marge && test3 < basis-marge)(sens22 < test22){//all 3 low readings mus be < the basis reading
digitalWrite(led, HIGH);
resetteller++; // count how long the LED stays on
}
if (test22 < basis22-marge22 && test222 < basis22-marge22 && test322 < basis22-marge22){//all 3 low readings mus be < the basis reading
digitalWrite(led22, HIGH);
resetteller22++; // count how long the LED stays on
}
else{
digitalWrite(led, LOW);
basis = test; // if the lowest test reading is higher than the basis, basis will be reset
resetteller = 0;
digitalWrite(led22, LOW);
basis22 = test22; // if the lowest test reading is higher than the basis, basis will be reset
resetteller22 = 0;
}
if (resetteller > resetsamples){basis = test;}//if LED stays on to long, we don't trust it and reset basis
Serial.print(basis);Serial.print(" ");Serial.print(test);Serial.print(" ");Serial.println(sens);//just for debugging
test = 1024;
if (resetteller22 > resetsamples22){basis22 = test22;}//if LED stays on to long, we don't trust it and reset basis
Serial.print(basis22);Serial.print(" ");Serial.print(test22);Serial.print(" ");Serial.println(sens22);//just for debugging
test22 = 1024;
}
and this is the website that i may forgot the "S" in the end
wx4me:
I have one question about merging sketches, is there any program will do that ?,
No, but it's not that hard. Indeed it is a routine part of programming. Any Arduino programme may be divided into three sections:
-
All that preliminary stuff where you call libraries and define variables etc.
-
Setup, where commands are executed once
-
The loop where commands are executed repeatedly.
To combine programmes, you just need to bring the preliminary stuff together, and the Setup stuff together. The loop is only slightly more complicated in that you need to determine the order in which things are to happen, hence the order in which they occur in the loop. It really is that simple. Yes, it might take a bit of perseverance, but hat is what the game is about....
int marge = 6.2; // the space between a positive and negative reading
How can you reasonably expect to store that value in that variable?
Might I suggest that you stop putting the { on the line with the statement? Put it on a new line, so you have somethinng to line the close brace up with. Then, properly indent the code between the { and the }. There is a tool, on the Tools menu to do that, but it only works when you don't insert random open braces in your code (which you did).
You'll find, I think, that seeing the structure of the code makes it easier to understand, and to cut and paste (ugh!) blocks for different purposes. (Of course there is an alternative - create a function).
anyway a sketch is compiled arduino code
to merge text nothing best than the "merge" tool
as it is coding you need just read both codes pick functions and rework the loop ; var are auto poped in your mind when you do this 
You might try using the IDE TAB feature