Guys,
Please find errors and help me improvise my code. I have tried to comment to the best extent. However, I putting up the project description below.
I am using 2 LDRs to sense the first axis (East to West). I had the following issue. The LDRs does not give equal outputs and hence I had to subtract 50 to ensure that the outputs are close to each other (line 94 of the code). I am certain that this is NOT the best approach and might not be reliable.
The system uses RESET system to bring back the panel to East after sunset. I have planned to put across two Limit switches, one at East and other at West. As soon at the West limit switch is triggered, a relay (DPDT), is switched ON. The first NO of the relay is used as holding contact and second goes to Arduino. Similarly the NC of the East Limit switch is in series with the NO of the West limit switch and the relay.
/*
MADHAV
04122015 0954
Ver 01
MAJOR NOTE: THIS VERSION USES SMOOTHING
INCORPORATING AUTO SET / RESET FACILITY
* Read Analog Voltage of East_Sens and West_Sens on pins A0 and A1
LCD Conn:
* LCD is from DFROBOT
* LCD RS pin to digital pin D8
* LCD Enable pin to digital D9
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
// Variables
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
int diff1 = 0; // difference between east and west sensor inputs
int diff2 = 0; // difference betweeen west and east sensor inputs
int East_drive = 10; // CW output to motor H bridge circuit
int West_drive = 11; // CCW output to motor H bridge circuit
int EastLS = 2; // Limitswitch on east end of travel for stopping the end
of travel from
// west to east and be ready for next day.
int WestLS = 3; // Limit swtich on west end of travel to sense end of day
// a relay (DPDT) will switch on the motor in CW (East) direction and is triggered by
// West Limit switch for ON. The other NO will work as holding contact and shall be
// switched OFF by NC of East end Limit switch.
int tol = 5; // dead band between East sensor and west sensor
int ton = 500; // motor's time ON duration.
int var = 0; // for future PWM output
// where we have the PWM (to) in proportion to error
// larger error = larger ton
// this PWM signal will to to fifth Transistor (MOSFET later) to be placed
// below the lower two transistors of the H bridge.
int PWM = 13;
void setup()
{Serial.begin(9600);
Serial.println("start");
lcd.clear();
lcd.setCursor (0,0);
lcd.print("SOLAR TRACKER");
lcd.setCursor (0,2);
lcd.print("Ver.4 Madhav");
delay (1000);
lcd.begin (16, 2);
lcd.clear();
pinMode(10, OUTPUT); // East_drive motor input to H bridge
pinMode(11, OUTPUT); // West_drive motor input to H bridge
pinMode(13, OUTPUT);
pinMode(2, INPUT); // Limitswitch on East side to stop panel
pinMode(3, INPUT); // Limitswitch on West side to sense sun set and start to East
}
void loop()
{
int East_Sens = analogRead (A5); // read the East_Sens on analog pin A0:
int West_Sens = analogRead (A4); // read the West_Sens on analog pin A1:
East_Sens = map (East_Sens, 0, 1023, 0, 255); // restrict the value between 0 and 255
West_Sens = map (West_Sens, 0, 1023, 0, 200); // restrict the West_Sensor between 0 and 255
diff1 = East_Sens - West_Sens;
diff2 = West_Sens - East_Sens;
if(abs (diff1) <= tol)
{
digitalWrite (10 & 11, LOW);
}
else
{
if( (diff1 > tol))
{
digitalWrite (East_drive, LOW);
digitalWrite (East_drive, HIGH);
delay (ton);
digitalWrite (East_drive, LOW);
}
if( (diff2 > tol) )
{
digitalWrite (West_drive, LOW);
digitalWrite (West_drive, HIGH);
delay (ton);
digitalWrite (West_drive, LOW);
}
}
delay (10);
digitalRead (EastLS);
digitalRead (WestLS);
if (WestLS == HIGH && East_drive == LOW) // Sunset is detected as West side limit swtich goes high, this is one end of the tracking
// and the panel starts back journey back to East through East drive and until the East side
// limit switch goes high.But the limit switch immediately bounces to close condition once the
// journey starts. To prevent that a holding contact has to be provided. This is done by
// external hardwiring.
{
int Var=255; // go back from West to East at full speed.
digitalWrite (East_drive, HIGH );
}
if (WestLS == HIGH && East_drive == HIGH) // Sunset is detected as West side limit swtich goes high, this is one end of the tracking
// and the panel starts back journey back to East through East drive and until the East side
// limit switch goes high.But the limit switch immediately bounces to close condition once the
// journey starts. To prevent that a holding contact has to be provided. This is done by
// external hardwiring.
{
digitalWrite (East_drive, LOW );
}
Serial.print ("East_Sens =");
Serial.println (East_Sens);
Serial.print ("West_Sens = ");
Serial.println (West_Sens);
Serial.print ("diff1 = ");
Serial.println (diff1);
Serial.print ("diff2 = ");
Serial.println (diff2);
delay(750);
}