Perfect, I simulated this code and it worked very well, I'll try it on the hardware and let you know the result.Something like this then? Note how each cycle (operation) are broken into individual functions which are called in when buttons are pushed. The button loop is simple and compact. Breaking code into functional blocks is and important step to writing clear, workable code.
Code://============= DISHWASHER CONTROL ===================================== // ..add info about the program here.. unsigned char PORTBimage; // IO definitions with correct syntax #define ButtonA RD1_bit #define ButtonB RD2_bit #define PRESSED 0 // 0 = pressed, 1 = not pressed #define NOT_PRESSED 1 //====================== Operation No. 1 ============================== void run_OperationA(void) { //RB1_bit=1; PORTBimage.B1 = 1; // Filling water PORTB = PORTBimage; // write the new pattern to the port Delay_ms(32000); PORTBimage.B1 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB2_bit=1; PORTBimage.B2 = 1; // Washing PORTB = PORTBimage; // write the new pattern to the port Delay_ms(240000); PORTBimage.B2 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB3_bit=1; PORTBimage.B3 = 1; // Empty water PORTB = PORTBimage; // write the new pattern to the port Delay_ms(30000); PORTBimage.B3 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(60000); } //===================== Operation No. 2 ========================== void run_OperationB(void) { //RB1_bit=1; PORTBimage.B1 = 1; // Filling water PORTB = PORTBimage; // write the new pattern to the port Delay_ms(32000); PORTBimage.B1 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB5_bit=1; PORTBimage.B5 = 1; // Heating water PORTB = PORTBimage; // write the new pattern to the port Delay_ms(600000); PORTBimage.B5 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB2_bit=1; PORTBimage.B2 = 1; // Washing PORTB = PORTBimage; // write the new pattern to the port Delay_ms(420000); PORTBimage.B2 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB3_bit=1; PORTBimage.B3 = 1; // Empty water PORTB = PORTBimage; // write the new pattern to the port Delay_ms(30000); PORTBimage.B3 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB1_bit=1; PORTBimage.B1 = 1; // Filling water PORTB = PORTBimage; // write the new pattern to the port Delay_ms(32000); PORTBimage.B1 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB4_bit=1; PORTBimage.B4 = 1; // Polishing oil PORTB = PORTBimage; // write the new pattern to the port Delay_ms(3000); PORTBimage.B4 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB5_bit=1; PORTBimage.B5 = 1; // Heating water PORTB = PORTBimage; // write the new pattern to the port Delay_ms(600000); PORTBimage.B5 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB2_bit=1; PORTBimage.B2 = 1; // Washing PORTB = PORTBimage; // write the new pattern to the port Delay_ms(420000); PORTBimage.B2 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB5_bit=1; PORTBimage.B5 = 1; // Heating water PORTB = PORTBimage; // write the new pattern to the port Delay_ms(300000); PORTBimage.B5 = 0; // Wait PORTB = PORTBimage; // write the new pattern to the port Delay_ms(2000); //RB3_bit=1; PORTBimage.B3 = 1; // Empty water PORTB = PORTBimage; // write the new pattern to the port Delay_ms(30000); PORTBimage.B3 = 0; // Wait PORTB = PORTBimage; // write it to the port Delay_ms(2000); //RB7_bit=1; PORTBimage.B7 = 1; //Finish [Green LED is ON] PORTB = PORTBimage; } //======================= PROGRAM STARTS HERE ========================== void main() { TRISD = 0b00000110; //Configure RD1, RD2 as input PORTD = 0b00000110; // Unused PORTD are output 0 TRISB=0; PORTB=0; PORTBimage = 0; PORTB = PORTBimage; //----------------- START OPERATION ON BUTTONS ----------------------- // Scan the 2 buttons // 1) Wait for no buttons pressed (idle) // 2) Scan the 2 buttons and start cycle when one pushed by calling the // cycle function // 3) 'continue' bypasses next button check, after cycle complete, // returns to top of while loop - step 1 // Note: this does NOT completely do everything needed to reliably // do the button input but is a start. // Buttons use the IO definitions above to make more readable code and // make changes easier while(1){ // scan button loop while((ButtonA == PRESSED) || (ButtonB == PRESSED)) ; // wait for no buttons if(ButtonA == PRESSED){ // call OperationA iff button A pressed run_OperationA(); continue; // cycle complete, don't check the other button yet } if(ButtonB == PRESSED){ // call OperationB iff button B pressed run_OperationB(); continue; // really not needed unless more buttons are added } }// while scan buttons }//main
Many thanks for your cooperation and for ErnieM and everybody gave an advice, highly appreciated.