Code - part2/2:
void screenUp(float POS) {
activateL9935EN();
float b;
//Initial opening: saved positon from internal EEPROM will be used. Other cases: standard logic calculating current and max position.
if (POS >= 0) b = POS; else b = POS_MAX-POS_CUR;
for (int a = 0; a < b; a++)
{
stepUp();
}
screenPark();
}
void screenDown(void) {
activateL9935EN();
int b = POS_CUR;
while (POS_CUR >= 1)
{
stepDown();
}
POS_CUR=0; //resetting current position
POS_CUR_HS=0; //resetting half step position
screenPark();
}
void stepUp(void) {
//full step mode processing for complete opening;
//POS_CUR direction 0 --> 1 --> 2 --> 3
/*States:
0 = CH A enabled, polarity=HIGH. CH B disabled.
1 = CH A disabled. CH B disabled, polarity=LOW.
2 = CH A enabled, polarity = LOW. CH B disabled.
1 = CH A disabled. CH B enabled, polarity=HIGH.
*/
if(POS_CUR <= (POS_MAX - 1)) {
setSPIValue(15); //00001111 CH A enabled, polarity=HIGH (Current=900mA). CH B disabled.
setSPIValue(56); //00111000 CH A disabled. CH B enabled, polarity=LOW (Current=900mA).
setSPIValue(6); //00000110 CH A enabled, polarity=LOW (Current=900mA). CH B disabled.
setSPIValue(57); //00111001 CH A disabled. CH B enabled, polarity=HIGH (Current=900mA).
POS_CUR += 1;
Serial.print("POS_CUR=");Serial.println(POS_CUR);
}
}
void stepDown(void) {
//full step mode processing for complete closing;
//POS_CUR direction 0 --> 3 --> 2 --> 1
if(POS_CUR > 0) {
setSPIValue(15); //00001111 CH A enabled, polarity=HIGH (Current=900mA). CH B disabled.
setSPIValue(57); //00111001 CH A disabled. CH B enabled, polarity=HIGH (Current=900mA).
setSPIValue(7); //00000111 CH A enabled, polarity=LOW (Current=900mA). CH B disabled.
setSPIValue(48); //00110000 CH A disabled. CH B enabled, polarity=LOW (Current=900mA).
POS_CUR -= 1;
Serial.print("POS_CUR=");Serial.println(POS_CUR);
}
}
void screenPark(void) {
/*Parking allows manual movement of screen by hand (counter is not working in this case).
Stepper motor current is set to HL (internally sensed).
HH will not fix monitor at all as current is 0.
HL and LL with 550mA and 900mA current will hold monitor and will not allow manual movement.
*/
setSPIValue(47); //00101111 CH A enabled, polarity=HIGH. CH B disabled (Current 60mA).
delay(DELAY_S);
}
void stepUpHalf(void) {
/*Half steps can be used here by using middle states (currently not used) - step 1.2, step 2.2, step 3.2, step 4.2.
In this case both channels are enabled, use current max 550mA (avoid higher current 900mA, overheating risk exists).
*/
if(POS_CUR <= (POS_MAX - POS_STEP_HS) && POS_CUR >= POS_STEP_MIN) {
//POS_CUR_HS direction 0 --> 1 --> 2 --> 3
if(POS_CUR_HS == 0) {
setSPIValue(31); //00011111 CH A enabled, polarity=HIGH (Current=550mA). CH B disabled. --current step
setSPIValue(58); //00111010 CH A disabled. CH B enabled, polarity=LOW. (Current=550mA). --next step
setSPIValue(60); //00111100 CH A disabled. CH B enabled, polarity=LOW (Current=60mA). --park
Serial.print("STEP1");
}
if(POS_CUR_HS == 1) {
setSPIValue(58); //00111010 CH A disabled. CH B enabled, polarity=LOW (Current=550mA). --current step
setSPIValue(22); //00010110 CH A enabled, polarity=LOW (Current=550mA). CH B disabled. --next step
setSPIValue(38); //00100110 CH A enabled, polarity=LOW (Current=60mA). CH B disabled. --park
Serial.print("STEP2");
}
if(POS_CUR_HS == 2) {
setSPIValue(22); //00010110 CH A enabled, polarity=LOW (Current=550mA). CH B disabled. --current step
setSPIValue(51); //00110011 CH A disabled. CH B enabled, polarity=HIGH (Current=550mA). --next step
setSPIValue(53); //00110101 CH A disabled. CH B enabled, polarity=HIGH (Current=60mA). --park
Serial.print("STEP3");
}
if(POS_CUR_HS == 3) {
setSPIValue(51); //00110011 CH A disabled. CH B enabled, polarity=HIGH (Current=550mA). --current step
setSPIValue(31); //00011111 CH A enabled (Current=550mA), polarity=HIGH. CH B disabled. --next step
setSPIValue(47); //00101111 CH A enabled (Current=60mA), polarity=HIGH. CH B disabled. --park
Serial.print("STEP4");
}
if(POS_CUR_HS == 3) POS_CUR_HS=0; else POS_CUR_HS +=1;
Serial.print(" POS_CUR_HS=");Serial.print(POS_CUR_HS);
POS_CUR += POS_STEP_HS;
Serial.print(" POS_CUR=");Serial.println(POS_CUR);
delay(DELAY_L);
}
}
void stepDownHalf(void) {
/*Half steps can be used here by using middle states (currently not used) - step 1.2, step 2.2, step 3.2, step 4.2.
In this case both channels are enabled, use current max 550mA (avoid 900mA current, overheating risk exists).
*/
if(POS_CUR > POS_STEP_MIN) {
//POS_CUR_HS direction 0 --> 3 --> 2 --> 1
if(POS_CUR_HS == 0) {
setSPIValue(31); //00011111 CH A enabled, polarity=HIGH (Current=550mA). CH B disabled. --current step
setSPIValue(59); //00111011 CH A disabled. CH B enabled, polarity=HIGH (Current=550mA). --next step
setSPIValue(61); //00111101 CH A disabled. CH B enabled, polarity=HIGH (Current=60mA). --park
Serial.print("STEP1");
}
if(POS_CUR_HS == 3) {
setSPIValue(59); //00111011 CH A disabled. CH B enabled, polarity=HIGH (Current=550mA). --current step
setSPIValue(23); //00010111 CH A enabled, polarity=LOW. CH B disabled (Current=550mA). --next step
setSPIValue(39); //00100111 CH A enabled, polarity=LOW. CH B disabled (Current=60mA). --park
Serial.print("STEP2");
}
if(POS_CUR_HS == 2) {
setSPIValue(23); //00010111 CH A enabled, polarity=LOW. CH B disabled (Current=550mA). --current step
setSPIValue(58); //00111010 CH A disabled. CH B enabled, polarity=LOW (Current=550mA). --next step
setSPIValue(60); //00111100 CH A disabled. CH B enabled, polarity=LOW (Current=60mA). --park
Serial.print("STEP3");
}
if(POS_CUR_HS == 1) {
setSPIValue(58); //00111010 CH A disabled. CH B enabled, polarity=LOW (Current=550mA). --current step
setSPIValue(31); //00011111 CH A enabled, polarity=HIGH (Current=550mA). CH B disabled. --next step
setSPIValue(47); //00101111 CH A enabled, polarity=HIGH (Current=60mA). CH B disabled. --park
Serial.print("STEP4");
}
if(POS_CUR_HS == 0) POS_CUR_HS=3; else POS_CUR_HS -=1;
Serial.print(" POS_CUR_HS=");Serial.print(POS_CUR_HS);
POS_CUR -= POS_STEP_HS;
Serial.print(" POS_CUR=");Serial.println(POS_CUR);
delay(DELAY_L);
}
}
void readPOS_CUR(int ADDR) {
//Reads a byte from the EEPROM. Locations that have never been written to have the value of 255.
//Serial.println("Reading current position from internal EEPROM!");
POS_CUR_SAVED = EEPROM.read(ADDR);
Serial.print("Reading DONE! Current position: ");Serial.println(POS_CUR_SAVED);
}
void writePOS_CUR(int ADDR, int VAL) {
//Write a byte to the EEPROM. The value is written only if differs from the one already saved at the same address.
//Serial.print("Writing current positon to internal EEPROM: "); Serial.println(POS_CUR);
EEPROM.update(ADDR, VAL);
Serial.println("Writing DONE!");
}