Cant' get it to work properly...

I work on a parachute system for a rocket and I need this code to be very reliable, looked for forums and tutorials but everytime something is wrong with it, the wiring is good, I verified it 3 times. So, i based my system on three LDR, one which have to detect when there is no more light in the rocket for the liftoff then 5 seconds til the apogee and move a servo 90 degrees to release parachute, then a backup system if the first one does’nt work, two LDR facing down or up, when the down one have bigger value than the other it should verifies if the first system is good if not it directly releases the chute. I did quite a few differents codes but no one worked… help please…

here is the code :

int LedPinW1 = 12;
int LedPinB1 = 11;
int LedPinR1 = 10;
int LedPinG1 = 9;
int LedPinW2 = 8;
int LDRPin1 = A3;
int LDRPin2 =A4;
int LDRPin3 = A5;
int RocketState = 0;
int servopos = 0;
#include <Servo.h>
Servo Servo1;
unsigned int LDRval1 = 0;
unsigned int LDRval2 = 0;
unsigned int LDRval3 = 0;

void setup() {
Servo1.attach(4);
Servo1.write(0);
pinMode (LedPinW1, OUTPUT);
pinMode (LedPinB1, OUTPUT);
pinMode (LedPinR1, OUTPUT);
pinMode (LedPinG1, OUTPUT);
pinMode (LedPinW2, OUTPUT);
pinMode (LDRPin1, INPUT);
pinMode (LDRPin2, INPUT);
pinMode (LDRPin3, INPUT);
pinMode (ButtonPin1, INPUT);
pinMode (ButtonPin2, INPUT);
delay(1000);
digitalWrite (LedPinW1, HIGH);

}

void loop() {

LDR;

if(LDRval2 < 400)
{
RocketState++;
digitalWrite(LedPinB1, HIGH);
delay(5000);
Servo1.write(0);
digitalWrite(LedPinR1, HIGH);
delay(5000);
digitalWrite(LedPinB1, LOW);
delay(5000);
digitalWrite(LedPinR1, LOW);
}
else
{
digitalWrite(LedPinB1, LOW);
}

LDR;

if(LDRval1 < LDRval3)
{
if(TrappeState = 0)
{
digitalWrite(LedPinB1, HIGH);
delay(5000);
Servo1.write(0);
digitalWrite(LedPinR1, HIGH);
delay(5000);
digitalWrite(LedPinB1, LOW);
}
}
}

void LDR () {
LDRval1 = analogRead(LDRPin1);
LDRval2 = analogRead(LDRPin2);
LDRval3 = analogRead(LDRPin3);
}

LDR;What is this line of code meant to do ? Are you trying to call the LDR() function ?

yes, thats'it, does'nt work ?

Those are some mighty long delays. 5000 is 5 seconds. One if being met does nothing for other ifs until it's done.

Also your servo never gets anything but a 0

I know need the time for the rocket to reach 300 meters high.
yes effectively, something like this is possibly a bit better :

int LedPinW1 = 12;
int LedPinB1 = 11;
int LedPinR1 = 10;
int LedPinG1 = 9;
int LedPinW2 = 8;
int LDRPin1 = A3;
int LDRPin2 =A4;
int LDRPin3 = A5;
int RocketState = 0;
int servopos = 0;
#include <Servo.h>
Servo Servo1;
unsigned int LDRval1 = 0;
unsigned int LDRval2 = 0;
unsigned int LDRval3 = 0;

void setup() {
Servo1.attach(4);
Servo1.write(0);
pinMode (LedPinW1, OUTPUT);
pinMode (LedPinB1, OUTPUT);
pinMode (LedPinR1, OUTPUT);
pinMode (LedPinG1, OUTPUT);
pinMode (LedPinW2, OUTPUT);
pinMode (LDRPin1, INPUT);
pinMode (LDRPin2, INPUT);
pinMode (LDRPin3, INPUT);
pinMode (ButtonPin1, INPUT);
pinMode (ButtonPin2, INPUT);
delay(1000);
digitalWrite (LedPinW1, HIGH);

}

