Solar tracker

Hello!

I’m pretty much a newbie with this arduino language C++. I study in electronic so all the hardware is fine but i am doubting my coding.
I have a problem with my algorythm. I need an other condition to distinct from the 2 movements.

Here’s my coding ( sorry the comments are in french since it’s for school and it’s a french school.)

/* Creation des tags. Nous assignons un tag a une pin physique */

int pinPwm = 3; // Cette pin est celle du PWM elle d/signe la vitesse.
int pinDir = 4; // Cette pin est celle de la direction HIGH = Sens des aiguilles, LOW = Sens contraire.
int relaySet = 32; // Cette pin active le relais 5V. Actif = Moteur 24V. Situe au dessus de la structure.
int relayReset = 34; // Cette pin desactive le relais 5V. Actif = Moteur 12V. situe au bas de la structure.
int photo1 = A0; // Cette pin lit la difference de potentielle entre la photoresistance 1 et une resistance de 10Kohm. Le micro controlleur fais une CAD.
int photo2 = A1; // Cette pin lit la difference de potentielle entre la photoresistance 2 et une resistance de 10Kohm. Le micro controlleur fais une CAD.
int photo3 = A2; // Cette pin lit la difference de potentielle entre la photoresistance 3 et une resistance de 10Kohm. Le micro controlleur fais une CAD.
int photo4 = A3; // Cette pin lit la difference de potentielle entre la photoresistance 4 et une resistance de 10Kohm. Le micro controlleur fais une CAD.

void setup()

/* Cette section definit le role de la pin. Elle est lue une seule fois par le CPU*/

{
/* SORTIE */

pinMode(pinPwm,OUTPUT);
pinMode(pinDir,OUTPUT);
pinMode(relaySet,OUTPUT);
pinMode(relayReset,OUTPUT);

/* ENTREES */

pinMode(photo1,INPUT);
pinMode(photo2,INPUT);
pinMode(photo3,INPUT);
pinMode(photo4,INPUT);
}
void loop()
{
/* Cette section se fait en boucle sans arrets. C’est le programme et ses actions */

int sensorValue1 = (((analogRead(photo1) - analogRead(photo2))) + ((analogRead(photo3) - (analogRead(photo4))))) / 2; // Comparaisons #1 entre 2 positions de photoresistance placer sur le panneau solaire.
int sensorValue2 = (((analogRead(photo1) - analogRead(photo3))) + ((analogRead(photo2) - (analogRead(photo4))))) / 2; // Comparaisons #2 entre 2 positions de photoresistance placer sur le panneau solaire.

if ((sensorValue1 > 50) || (sensorValue1 < -50)) // Si l’erreur de la comparaison #1 est plus grande que 50.
{
digitalWrite(relaySet,LOW); // Desativation du relais.
digitalWrite(relayReset,HIGH); // Desactivation du relais.
if (sensorValue1 > 50) // Si l’erreur est positive.
{
analogWrite(pinPwm,150); // Vitesse du moteur a 60%.
digitalWrite(pinDir,HIGH); // Mouvement a sens contraire des aiguilles d’une montre.
}
else // Si l’erreur est negative.
{
analogWrite(pinPwm,150); // Vitesse du moteur a 60%.
digitalWrite(pinDir,LOW); // Mouvement dans le sens des aiguilles d’une montre.
}
}
else if ((sensorValue2 > 50) || (sensorValue2 < -50)) // Si l’erreur de la comparaison #2 est plus grande que 50.
{
digitalWrite(relaySet,HIGH); // Activation du relais.
digitalWrite(relayReset,LOW); // Activation du relais.

if (sensorValue2 < -50) // Si l’erreur est negative.
{
digitalWrite(pinDir,LOW); // Mouvement dans le sens des aiguilles d’une montre.
analogWrite(pinPwm,100); // vitesse du moteur a 40%.
}
else
{
digitalWrite(pinDir,HIGH); // Mouvement a sens contraire des aiguilles d’une montre.
analogWrite(pinPwm,100); // Vitesse du moteur a 40%.
}
}
else // S’il n’y a pas d’erreur.
{
digitalWrite(relaySet,LOW); // Relais desactive
digitalWrite(relayReset,HIGH); // Relais desactive
analogWrite(pinPwm,0); // Vitesse Moteur 0%.
digitalWrite(pinDir,LOW); // Mouvement dans le sens des aiguilles d’une montre.
delay(1000); // Delais de Une seconde.
}
}

