// LCDenKeyPad_Reg_LED's_Stepper //Keypad #include // Stepper #include // TFTLCD #include MCUFRIEND_kbv tft; // Stepper Motor - Model = 28BYJ-48 (Get from SPEC Sheet) int stepsPerRevolution=2048; //shed numbers 0 1 2 3 4 5 6 7 8 9 int shedLocations[] = {0, 683, 512, 341, 171, 1877,1707,1536,1365,1024}; int currentLocation; // current location of bridge <------- added int desiredLocation; // desired location of bridge <--------added int motSpeed=10; int dt=500; int num; // Shed number selected int count = 5; Stepper StepMot(stepsPerRevolution, 16,18,17,19); // LED Pins int BY_1 = 50; //Bridge Yellow 1 int BY_2 = 51; //Bridge Yellow 2 int BG_1 = 52; //Bridge Green 1 int BG_2 = 53; //Bridge Green 1 // IRA Sensors int IRSensor1=48; //Sensor 1 is setup on pin 50 int IRSensor2=49; //Sensor 2 is setup on pin 52 int IRAval1; int IRAval2; int IRState = 0; // =1 if both IRAvals are 0 and =2 if both IRAvals are non-zero // Keypad const byte ROWS = 4; const byte COLS = 4; char Keys[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte rowPins[ROWS] = {22, 24, 26, 28}; byte colPins[COLS] = {30, 32, 34, 36}; Keypad kpd = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS ); // TFTLCD float LCDRefreshTimer = 0; // Define Colours #define BLUE 0x001F #define WHITE 0xFFFF #define RED 0xF882 #define BLACK 0x0000 void setup() { Serial.begin(9600); //Step Motor StepMot.setSpeed(motSpeed); tft.reset(); // IRA pinMode(IRSensor1, INPUT); pinMode(IRSensor2, INPUT); // LCD uint16_t id = tft.readID(); tft.begin(id); tft.setRotation(1); tft.fillScreen(WHITE); tft.setTextColor(BLUE, WHITE); // LED pinMode (BY_1, OUTPUT); pinMode (BY_2, OUTPUT); pinMode (BG_1, OUTPUT); pinMode (BG_2, OUTPUT); // // Reset the screen ready for next loop // displayInfo("To select a Locomotive","Press the loco sched no", BLACK ); currentLocation = 0; //<------------------- added } void loop() { // // This used to be the module GetNumber() // The active code has been moved here to avoid an extra sub // char key = kpd.getKey(); // Read keypad if ( key!=NO_KEY && ( '0'<=key && key<='9' )) { // If a valid numeric key num = key-'0'; // Convert char to shed number moveToShed(num); // rotate to shed number showMainScreen(); // Re-display main screen } // // Read and process sensors // IRAval1 = digitalRead(IRSensor1); IRAval2 = digitalRead(IRSensor2); if ((IRAval1==0)&&(IRAval2==0) && IRState!=1) { IRState = 1; digitalWrite(BY_1, HIGH); digitalWrite(BY_2, HIGH); digitalWrite(BG_1, LOW); digitalWrite(BG_2, LOW); Serial.println(2222); Serial.println(IRAval1); Serial.println(2222); Serial.println(IRAval2); displayInfo("Bridge Occupied - Remove ","Locomotive from BRIDGE", RED ); delay (2000); showMainScreen(); // Re-display main screen } if ((IRAval1>=1)&&(IRAval2>=1) && IRState!=2) { IRState = 2; digitalWrite(BY_1, LOW); digitalWrite(BY_2, LOW); digitalWrite(BG_1, HIGH); digitalWrite(BG_2, HIGH); Serial.println(1111); Serial.println(IRAval1); Serial.println(1111); Serial.println(IRAval2); displayInfo("Bridge Clean","", BLACK ); delay (2000); showMainScreen(); // Re-display main screen } } void showMainScreen() { tft.setTextSize(2); tft.setCursor(1, 20); tft.fillScreen(WHITE); displayInfo("To select a Locomotive","Press the loco sched no", BLACK ); } //**************************************************************************************************** // // Blink the BY LEDs times // void blinkBY( int count ) { bool LEDState = HIGH; for (int i=0;i<2*count;i++) { digitalWrite(BY_1, LEDState); digitalWrite(BY_2, LEDState); delay(500); LEDState = !LEDState; } } // // Display info on TFT screen // Note message length limited to 25 characters // void displayInfo( String m1, String m2, long colour ) { String line; String delim = "-------------------------"; String empty = " "; int mSize; // // Set to required colour, // write delimiter, // write first line (centered in 25 bytes), // write second line (centered in 25 bytes), // write delimiter. // tft.setTextSize(2); tft.setTextColor(colour); // Set to desired colour tft.println(delim); // Print top delimiter tft.println( empty.substring(0,(25-m1.length())/2)+m1); if (m2.length()>0) tft.println( empty.substring(0,(25-m2.length())/2)+m2); tft.println(delim); // Print bottom delimiter } // Confirm a command // bool confirmCommand() { int theKey = NO_KEY; /// Nothing read yet // // Prompt user for confirmation and read response // displayInfo("Press C to confirm","",RED); while (theKey==NO_KEY) theKey = kpd.getKey(); // keep reading until user responds // return (theKey=='c' || theKey=='C'); // Confirmed if user entered cor C } // // Calculate stepcount for specified angle // int calcBridgeSteps( int shedNumber ) { int Direction; int stepCount; currentLocation = shedLocations[shedNumber]; //*** compute number and direction of steps * stepCount = abs(desiredLocation-currentLocation); if (desiredLocation <= currentLocation){ Direction = 0; }else{ Direction = 1; } if (abs(desiredLocation-currentLocation)>1023){ stepCount = 2048 - stepCount; if (Direction==0){ Direction = 1; }else{ Direction = 0; } } if (Direction == 0){ stepCount = -stepCount; } currentLocation = desiredLocation; // update turntable position return stepCount; } // // Turn to a specific shed number at specified stepCount // void moveToShed(int shedNo) { String shed = "Loco sched "+String(shedNo); tft.setTextSize(2); tft.setCursor(10, 50); Serial.print(shedNo); delay (500); tft.fillScreen(WHITE); displayInfo("Bridge move to",shed+" selected",BLACK); delay (500); // // If not confirmed, then exit now // if ( !confirmCommand() ) return; // // Execute the command // tft.fillScreen(WHITE); tft.setCursor(10, 50); blinkBY(3); displayInfo("Please wait - the","bridge is moving",RED); blinkBY(3); // * move to shed num ***** int stepCount = calcBridgeSteps(shedNo); // <--- added StepMot.step(stepCount); // blinkBY(3); delay (1000); tft.setCursor(10, 50); tft.fillScreen(WHITE); displayInfo("Bridge turned","to "+shed,BLACK); delay (2000); // showMainScreen(); // Re-display main screenshowMainScreen(); // Re-display main screen return; }