Hi All
Thank you all for helping so far.
Let me give more info on my project.
I use a winch to open and close a window in increments at a time depending on the temperature.
There are two limit switches one at the top and one at the bottom called TopWindowSPin and BottomWindowSPin. The limit switches is pulled high to 5V with a pull up resistor and when one of them are trigged the go low to ground.
I added code to check if one of the switches gets trigged unexpectedly but that does not happen.
At start up of the code the Arduino measures the time it takes to open and close the window, the time is the divided by the amount of times I want the winch to move all the way in one direction.
The winch will keep moving Up until TopWindowSPin is LOW and keep moving down until BottomWindowSPin is LOW.
Here is the other code as requested:
Example for using the function:
int Move_Flag, WindowSize_Flag = 1;
unsigned long MiliMoveUp, MiliMoveDown = 0;
int CU = 4; //CU = Curtian Unit it is the No of units to divide curtain into
void setup(void)
{
Serial.begin(57600);
Move_Flag = 1;
WindowSize_Flag = 1;
Serial.println("GetWindowSize ");
GetWindowSize();
}
void loop(void)
{
if( “to cold” ) //
{
MoveWindow("UP", MiliMoveUp);
}
if( “to hot” ) //
{
MoveWindow("DOWN", MiliMoveDown);
}
wait(); //use RTC to wait 5min and check again code here
}
ControlWinch:
//_______________ControlWinch Begin________________________
void ControlWinch(char *choice)
{
if( choice == "UP" )
{
digitalWrite(WinchDownPin, LOW ); //Motor switch )
digitalWrite(WinchUpPin, HIGH ); //Motor switch
}
else if( choice == "DOWN" )
{
digitalWrite(WinchUpPin, LOW ); //Motor switch )
digitalWrite(WinchDownPin, HIGH ); //Motor switch
}
else if( choice == "STOP" )
{
digitalWrite(WinchUpPin, LOW ); //Motor switch )
digitalWrite(WinchDownPin, LOW ); //Motor switch
delay(100); //Added on 2011/08/26
}
else
{
Serial.println("Winch wrong choice");
digitalWrite(WinchUpPin, LOW ); //Motor switch )
digitalWrite(WinchDownPin, LOW ); //Motor switch
delay(100);
delay(100); //Added on 2011/08/26
}
}
//_______________ControlWinch End________________________
MoveWindow full code:
//_______________MoveWindow Begin________________________
void MoveWindow(char *choice, unsigned long tMiliDelay)
{
StopWatch MySW;
char FloatBuffer[10];
PString strFloat(FloatBuffer, sizeof(FloatBuffer));
strFloat.begin();
unsigned long tTimerVal = 0;
float tAjustVal, tWinAVal = 0;
if( (choice == "UP") && (Move_Flag == 1) ) //2011/03/30
{
Serial.println("Winch UP Begin");
DistCounter ++;
CollectData("Winch UP");
Serial.println( strM );
WriteData();
tWinAVal = WUAM;
tAjustVal = (tWinAVal/100)+1;
if( tAjustVal == 0 )
{
tAjustVal = 1;
}
Serial.print("tAjustVal UP = " );
Serial.println( tAjustVal );
Serial.print("tMiliDelay UP = " );
Serial.println( tMiliDelay );
MySW.reset();
MySW.start();
ControlWinch("UP"); //Extra
delay(200); // This delay seem to fix the problem, Why??
//while( (digitalRead(TopWindowSPin) == HIGH) && ( tTimerVal < (tMiliDelay * tAjustVal) ) ) //2011/03/25
while( (digitalRead(TopWindowSPin) == HIGH) && ( tTimerVal < (tMiliDelay) ) ) //2011/08/28 testing
{
ControlWinch("UP");
tTimerVal = MySW.value();
}
ControlWinch("STOP");
MySW.stop();
if( digitalRead(TopWindowSPin) == LOW ) //added on 2011/08/27
{
Serial.println("TopWindowSPin trigered");
CollectData("TopWindowSPin");
Serial.println( strM );
WriteData();
}
else
{
Serial.println("Timer end reached");
strFloat.print(tTimerVal);
CollectData(FloatBuffer);
Serial.println( strM );
WriteData();
}
Serial.println("Winch UP End");
Serial.print("tTimerVal UP = " );
Serial.println( tTimerVal );
}
else if( (choice == "DOWN") && (Move_Flag == 1) ) //2011/03/30
{
Serial.println("Winch DOWN Begin");
DistCounter -- ;
CollectData("Winch DOWN");
Serial.println( strM );
WriteData();
tWinAVal = WDAM;
tAjustVal = (tWinAVal/100)+1;
if( tAjustVal == 0 )
{
tAjustVal = 1;
}
Serial.print("tAjustVal down" );
Serial.println( tAjustVal );
Serial.print("tMiliDelay UP = " );
Serial.println( tMiliDelay );
MySW.reset();
MySW.start();
while( (digitalRead(BottomWindowSPin) == HIGH) && ( tTimerVal < (tMiliDelay * tAjustVal) ) ) //2011/03/25 (tMiliDelay * 1.02) to move a bit more than time /CU
{
ControlWinch("DOWN");
tTimerVal = MySW.value();
}
ControlWinch("STOP");
MySW.stop();
if( digitalRead(BottomWindowSPin) == LOW ) //added on 2011/08/27
{
Serial.println("BottomWindowSPin trigered");
CollectData("BottomWindowSPin");
Serial.println( strM );
WriteData();
}
else
{
Serial.println("Timer end reached");
strFloat.print(tTimerVal);
CollectData(FloatBuffer);
Serial.println( strM );
WriteData();
}
Serial.println("Winch Down End");
Serial.print("tTimerVal Down = " );
Serial.println( tTimerVal );
}
else if( choice == "STOP" )
{
Serial.println("Winch STOP");
ControlWinch("STOP");
MySW.stop();
}
else
{
ControlWinch("STOP");
}
ResetDistCounterIfAtMagnets();
CheckCurtainMovementAndSirenIfNeeded();
}
//_______________MoveWindow End________________________
GetWindowSize function:
void GetWindowSize() //window stop must be depending on temp if cold at top, if hot at bottom
{
StopWatch MySW;
Serial.println("up 1 begin = ");
MySW.start();
while( (digitalRead(TopWindowSPin) != LOW) && (WindowSize_Flag == 1) ) // Move up and save setting
{
ControlWinch("UP");
}
MySW.stop();
ControlWinch("STOP");
if( digitalRead(TopWindowSPin) == LOW ) //added on 2011/08/27
{
Serial.println("TopWindowSPin trigered");
}
else
{
Serial.println("Time Out");
}
Serial.print("Up 1 end = ");
Serial.println(MySW.value());
MiliMoveUp = (MySW.value() / CU);
Serial.println(MiliMoveUp);
MySW.reset();
Serial.print("Window TOP stop = ");
Serial.print("CU = ");
Serial.println(CU);
//___________LFC Move window down Begin___________________________
delay(500); //Dely before direction change
Serial.println("down 2 begin = ");
MySW.reset();
MySW.start();
while( (digitalRead(BottomWindowSPin) == HIGH) && (WindowSize_Flag == 1) ) //LFC Move window down
{
ControlWinch("DOWN");
}
ControlWinch("STOP");
MySW.stop();
if( digitalRead(BottomWindowSPin) == LOW )
{
Serial.println("BottomWindowSPin trigered");
}
else
{
Serial.println("Time Out");
}
Serial.println("Window UP and stop ");
//___________LFC Move window down End___________________________
ControlWinch("STOP");
Serial.print("down 2 end = ");
Serial.println(MySW.value());
MiliMoveDown = MySW.value() / CU;
Serial.println(MiliMoveDown);
}
//_______________GetWindowSize End________________________
Here is a lot of code now but I hope this answers your questions.
Thank you so far
Regards
Luan 