
This has increased comments and a readme
Dependencies: mbed mbed-rtos TextLCD
main.cpp@12:d9c133b360b0, 2019-02-05 (annotated)
- Committer:
- thomasmorris
- Date:
- Tue Feb 05 16:20:32 2019 +0000
- Revision:
- 12:d9c133b360b0
- Parent:
- 11:0309bef74ba8
- Child:
- 13:c681f340909b
Working LCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
mbed_official | 11:0309bef74ba8 | 2 | #include "rtos.h" |
thomasmorris | 12:d9c133b360b0 | 3 | #include "TextLCD.h" |
thomasmorris | 12:d9c133b360b0 | 4 | #include "iostream" |
thomasmorris | 12:d9c133b360b0 | 5 | #include "SETUP.hpp" |
thomasmorris | 12:d9c133b360b0 | 6 | #include "stdio.h" |
thomasmorris | 12:d9c133b360b0 | 7 | #include "string.h" |
thomasmorris | 12:d9c133b360b0 | 8 | |
thomasmorris | 12:d9c133b360b0 | 9 | //Digital Outputs |
emilmont | 1:491820ee784d | 10 | DigitalOut led1(LED1); |
emilmont | 1:491820ee784d | 11 | DigitalOut led2(LED2); |
thomasmorris | 12:d9c133b360b0 | 12 | |
thomasmorris | 12:d9c133b360b0 | 13 | //Thread Setups |
thomasmorris | 12:d9c133b360b0 | 14 | Thread lcd_thread; |
thomasmorris | 12:d9c133b360b0 | 15 | Thread led_thread; |
thomasmorris | 12:d9c133b360b0 | 16 | TextLCD lcd(D0,D1,D4,D5,D6,D7); // rs, e, d4-d7 |
thomasmorris | 12:d9c133b360b0 | 17 | Serial pc(USBTX, USBRX); //Define serial namespace so the serial comms can be printed to |
thomasmorris | 12:d9c133b360b0 | 18 | |
thomasmorris | 12:d9c133b360b0 | 19 | |
thomasmorris | 12:d9c133b360b0 | 20 | //Switch toggling to bools for readability |
thomasmorris | 12:d9c133b360b0 | 21 | InterruptIn button_up(A0); // UP BUTTON |
thomasmorris | 12:d9c133b360b0 | 22 | InterruptIn button_down(A1); // These setup the button interupts |
thomasmorris | 12:d9c133b360b0 | 23 | InterruptIn button_start(A2); // START / STOP BUTTON |
thomasmorris | 12:d9c133b360b0 | 24 | InterruptIn button_funct(A3);// Random pin CHANGE FOR FUNCTION BUTTON |
thomasmorris | 12:d9c133b360b0 | 25 | InterruptIn button_select(A4);// Random pin CHANGE FOR SELECT BUTTON |
thomasmorris | 12:d9c133b360b0 | 26 | |
thomasmorris | 12:d9c133b360b0 | 27 | PwmOut Tendon_Power(PE_8); |
thomasmorris | 12:d9c133b360b0 | 28 | |
thomasmorris | 12:d9c133b360b0 | 29 | //VARIABLE DEFINITIONS |
thomasmorris | 12:d9c133b360b0 | 30 | int No_Of_Rotations; |
thomasmorris | 12:d9c133b360b0 | 31 | int Function; |
thomasmorris | 12:d9c133b360b0 | 32 | bool Twist_Go; |
thomasmorris | 12:d9c133b360b0 | 33 | bool Anneal_Go; |
thomasmorris | 12:d9c133b360b0 | 34 | bool Test_Go; |
thomasmorris | 12:d9c133b360b0 | 35 | bool Select; |
thomasmorris | 12:d9c133b360b0 | 36 | |
thomasmorris | 12:d9c133b360b0 | 37 | int turns_done; |
thomasmorris | 12:d9c133b360b0 | 38 | int turns_todo; |
thomasmorris | 12:d9c133b360b0 | 39 | int Loop; |
thomasmorris | 12:d9c133b360b0 | 40 | int Wait_Time;//IN SECONDS |
thomasmorris | 12:d9c133b360b0 | 41 | |
thomasmorris | 12:d9c133b360b0 | 42 | int Duty_Cycle; |
thomasmorris | 12:d9c133b360b0 | 43 | int Power_Time; |
thomasmorris | 12:d9c133b360b0 | 44 | |
thomasmorris | 12:d9c133b360b0 | 45 | |
thomasmorris | 12:d9c133b360b0 | 46 | //Thread Functions |
thomasmorris | 12:d9c133b360b0 | 47 | void LCD_thread(){ |
thomasmorris | 12:d9c133b360b0 | 48 | while(1){ |
thomasmorris | 12:d9c133b360b0 | 49 | //lcd.printf("Test\n"); |
thomasmorris | 12:d9c133b360b0 | 50 | lcd.cls(); |
thomasmorris | 12:d9c133b360b0 | 51 | if(Function == 0){ |
thomasmorris | 12:d9c133b360b0 | 52 | lcd.printf("Mode: Anneal\n"); |
thomasmorris | 12:d9c133b360b0 | 53 | lcd.printf("Loop:%d Wait:%d\n",Loop,Wait_Time); |
thomasmorris | 12:d9c133b360b0 | 54 | }else if(Function == 1){ |
thomasmorris | 12:d9c133b360b0 | 55 | lcd.printf("Mode: Test\n"); |
thomasmorris | 12:d9c133b360b0 | 56 | lcd.printf("Duty: %d Time: %d\n",Duty_Cycle,Power_Time); |
thomasmorris | 12:d9c133b360b0 | 57 | }else if(Function == 2){ |
thomasmorris | 12:d9c133b360b0 | 58 | |
thomasmorris | 12:d9c133b360b0 | 59 | lcd.printf("Mode: Turn\n"); |
thomasmorris | 12:d9c133b360b0 | 60 | lcd.printf("Done: %d ToDo: %d\n",turns_done,turns_todo); |
thomasmorris | 12:d9c133b360b0 | 61 | } |
thomasmorris | 12:d9c133b360b0 | 62 | Thread::wait(5000); |
thomasmorris | 12:d9c133b360b0 | 63 | lcd.cls(); |
thomasmorris | 12:d9c133b360b0 | 64 | Function = Function +1; |
thomasmorris | 12:d9c133b360b0 | 65 | if(Function >2) |
thomasmorris | 12:d9c133b360b0 | 66 | { |
thomasmorris | 12:d9c133b360b0 | 67 | Function = 0; |
thomasmorris | 12:d9c133b360b0 | 68 | } |
thomasmorris | 12:d9c133b360b0 | 69 | } |
thomasmorris | 12:d9c133b360b0 | 70 | } |
thomasmorris | 12:d9c133b360b0 | 71 | void LED_thread(){ |
thomasmorris | 12:d9c133b360b0 | 72 | while (1){ |
emilmont | 1:491820ee784d | 73 | led2 = !led2; |
mbed_official | 11:0309bef74ba8 | 74 | Thread::wait(1000); |
emilmont | 1:491820ee784d | 75 | } |
emilmont | 1:491820ee784d | 76 | } |
thomasmorris | 12:d9c133b360b0 | 77 | //Interrupt functions |
thomasmorris | 12:d9c133b360b0 | 78 | void up(){//Action if the up button is pressed increment |
thomasmorris | 12:d9c133b360b0 | 79 | if (Function == 0){ |
thomasmorris | 12:d9c133b360b0 | 80 | No_Of_Rotations = No_Of_Rotations + 1;//Increases Turn number |
thomasmorris | 12:d9c133b360b0 | 81 | }else if (Function == 1){ |
thomasmorris | 12:d9c133b360b0 | 82 | if (Select == false){ |
thomasmorris | 12:d9c133b360b0 | 83 | Loop = Loop + 1; //Increases Repetitions in Annealing |
thomasmorris | 12:d9c133b360b0 | 84 | }else if (Select == true){ |
thomasmorris | 12:d9c133b360b0 | 85 | Wait_Time = Wait_Time + 1; //Increases Wait time |
thomasmorris | 12:d9c133b360b0 | 86 | } |
thomasmorris | 12:d9c133b360b0 | 87 | }else if (Function == 2){ |
thomasmorris | 12:d9c133b360b0 | 88 | if (Select == false){ |
thomasmorris | 12:d9c133b360b0 | 89 | if (Duty_Cycle < 100){ |
thomasmorris | 12:d9c133b360b0 | 90 | Duty_Cycle = Duty_Cycle + 10; //Increases Testing Duty Cycle |
thomasmorris | 12:d9c133b360b0 | 91 | }}else if (Select == true){ |
thomasmorris | 12:d9c133b360b0 | 92 | Power_Time = Power_Time + 1; //Increases time on |
thomasmorris | 12:d9c133b360b0 | 93 | } |
thomasmorris | 12:d9c133b360b0 | 94 | } |
thomasmorris | 12:d9c133b360b0 | 95 | } |
thomasmorris | 12:d9c133b360b0 | 96 | void down(){ //Action if the down button is pressed decrement |
thomasmorris | 12:d9c133b360b0 | 97 | if (Function == 0){ |
thomasmorris | 12:d9c133b360b0 | 98 | No_Of_Rotations = No_Of_Rotations - 1; |
thomasmorris | 12:d9c133b360b0 | 99 | }else if (Function == 1){ |
thomasmorris | 12:d9c133b360b0 | 100 | if (Select == false){ |
thomasmorris | 12:d9c133b360b0 | 101 | if (Loop > 0){ |
thomasmorris | 12:d9c133b360b0 | 102 | Loop = Loop - 1; |
thomasmorris | 12:d9c133b360b0 | 103 | } |
thomasmorris | 12:d9c133b360b0 | 104 | }else if (Select == true){ |
thomasmorris | 12:d9c133b360b0 | 105 | if (Wait_Time > 0){ |
thomasmorris | 12:d9c133b360b0 | 106 | Wait_Time = Wait_Time - 1; |
thomasmorris | 12:d9c133b360b0 | 107 | } |
thomasmorris | 12:d9c133b360b0 | 108 | } |
thomasmorris | 12:d9c133b360b0 | 109 | }else if (Function == 2){ |
thomasmorris | 12:d9c133b360b0 | 110 | if (Select == false){ |
thomasmorris | 12:d9c133b360b0 | 111 | if (Duty_Cycle > 0){ |
thomasmorris | 12:d9c133b360b0 | 112 | Duty_Cycle = Duty_Cycle - 10; //Decreases duty |
thomasmorris | 12:d9c133b360b0 | 113 | }}else if (Select == true){ |
thomasmorris | 12:d9c133b360b0 | 114 | if (Power_Time > 0){ |
thomasmorris | 12:d9c133b360b0 | 115 | Power_Time = Power_Time - 1; //Decreases Time on |
thomasmorris | 12:d9c133b360b0 | 116 | } |
thomasmorris | 12:d9c133b360b0 | 117 | } |
thomasmorris | 12:d9c133b360b0 | 118 | } |
thomasmorris | 12:d9c133b360b0 | 119 | } |
thomasmorris | 12:d9c133b360b0 | 120 | void start_stop() { //Action if the Start/Stop button is pressed |
thomasmorris | 12:d9c133b360b0 | 121 | if (Function == 0)//Twist selected |
thomasmorris | 12:d9c133b360b0 | 122 | { |
thomasmorris | 12:d9c133b360b0 | 123 | Twist_Go = !Twist_Go;//toggle used for coiling and twisting |
thomasmorris | 12:d9c133b360b0 | 124 | }else if (Function == 1){ //Annealing selected |
thomasmorris | 12:d9c133b360b0 | 125 | Anneal_Go = !Anneal_Go;//toggle |
thomasmorris | 12:d9c133b360b0 | 126 | }else if (Function == 2){ //Testing selected |
thomasmorris | 12:d9c133b360b0 | 127 | Test_Go = !Test_Go;//toggle |
thomasmorris | 12:d9c133b360b0 | 128 | } |
thomasmorris | 12:d9c133b360b0 | 129 | } |
thomasmorris | 12:d9c133b360b0 | 130 | void Function_Selection() { //Action if the Function button is pressed |
thomasmorris | 12:d9c133b360b0 | 131 | if (Function < 2){ |
thomasmorris | 12:d9c133b360b0 | 132 | Function = Function + 1; |
thomasmorris | 12:d9c133b360b0 | 133 | }else{ |
thomasmorris | 12:d9c133b360b0 | 134 | Function = 0; |
emilmont | 1:491820ee784d | 135 | } |
emilmont | 1:491820ee784d | 136 | } |
thomasmorris | 12:d9c133b360b0 | 137 | void Selection() { //Action if the Select button is pressed |
thomasmorris | 12:d9c133b360b0 | 138 | Select = !Select; |
thomasmorris | 12:d9c133b360b0 | 139 | } |
thomasmorris | 12:d9c133b360b0 | 140 | int main() |
thomasmorris | 12:d9c133b360b0 | 141 | { |
thomasmorris | 12:d9c133b360b0 | 142 | //Interrupt setters |
thomasmorris | 12:d9c133b360b0 | 143 | button_up.rise(&up); //Sets up Up button |
thomasmorris | 12:d9c133b360b0 | 144 | button_down.rise(&down); //Sets up Down Button |
thomasmorris | 12:d9c133b360b0 | 145 | button_start.rise(&start_stop); //Sets up Start/Stop Button |
thomasmorris | 12:d9c133b360b0 | 146 | button_funct.rise(&Function_Selection); //Sets up Function Button |
thomasmorris | 12:d9c133b360b0 | 147 | button_select.rise(&Selection); //Sets up Select Button |
thomasmorris | 12:d9c133b360b0 | 148 | |
thomasmorris | 12:d9c133b360b0 | 149 | No_Of_Rotations = 20;//Defaults at 20 as a starting value |
thomasmorris | 12:d9c133b360b0 | 150 | |
thomasmorris | 12:d9c133b360b0 | 151 | Twist_Go = false; // |
thomasmorris | 12:d9c133b360b0 | 152 | Anneal_Go = false; // The Values for the start/ stop button on each select setting |
thomasmorris | 12:d9c133b360b0 | 153 | Test_Go = false; // |
thomasmorris | 12:d9c133b360b0 | 154 | |
thomasmorris | 12:d9c133b360b0 | 155 | Function = 0; //These values are used to navigate the differing modes |
thomasmorris | 12:d9c133b360b0 | 156 | Select = false; // |
thomasmorris | 12:d9c133b360b0 | 157 | |
thomasmorris | 12:d9c133b360b0 | 158 | Loop = 8; //Default loops |
thomasmorris | 12:d9c133b360b0 | 159 | Wait_Time = 6; //Default wait time |
thomasmorris | 12:d9c133b360b0 | 160 | |
thomasmorris | 12:d9c133b360b0 | 161 | Duty_Cycle = 50; //Percent |
thomasmorris | 12:d9c133b360b0 | 162 | Power_Time = 6; //Seconds |
thomasmorris | 12:d9c133b360b0 | 163 | |
thomasmorris | 12:d9c133b360b0 | 164 | /* |
thomasmorris | 12:d9c133b360b0 | 165 | Function 0 = Turn code |
thomasmorris | 12:d9c133b360b0 | 166 | Function 1 = Anneleaing |
thomasmorris | 12:d9c133b360b0 | 167 | Function 2 = Test |
thomasmorris | 12:d9c133b360b0 | 168 | */ |
thomasmorris | 12:d9c133b360b0 | 169 | pc.printf("testing\n"); |
thomasmorris | 12:d9c133b360b0 | 170 | //Thread Starts |
thomasmorris | 12:d9c133b360b0 | 171 | lcd_thread.start(LCD_thread); |
thomasmorris | 12:d9c133b360b0 | 172 | led_thread.start(LED_thread); |
thomasmorris | 12:d9c133b360b0 | 173 | while(1) // Main code |
thomasmorris | 12:d9c133b360b0 | 174 | { |
thomasmorris | 12:d9c133b360b0 | 175 | if (Function == 0){ //Turning Code |
thomasmorris | 12:d9c133b360b0 | 176 | if (Twist_Go == true){ |
thomasmorris | 12:d9c133b360b0 | 177 | STEPPER_MOTOR_1.Rotate_Steps(No_Of_Rotations); |
thomasmorris | 12:d9c133b360b0 | 178 | } |
thomasmorris | 12:d9c133b360b0 | 179 | }else if (Function == 1){ //Annealing Code |
thomasmorris | 12:d9c133b360b0 | 180 | if (Anneal_Go == true) |
thomasmorris | 12:d9c133b360b0 | 181 | { |
thomasmorris | 12:d9c133b360b0 | 182 | for ( int counter = 0; counter < Loop; counter++) //Loop value, check if works |
thomasmorris | 12:d9c133b360b0 | 183 | { |
thomasmorris | 12:d9c133b360b0 | 184 | Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare |
thomasmorris | 12:d9c133b360b0 | 185 | Tendon_Power=1; // set duty cycle to 100% |
thomasmorris | 12:d9c133b360b0 | 186 | wait(Wait_Time);//Variable |
thomasmorris | 12:d9c133b360b0 | 187 | Tendon_Power=0; // set duty cycle to 0% |
thomasmorris | 12:d9c133b360b0 | 188 | wait(6);//Fixed off time |
thomasmorris | 12:d9c133b360b0 | 189 | } |
thomasmorris | 12:d9c133b360b0 | 190 | } |
thomasmorris | 12:d9c133b360b0 | 191 | }else if (Function == 2){ //Testing Code |
thomasmorris | 12:d9c133b360b0 | 192 | if (Test_Go == true) |
thomasmorris | 12:d9c133b360b0 | 193 | { |
thomasmorris | 12:d9c133b360b0 | 194 | Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare |
thomasmorris | 12:d9c133b360b0 | 195 | Tendon_Power= Duty_Cycle / 100; // set duty cycle to variable input from buttons between 0-1 (on lcd this is a percentage) also increment by values of 10 |
thomasmorris | 12:d9c133b360b0 | 196 | wait(Power_Time);//Variable on time for power on |
thomasmorris | 12:d9c133b360b0 | 197 | Tendon_Power=0; // set duty cycle to 0% and power off |
thomasmorris | 12:d9c133b360b0 | 198 | } |
thomasmorris | 12:d9c133b360b0 | 199 | } |
thomasmorris | 12:d9c133b360b0 | 200 | } |
thomasmorris | 12:d9c133b360b0 | 201 | } |