Testing 1 blue pill

Dependencies:   mbed mbed-rtos TextLCD

main.cpp

Committer:
thomasmorris
Date:
2019-02-05
Revision:
12:d9c133b360b0
Parent:
11:0309bef74ba8
Child:
13:c681f340909b

File content as of revision 12:d9c133b360b0:

#include "mbed.h"
#include "rtos.h"
#include "TextLCD.h"
#include "iostream"
#include "SETUP.hpp"
#include "stdio.h"
#include "string.h"

//Digital Outputs
DigitalOut led1(LED1);
DigitalOut led2(LED2);

//Thread Setups
Thread lcd_thread;
Thread led_thread;
TextLCD lcd(D0,D1,D4,D5,D6,D7); // rs, e, d4-d7
Serial pc(USBTX, USBRX);         //Define serial namespace so the serial comms can be printed to


//Switch toggling to bools for readability
InterruptIn button_up(A0);      // UP BUTTON
InterruptIn button_down(A1);    // These setup the button interupts
InterruptIn button_start(A2);   // START / STOP BUTTON
InterruptIn button_funct(A3);// Random pin  CHANGE FOR FUNCTION BUTTON
InterruptIn button_select(A4);// Random pin CHANGE FOR SELECT BUTTON

PwmOut Tendon_Power(PE_8); 

//VARIABLE DEFINITIONS
int No_Of_Rotations;
int Function;
bool Twist_Go;
bool Anneal_Go;
bool Test_Go;
bool Select;

int turns_done;
int turns_todo;
int Loop;
int Wait_Time;//IN SECONDS

int Duty_Cycle;
int Power_Time;


//Thread Functions
void LCD_thread(){
    while(1){
        //lcd.printf("Test\n");
        lcd.cls();
        if(Function == 0){
            lcd.printf("Mode: Anneal\n");
            lcd.printf("Loop:%d Wait:%d\n",Loop,Wait_Time);
        }else if(Function == 1){
            lcd.printf("Mode: Test\n");
            lcd.printf("Duty: %d Time: %d\n",Duty_Cycle,Power_Time);
        }else if(Function == 2){
        
            lcd.printf("Mode: Turn\n");
            lcd.printf("Done: %d ToDo: %d\n",turns_done,turns_todo);
        }
        Thread::wait(5000);
        lcd.cls();
        Function = Function +1;
        if(Function >2)
        {
            Function = 0;   
        }   
    } 
}
void LED_thread(){
    while (1){
        led2 = !led2;
        Thread::wait(1000);
    }
}
//Interrupt functions
void up(){//Action if the up button is pressed increment
    if (Function == 0){ 
        No_Of_Rotations = No_Of_Rotations + 1;//Increases Turn number
    }else if (Function == 1){
        if (Select == false){
            Loop = Loop + 1;  //Increases Repetitions in Annealing
        }else if (Select == true){
            Wait_Time = Wait_Time + 1;  //Increases Wait time
        }
        }else if (Function == 2){  
            if (Select == false){
                if (Duty_Cycle < 100){
                    Duty_Cycle = Duty_Cycle + 10; //Increases Testing Duty Cycle
                }}else if (Select == true){
                    Power_Time = Power_Time + 1;    //Increases time on
                }
        }
}
void down(){ //Action if the down button is pressed decrement
    if (Function == 0){ 
        No_Of_Rotations = No_Of_Rotations - 1;
        }else if (Function == 1){
            if (Select == false){
                if (Loop > 0){
                    Loop = Loop - 1;
                }
                }else if (Select == true){
                    if (Wait_Time > 0){
                        Wait_Time = Wait_Time - 1;
                    }
                }
     }else if (Function == 2){  
        if (Select == false){
        if (Duty_Cycle > 0){
            Duty_Cycle = Duty_Cycle - 10;     //Decreases duty
        }}else if (Select == true){
        if (Power_Time > 0){
            Power_Time = Power_Time - 1;    //Decreases Time on
        }
    }
     }
}
void start_stop() { //Action if the Start/Stop button is pressed
    if (Function == 0)//Twist selected
    {
        Twist_Go = !Twist_Go;//toggle used for coiling and twisting
    }else if (Function == 1){ //Annealing selected 
        Anneal_Go = !Anneal_Go;//toggle
    }else if (Function == 2){ //Testing selected 
        Test_Go = !Test_Go;//toggle
    }
}
void Function_Selection() { //Action if the Function button is pressed
    if (Function < 2){
        Function = Function + 1;
    }else{
        Function = 0;
    }
}
void Selection() { //Action if the Select button is pressed
    Select = !Select;
}
int main() 
{
    //Interrupt setters
    button_up.rise(&up);                        //Sets up Up button
    button_down.rise(&down);                    //Sets up Down Button
    button_start.rise(&start_stop);             //Sets up Start/Stop Button
    button_funct.rise(&Function_Selection);     //Sets up Function Button
    button_select.rise(&Selection);             //Sets up Select Button
   
    No_Of_Rotations = 20;//Defaults at 20 as a starting value
    
    Twist_Go = false;  //
    Anneal_Go = false; // The Values for the start/ stop button on each select setting
    Test_Go = false;   //
    
    Function = 0;   //These values are used to navigate the differing modes
    Select = false;    //
    
    Loop = 8;       //Default loops
    Wait_Time = 6;  //Default wait time
    
    Duty_Cycle = 50; //Percent
    Power_Time = 6; //Seconds
    
    /*
    Function 0 = Turn code
    Function 1 = Anneleaing
    Function 2 = Test
    */
    pc.printf("testing\n");
    //Thread Starts
    lcd_thread.start(LCD_thread);
    led_thread.start(LED_thread);
    while(1)            // Main code
    {
        if (Function == 0){       //Turning Code
            if (Twist_Go == true){
                STEPPER_MOTOR_1.Rotate_Steps(No_Of_Rotations);
            }
        }else if (Function == 1){  //Annealing Code
            if (Anneal_Go == true)
            {
                for ( int counter = 0; counter < Loop; counter++)  //Loop value, check if works
                {
                    Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare
                    Tendon_Power=1; // set duty cycle to 100%
                    wait(Wait_Time);//Variable
                    Tendon_Power=0; // set duty cycle to 0%
                    wait(6);//Fixed off time
                 }
              }
        }else if (Function == 2){  //Testing Code
            if (Test_Go == true)
            {
                Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare
                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
                wait(Power_Time);//Variable on time for power on
                Tendon_Power=0; // set duty cycle to 0% and power off
            }
        }
    }
}