void loop() {

LDR;

if(LDRval2 < 400)
{
RocketState++;
digitalWrite(LedPinB1, HIGH);
delay(5000);
Servo1.write(90);
digitalWrite(LedPinR1, HIGH);
delay(5000);
digitalWrite(LedPinB1, LOW);
delay(5000);
digitalWrite(LedPinR1, LOW);
}
else
{
digitalWrite(LedPinB1, LOW);
}

LDR;

if(LDRval1 < LDRval3)
{
if(TrappeState = 0)
{
digitalWrite(LedPinB1, HIGH);
delay(5000);
Servo1.write(90);
digitalWrite(LedPinR1, HIGH);
delay(5000);
digitalWrite(LedPinB1, LOW);
}
}
}

void LDR () {
LDRval1 = analogRead(LDRPin1);
LDRval2 = analogRead(LDRPin2);
LDRval3 = analogRead(LDRPin3);
}

theo_vador: yes, thats'it, does'nt work ?

You think UKHeliBob might have given you a clue in his reply?

OK, so far I’m here

int LedW1 = 12;
int LedB1 = 11;
int LedR1 = 10;
int LedG1 = 9;
int LedW2 = 8;
int LDR1 = A3;
int LDR2 =A4;
int LDR3 = A5;
int RocketState = 0;
int servopos = 0;
#include <Servo.h>
Servo Servo1;
unsigned int LDRval1 = 0;
unsigned int LDRval2 = 0;
unsigned int LDRval3 = 0;

void setup() {
Servo1.attach(4);
Servo1.write(0);
pinMode (LedW1, OUTPUT);
pinMode (LedB1, OUTPUT);
pinMode (LedR1, OUTPUT);
pinMode (LedG1, OUTPUT);
pinMode (LedW2, OUTPUT);
pinMode (LDR1, INPUT);
pinMode (LDR2, INPUT);
pinMode (LDR3, INPUT);
delay(1000);
digitalWrite (LedW1, HIGH);

}

void loop() {

LDR ();

if(LDRval2 < 400)
{
RocketState++;
digitalWrite(LedB1, HIGH);
delay(5000);
Servo1.write(90);
digitalWrite(LedR1, HIGH);
delay(5000);
digitalWrite(LedB1, LOW);
delay(5000);
digitalWrite(LedR1, LOW);
}
else
{
digitalWrite(LedB1, LOW);
}

LDR();

if(LDRval1 < LDRval3)
{
if(RocketState = 0)
{
digitalWrite(LedB1, HIGH);
delay(5000);
Servo1.write(90);
digitalWrite(LedR1, HIGH);
delay(5000);
digitalWrite(LedB1, LOW);
}
}
}

void LDR () {
LDRval1 = analogRead(LDR1);
LDRval2 = analogRead(LDR2);
LDRval3 = analogRead(LDR3);
}

Please stop posting inline code like that. It's all in the 'book'.

  if(RocketState = 0)oops

Why would you not use an accelerometer to trigger 'chute release? Launch/apogee signature very obvious from an accelerometer, will work at night too!

I think the LDR solution is quite clever. If the 'up' LDR sees ground and the 'down' LDR sees sky then you know your rocket is tumbling and it's time to release the chute. If they're both bright then the motor is still running and you're the right way up.

Like XKCD's Up Goer Five "If this end points towards sky you are having a very bad day."

BTW first get the physical mechanism reliable, even these guys got it wrong and they seem to be well clued-up:

[ start at 38 mins in ] https://www.youtube.com/watch?v=BJppeRWSD94

thanks, I can't get an accelerometer, expensive, big an dcomplex to use, LDRs are easier, I already did quite a few rockets, this is my 18th ;) so the mechanical side is OK, just need some help for the code, PS I have to launch at some special events, can't do it everytime so I need the code for toomorrow to launch Friday, that's very short and that's why I ask it, originally I didn't want to get help... but well, you're great guys too :D

OK, so far I’m here

OK, but that doesn’t give us any idea on what’s wrong. What does this code do when you test it? What did you want it to do instead?

Which LDR is which? Give them proper names so it’s easy to see in the code “if(engineLDRValue<400)…”

And please use [ code ] tags next time. The forum software eats some of your code and turns it into smileys. 8)

LDR1 is at the top, 2 looking on the side, that's the one which should detect when there is no more light during liftoff and the third is facing down. when I run the code, the blue and red led light on but nothing more :(

ok, I slightly changed the code, but the servo does'nt move, I already rewired all, 2 times... do you think there's a code problem or something else ? I also added a very bright RGB LED (WS2811) to find the rocket if it land in the forest ;)

theo_vador: ok, I slightly changed the code ...

What code is that? Do you not know how to use code tags? Really dude. Read the how to... it will save you time, both in the long term, AND in the short term.