I need an other condition to distinct from the 2 movements.

OK. You are hereby granted permission to add one.

Did you have a question?

Well i was wondering if anyone had an idea of a condition i could add OR if my algorthym was simply wrong.

The code you posted does something. You didn't describe what it does.

You want the code to do something. It is not clear if it does what you want, or not.

It isn't clear what the new condition is supposed to control or anything about it, really.

Okay well i guess i will translate my description in english and repost it!

I don't think it's the French comments that are the problem. You haven't given sufficient information about what you think the problem is. In any language "I need an other condition to distinct from the 2 movements." doesn't convey much information.

What two movements are you talking about? Do you mean you need a third movement? What is actually happening? What do you want to happen?

...R

The code would be easier to understand if you used descriptive names instead of just numbering the inputs photo1, photo2, etc.

Your two calculations are identical so the results should be almost identical:

 int sensorValue1 = (((analogRead(photo1) - analogRead(photo2))) + ((analogRead(photo3) - (analogRead(photo4))))) / 2; 
 int sensorValue2 = (((analogRead(photo1) - analogRead(photo2))) + ((analogRead(photo3) - (analogRead(photo4))))) / 2;

I'm guessing you want one value to represent the left/right difference and the other to represent the up/down difference. This would be easier to do with the descriptive names:

 int verticalDifference = (((analogRead(photoTopLeft) - analogRead(photoBottomLeft))) + 
                                   ((analogRead(photoTopRight) - (analogRead(photoBottomRight))))) / 2; 
 int horizontalDifference = (((analogRead(photoTopLeft) - analogRead(photoTopRight))) + 
                                  ((analogRead(photoBottomLeft) - (analogRead(photoBottomRight))))) / 2;

Okay well this is the project:

A solar panel driven by 2 DC motors But only 1 drive wich is chosen by a relay (RelaySet,RelayReset) Wich is a twin coil relay. There are 4 photoresistances wich are placed at the 4 corners of the solar panel. In my programme you can see that my sensor value 1 and 2 are the difference of 2 photoresistances and then i take the average of the the 2.

My resistance are placed like this on the solar panel:

1 2

3 4

so you can see i do ((1-2) + (3-4))/2 for one angle and ((1-3) + (2-4))/2 for the other one so i can get an average value of the readings i get. That way i can have an error off 100 (from -50 to 50)

My problem is that if darkness comes on my photo 1 it will change Both sensor value and will take the first movement in concideration but not the other one.

johnwasser: ``` The code would be easier to understand if you used descriptive names instead of just numbering the inputs photo1, photo2, etc.

Your two calculations are identical so the results should be almost identical: [code] int sensorValue1 = (((analogRead(photo1) - analogRead(photo2))) + ((analogRead(photo3) - (analogRead(photo4))))) / 2; int sensorValue2 = (((analogRead(photo1) - analogRead(photo2))) + ((analogRead(photo3) - (analogRead(photo4))))) / 2;




I'm guessing you want one value to represent the left/right difference and the other to represent the up/down difference. This would be easier to do with the descriptive names:


int verticalDifference = (((analogRead(photoTopLeft) - analogRead(photoBottomLeft))) +                                   ((analogRead(photoTopRight) - (analogRead(photoBottomRight))))) / 2; int horizontalDifference = (((analogRead(photoTopLeft) - analogRead(photoTopRight))) +                                   ((analogRead(photoBottomLeft) - (analogRead(photoBottomRight))))) / 2; ```

[/code]

Yes sorry thats a mistake of typing!!