Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Revision:
48:aca02ef5cd01
Parent:
47:80cfc181f8b3
Child:
50:442d13dff34a
--- a/Sensorplate/main.cpp	Mon Jan 15 12:15:29 2018 +0000
+++ b/Sensorplate/main.cpp	Tue Jan 16 12:05:00 2018 +0000
@@ -8,7 +8,7 @@
 Date of modification:                   8-1-2018
 Purpose of this file:                   Code for LPC1768 microcontroller for controlling buttons, LED's and communicate to PI
 Update ‘what’s new in this version?’:   Sensorplate connection test changed from software to hardware check.
-                                        ADC readout for accu deleted.                                         
+                                        ADC readout for accu deleted.
 Todo:                                   -> Fix LED issue (yellow and red flashes at random moments);
                                         -> Optimize functions / improve readability;
                                         -> Split functions in seperate files?;
@@ -35,7 +35,7 @@
 #define WS2812_BUF 3
 #define NUM_COLORS 5
 #define NUM_LEDS_PER_COLOR 3
-#define NUMBER_LED_FRONT (3)                                                       
+#define NUMBER_LED_FRONT (3)
 #define ONE_COLOR
 
 InterruptIn button_lock(PC_1);                                                       // Input on intterupt base decleration.
@@ -68,7 +68,7 @@
 Timer delay_between_button_pressed;                                                 // Timer for time between two buttons (to prevent pressing buttons simultaneously).
 Timer speaker_timer;                                                                // Timer for speaker activation.
 Timer piezo_electric_sample_timer;                                                  // Timer for equally time-spaced samples.
-
+Timer Knight_Rider_Timer;
 /*
 The code underneath this commentbox has some fixed parameters for serial/ADC reading:
 -> The address for the angle_device_reference_belt is set to 0x68 in the file MPU6050_belt (rule number: 19);
@@ -91,6 +91,7 @@
 
 int boot_delay_ms = 500;
 int total_readout_cycle_time_us = 100000;                                           // Cycle time in us.
+int total_knight_rider_cycle_time_ms = 1000;
 int i2c__frequency = 400000;                                                              // I2C Frequency.
 int baud_rate = 115200;                                                             // Baud rate.
 short piezo_resistive_array[8] = {0,0,0,0,0,0,0,0};                                 // 8 PR sensors 1 time per cycle.
@@ -100,15 +101,16 @@
 float gyroscope_sensorplate[3];                                                     // Raw gyroscope data.
 float accelerometer_reference_belt[3];                                              // Raw accelerometer data from belt.
 float gyroscope_reference_belt[3];                                                  // Raw gyroscope data from belt.
-int colourbuf[NUM_COLORS] = {0xff0000,0x00ff00,0x0000ff,0xffff00,0xffffff};          // hex codes for the different colours
+int colourbuf[NUM_COLORS] = {0xff0000,0x00ff00,0x0000ff,0xffa500,0xffffff};          // hex codes for the different colours
 char LED_colour = 'w';                                                              // Variable to set LED colour (standard set to green, untill PI sends other character). Other possible colours: red ('r') & yellow ('y').
 bool lock_state = false, lock_flag = 0, mute_state = 0, alarm = 0, calibration_flag = 0, intensity_select = 1;            // Boolean variables for logging states.
 bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0;                      // Flag variables.
 bool speaker_state = 0, LED_red_state = 0, LED_yellow_state = 0, LED_green_state = 0, power_plug_state = 0;
 bool speaker_logged = 0, LED_red_logged = 0, LED_yellow_logged = 0, LED_green_logged = 0, power_plug_logged = 0; // is toevoegen
 int locktime_ms = 2000;                                                             // Waittime for lock user interface in ms.
-int calibrationtime_ms = 5000;                                                      // Time to press new_patient button for calibration system.
+int calibrationtime_ms = 2000;                                                      // Time to press new_patient button for calibration system.
 int calibration_flash = 0;                                                          // Variable for flash LED's to indicate calibration.
+int lock_flash = 0;
 int buttondelay_ms = 750;                                                           // Button delay in ms.
 int delay_lock_interface = 3000*60;                                                 // Delay for non using interface locktime.
 int speaker_active_ms = 750;                                                        // Time to iterate speaker on and off when alarm occurs.
@@ -116,7 +118,7 @@
 int LED_red_intensity = 0, LED_blue_intensity = 0, LED_green_intensity = 0;         // Variables to set LED intensity.
 //short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last;                  // Variables to manage batteryvoltage. Maybe change current to other?
 //const int digital_value_ADC_powervoltage_unplugged = 15000;                         // Digital value to set the indicating LEDs to wall blue (should be set off later). const in hoofdletters
-int intensity_day = 40, intensity_night = 10;                                       // Intensity settings for LED's to wall.
+int intensity_day = 40, intensity_night = 7;                                       // Intensity settings for LED's to wall.
 double intensity = 0.0, control_LED_intensity = 0.0;                                // Variable between 0 and 1 to set the intensity of the LED's above the buttons. Intensity change to smart name!
 int colour_code = 0b00;
 bool pi_active = false;
@@ -153,41 +155,38 @@
 
 void serial_read()                                                                  // Function for serial read for select LED intensity and colour.
 {
-    intensity_select = intensity_code;
     colour_code = (colour_code_1 << 1 | colour_code_0);
-    if(colour_code != 0b00 && pi_active == false){pi_active = true;}
-    if(pi_active){
-    switch(colour_code){
-        case 0b11 :
-            LED_colour = 'y';
-            break;
-        case 0b10 :
-            LED_colour = 'b';
-            break;
-        case 0b01 :
-            LED_colour = 'g';
-            break;
-        case 0b00 :
-            LED_colour = 'r';
-            break;
-        default   :
-            LED_colour = 'w';
-            break;
-    }}
-    //bool read = pi_serial.readable();
-    //usb_serial.printf("Readable = %d\n", read);
-//    if (read_done) {                                                     // Function to check if pi is readable.
-//
-//        pi_serial.scanf("%s", message);
-//
+    if(colour_code != 0b00 && pi_active == false) {
+        pi_active = true;
+    }
+    if(pi_active) {
+        intensity_select = intensity_code;
+        switch(colour_code) {
+            case 0b11 :
+                LED_colour = 'y';
+                break;
+            case 0b10 :
+                LED_colour = 'b';
+                break;
+            case 0b01 :
+                LED_colour = 'g';
+                break;
+            case 0b00 :
+                LED_colour = 'r';
+                break;
+            default   :
+                LED_colour = 'w';
+                break;
+        }
+    }
 
     if (test_mode == 1) {                                                       // If statement for test purposal.
         usb_serial.printf("Intensity_select = %d en LED_colour = %d\n", intensity_select, LED_colour);
     }
 
     if (test_mode == 0) {
-    //usb_serial.printf("Message: %s\n", message);
-    usb_serial.printf("Intensity_select = %d en LED_colour = %d\n", intensity_select, LED_colour);
+        //usb_serial.printf("Message: %s\n", message);
+        usb_serial.printf("Intensity_select = %d en LED_colour = %d\n", intensity_select, LED_colour);
 
     }
 }
@@ -224,16 +223,6 @@
         reposition_flag = 0;
     }
 
-    /*if (batteryvoltage_current != batteryvoltage_last) {                            // If statement to control logging for batteryvoltage.
-        pi_serial.printf("%%" "%d\n", batteryvoltage_current);
-
-        if (test_mode == 1) {                                                       // If statement for test purposal.
-            usb_serial.printf("%%" "%d\n", batteryvoltage_current);
-        }
-
-        batteryvoltage_last = batteryvoltage_current;
-    }*/
-
     if (LED_red_logged != LED_red_state) {                                          // If statement to control logging for LED_red.
         if (LED_red_state == 1) {
             pi_serial.printf("&04\n");
@@ -363,7 +352,7 @@
 
     if (calibration_flash >= 1) {
         if ((calibration_flash % 2) == 0) {
-            px.SetAll(colourbuf[4]);
+            px.SetAll(colourbuf[2]);
         } else {
             ws.setII(0);
         }
@@ -372,61 +361,6 @@
     for (int z=WS2812_BUF; z >= 0 ; z--) {
         ws.write_offsets(px.getBuf(),0,0,0);
     }
-    /*
-    if ((LED_colour == 'r') || (LED_colour == 'g') || (LED_colour == 'b') || (LED_colour == 'y')) { // If statement to prevent potential errors in communication.
-        LED_red_intensity = 0;                                                                // Reset
-        LED_green_intensity = 0;
-        LED_blue_intensity = 0;
-
-        if (LED_colour == 'r') {                                                    // Set LED_colour to red.
-            LED_red_intensity = (2.55*intensity);                                             // 255 / 100 = 2.55 (8 - bit digital value; 0-255 = 256 steps); intensity is a value between 0 and 100.
-            LED_green_intensity = 0;
-            LED_blue_intensity = 0;
-            LED_red_state = 1;
-        } else {
-            LED_red_state = 0;
-        }
-
-        if (LED_colour == 'y') {                                                    // Set LED_colour to yellow.
-            LED_red_intensity = (2.55*intensity);
-            LED_green_intensity = (2.55*intensity);
-            LED_blue_intensity = 0;
-            LED_yellow_state = 1;
-        } else {
-            LED_green_state = 0;
-        }
-
-        if (LED_colour == 'g') {                                                    // Set LED_colour to green.
-            LED_red_intensity = 0;
-            LED_green_intensity = (2.55*intensity);
-            LED_blue_intensity = 0;
-            LED_green_state = 1;
-        } else {
-            LED_green_state = 0;
-        }
-
-        if (LED_colour == 'b') {                                                    // Set LED_colour to blue.
-            LED_red_intensity = 0;
-            LED_green_intensity = 0;
-            LED_blue_intensity = (2.55*intensity);
-        }
-    }
-
-    if (calibration_flash >= 1) {                                                   // If statement for flashing LED's (colour = white) when calibration is active.
-        if ((calibration_flash % 2) == 0) {                                         // If value can not be devided by two, set LED's on.
-            LED_red_intensity = 255;
-            LED_green_intensity = 255;
-            LED_blue_intensity = 255;
-            LED_on_dev_board4 = 1;
-        } else {                                                                    // Else set LED's off.
-            LED_red_intensity = 0;
-            LED_green_intensity = 0;
-            LED_blue_intensity = 0;
-            LED_on_dev_board4 = 0;
-        }
-        calibration_flash--;
-    }
-    */
 }
 
 void trigger_lock()                                                                 // If rising edge lock button is detected start locktimer.
@@ -454,6 +388,7 @@
 void reposition_button_triggered()
 {
     if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) {  // Control statement for lock interface and delay for non using buttons at the same time.
+        lock_flash = 10;
     } else {
         delay_between_button_pressed.reset();
         delay_between_button_pressed.start();
@@ -480,6 +415,7 @@
 {
 
     if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) {  // Control statement for lock interface and delay for non using buttons at the same time.
+        lock_flash = 10;
     } else {
         delay_between_button_pressed.reset();
         delay_between_button_pressed.start();
@@ -504,6 +440,7 @@
 {
 
     if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) {
+        lock_flash = 10;
     } else {
         button_calibration_hold_timer.reset(); // inline ?
         button_calibration_hold_timer.start();
@@ -547,7 +484,7 @@
 
 void timer_functions()                                                              // Function which contains statements using timers.
 {
-    if (button_lock == 1){
+    if (button_lock == 1) {
         button_lock_hold_timer.stop();
         button_lock_hold_timer.reset();
     }
@@ -563,7 +500,7 @@
         }
     }
 
-    if (button_new_patient == 1){
+    if (button_new_patient == 1) {
         button_calibration_hold_timer.stop();
         button_calibration_hold_timer.reset();
     }
@@ -586,13 +523,6 @@
     }
 }
 
-//void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val)                 // Generate LED colour function (library function PixelArray is used for this item).
-//{
-//    out->red   = LED_red_intensity;
-//    out->green = LED_green_intensity;
-//    out->blue  = LED_blue_intensity;
-//}
-
 void set_userinterface_LED()                                                                   // Control functions for LED above buttons (added because of failures).
 {
     if (lock_state == 1) {
@@ -609,168 +539,29 @@
             new_patient_feedback_LED = 0;
         }
     }
-}
-
-/*void read_voltage()                                                                 // Function for reading voltages from power and battery.
-{
-    if (power_plug_state == 1) {                                                    // If supplyvoltage (readed from input) is greater then the setted alarmvoltage.
-        alarm = 0;                                                                  // Alarm is off.
-        speaker_state = 0;
-    } else {
-        alarm = 1;                                                                  // Else alarm is on.
-        speaker_state = 1;
-    }
-
-
-    if (alarm == 1 && mute_state == 1 && (batteryvoltage_current > alarm_voltage)) {// Set speaker on for 750 ms. Use PWM? => Split in more functions.
-        speaker1 = 0;                                                               // Set speaker.
-    }
-
-    if ((alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) || ((batteryvoltage_current < alarm_voltage) && (speaker_timer.read_ms() < speaker_active_ms) && power_plug_state == 0)) { // Set speaker on for 750 ms.
-        speaker1 = 1;                                                               // Set speaker.
-        speaker_timer.start();                                                      // Set timer for speaker to iterate on and off.
-    }
-
-    if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) {
-        speaker1 = 0;                                                               // Turn off speaker (use two outputs because of currentlimiting of one).
-    }
-
-    if (speaker_timer.read_ms() > (speaker_active_ms*2)) {                          //
-        speaker_timer.stop();                                                       // Stop speaker timer.
-        speaker_timer.reset();
-    }
-    // Read channel 0 from external ADC (batteryvoltage); voltagedeviders are used,
-    batteryvoltage_current = adsAccu.readADC_SingleEnded(0);                        // because of higher voltage then Vcc of ADC (5.3 V (= Vcc + 0.3 V) max possible at each analog input).
-    powervoltage_current = adsAccu.readADC_SingleEnded(1);                          // Read channel 1 from external ADC (powervoltage).
-
-    if (powervoltage_current < digital_value_ADC_powervoltage_unplugged) {          // If statement to set LED's to blue.
-        power_plug_state = 0;
-        //LED_colour = 'b';
-    } else {
-        power_plug_state = 1;
-    }
-}*/
-
-void read_adc()
-{
-    return;
-    piezo_electric_sample_timer.reset();                                            // Clock gebruiken o.i.d.?
-    piezo_electric_sample_timer.start();
-    connection_test_sensorplate = !testpin_sensorplate;
-
-    if (test_mode == 1) {
-        usb_serial.printf("Connection test sensorplate = %d\n", connection_test_sensorplate);
-    }
-
-    /*
-    if (connection_test_sensorplate == 0) {
-        lock_state = 1;
-        LED_on_dev_board2 = 1;
-        lock_feedback_LED = 0;
-    }*/
-
-    if (connection_test_sensorplate == 1) {
-        piezo_electric_array[0] = piezo_electric_adc.readADC_SingleEnded(0);            // First PE readout.
-
-        for (uint8_t k = 0; k < 4; ++k) {
-            piezo_resistive_array[k] =    piezo_resistive_adc1.readADC_SingleEnded(k);  // First 4 PR readout.
-        }
-
-        while(piezo_electric_sample_timer.read_us()<(1*(total_readout_cycle_time_us/5))) {} // Wait untill 20% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
-
-        piezo_electric_array[1] = piezo_electric_adc.readADC_SingleEnded(0);            // Second PE readout.
-
-        for (uint8_t k = 0; k < 4; ++k) {
-            piezo_resistive_array[k+4] =  piezo_resistive_adc2.readADC_SingleEnded(k);  // Last 4 PR readout.
+    if (lock_flash >= 1 && lock_state == 1) {
+        if ((lock_flash % 2) == 0) {
+            lock_feedback_LED = control_LED_intensity;
+        } else {
+            lock_feedback_LED = 0;
         }
-
-        while(piezo_electric_sample_timer.read_us()<(2*(total_readout_cycle_time_us/5))) {} // Wait untill 40% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
-
-        piezo_electric_array[2] = piezo_electric_adc.readADC_SingleEnded(0);            // Third PE readout.
-
-        angle_device_sensorplate.getAccelero(accelerometer_sensorplate);                // Get accelerometer data.
-        angle = accelerometer_sensorplate[2]*100;
-        if(angle == 0) {
-            MPU6050 angle_device_sensorplate(PB_9, PB_8);
-            angle_device_sensorplate.getAccelero(accelerometer_sensorplate);
-            angle = accelerometer_sensorplate[2]*100;
-        }
-        angle_device_sensorplate.getGyro(gyroscope_sensorplate);                        // Get gyroscope data.
-
-        if (test_belt == 1) {
-            angle_device_reference_belt.getGyro(gyroscope_reference_belt);              // Get gyroscope data from Belt.
-            angle_device_reference_belt.getAccelero(accelerometer_reference_belt);      // Get accelerometer data from belt.
-        }
-
-        if (connection_test_sensorplate == 1) {                                                     // If statement for sending serial information sensorplate data when connection test is active.
-            // Receiving order sensor information: 3 accelero sensors & 3 gyroscope sensors from sensorplate; 3 accelero sensors & 3 gyroscope sensors from belt. Is splitted in two parts - part 2/2.
-            pi_serial.printf("?,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n", accelerometer_sensorplate[0], accelerometer_sensorplate[1], accelerometer_sensorplate[2], gyroscope_sensorplate[0], gyroscope_sensorplate[1], gyroscope_sensorplate[2], accelerometer_reference_belt[0], accelerometer_reference_belt[1], accelerometer_reference_belt[2], gyroscope_reference_belt[0], gyroscope_reference_belt[1], gyroscope_reference_belt[2]);
-        } // binair print and convert in pi
-
-        while(piezo_electric_sample_timer.read_us()<(3*(total_readout_cycle_time_us/5))) {}  // Wait untill 60% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
-
-        piezo_electric_array[3] = piezo_electric_adc.readADC_SingleEnded(0);        // Fourth PE readout.
+        lock_flash--;
+    } else {
+        lock_flash = 0;
     }
-
-    timer_functions();
-
-    /*batteryvoltage_current = batteryvoltage_last;
-    powervoltage_current = powervoltage_last;
-    read_voltage();*/                                                                 // Read_voltage function to control alarm.
-
-    /*if (test_mode == 1) {
-        usb_serial.printf("Voltage = %d   ,   %d\n", batteryvoltage_current, powervoltage_current);
-    }*/
-
-    uint32_t val = 0;
-    colour_select_indicating_LED_wall(LED_colour);                                  // Function to select colour.
-//    indicator_LEDs.update(generate, NUMBER_LED_FRONT, val);                         // Function to set the LED's which shines to the wall (indicating change patient position).
-    set_userinterface_LED();                                                        // Set LED's of user interface (LED's above buttons).
-
-    while(piezo_electric_sample_timer.read_us()<(4*(total_readout_cycle_time_us/5))) {}  // Wait untill 80% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
-
-    if (test_mode == 1) {                                                           // If statement for test purposal.
-        usb_serial.printf("Angle device sensorplate = %d\n",angle_device_sensorplate.testConnection());
-    }
-
-    if (connection_test_sensorplate == 1) {
-        piezo_electric_array[4] = piezo_electric_adc.readADC_SingleEnded(0);         // Fifth PE readout.
-    }
-
-    while(piezo_electric_sample_timer.read_us()<(4.25*(total_readout_cycle_time_us/5))) {}  // Wait untill 85% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
-
-    serial_read();                                                                   // Call function for reading information from PI by serial connection.
-    serial_log();                                                                    // Call function for logging information to PI by serial connection.
-
-    if (test_mode == 1) {                                                            // If statements for test purposal (untill * mark).
-        usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
-    }
-    if (test_pin == 1) {
-        test_mode = 1;
-        usb_serial.printf("%d\n",test_mode);
-    }
-    if (test_pin == 0) {
-        test_mode = 0;
-        usb_serial.printf("%d\n",test_mode);
-    }
-
-    if (test_mode == 1) {
-        usb_serial.printf("Loop time: %d ms\n", piezo_electric_sample_timer.read_ms());
-    }
-    // * End of if statements for test purposal.
 }
 
 int main()                                                                          // Main function. inline function "Momo Init" bijvoorbeeld
 {
+    speaker1 = 1;
     wait_ms(boot_delay_ms);                                                         // Wait to boot sensorplate first.
+    speaker1 = 0;
     i2c_sensorplate_adc.frequency(i2c__frequency);                                        // Set frequency for i2c connection to sensorplate (variable is declared in config part).
-    //i2c_power_adc.frequency(i2c__frequency);                                              // Same as line 695, but now for ADC to read battery- en powervoltage.
     usb_serial.baud(baud_rate);                                                     // Set serial USB connection baud rate (variable is declared in config part).
     pi_serial.baud(baud_rate);                                                      // Same as line 697, but now for serial PI connection.
     piezo_resistive_adc1.setGain(GAIN_TWOTHIRDS);                                   // Set ranges of ADC to +/-6.144V (end is marked with #):
     piezo_resistive_adc2.setGain(GAIN_TWOTHIRDS);
     piezo_electric_adc.setGain(GAIN_TWOTHIRDS);
-    //adsAccu.setGain(GAIN_TWOTHIRDS);                                                // #) End of configuration ADC ranges.
     pi_serial.format(8, SerialBase::None, 1);                                       // Set serial communication line with PI.
 
     button_lock.mode(PullUp);
@@ -790,13 +581,40 @@
 
     ws.useII(WS2812::GLOBAL);                                                       // use global intensity scaling
     set_intensity_LEDs();                                                           // Initialize intensity for user interface LED's and LED's shines to wall.
-    lock_feedback_LED = control_LED_intensity;                                      // Lock LED initialization.
 
-//    pi_serial.attach(&serial_read);
-//    total_readout_cycle.attach_us(&read_adc, total_readout_cycle_time_us);        // Call function to start reading sensorplate and other functionalities.
+    __disable_irq();
+    while(!pi_active) {
+        Knight_Rider_Timer.reset();
+        Knight_Rider_Timer.start();
+        reposition_feedback_LED = control_LED_intensity;
+        new_patient_feedback_LED = 0;
+        while(Knight_Rider_Timer.read_ms()<(1*(total_knight_rider_cycle_time_ms/8))) {}
+        new_patient_feedback_LED = control_LED_intensity;
+        while(Knight_Rider_Timer.read_ms()<(2*(total_knight_rider_cycle_time_ms/8))) {}
+        mute_feedback_LED = control_LED_intensity;
+        reposition_feedback_LED = 0;
+        while(Knight_Rider_Timer.read_ms()<(3*(total_knight_rider_cycle_time_ms/8))) {}
+        lock_feedback_LED = control_LED_intensity;
+        new_patient_feedback_LED = 0;
+        while(Knight_Rider_Timer.read_ms()<(4*(total_knight_rider_cycle_time_ms/8))) {}
+        mute_feedback_LED = 0;
+        while(Knight_Rider_Timer.read_ms()<(5*(total_knight_rider_cycle_time_ms/8))) {}
+        mute_feedback_LED = control_LED_intensity;
+        while(Knight_Rider_Timer.read_ms()<(6*(total_knight_rider_cycle_time_ms/8))) {}
+        new_patient_feedback_LED = control_LED_intensity;
+        lock_feedback_LED = 0;
+        while(Knight_Rider_Timer.read_ms()<(7*(total_knight_rider_cycle_time_ms/8))) {}
+        reposition_feedback_LED = control_LED_intensity;
+        mute_feedback_LED = 0;
 
+        serial_read();
+        colour_select_indicating_LED_wall(LED_colour);
+        while(Knight_Rider_Timer.read_ms()<(8*(total_knight_rider_cycle_time_ms/8))) {}
+
+    }
+    lock_feedback_LED = control_LED_intensity;                                      // Lock LED initialization.
+    __enable_irq();
     while (1) {
-//        wait_us(total_readout_cycle_time_us+1);                                     // Wait indefinitely.
         piezo_electric_sample_timer.reset();                                            // Clock gebruiken o.i.d.?
         piezo_electric_sample_timer.start();
         connection_test_sensorplate = !testpin_sensorplate && pi_active;
@@ -807,19 +625,13 @@
         if (test_mode == 1) {
             usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
         }
-        /*
-        if (connection_test_sensorplate == 0) {
-            lock_state = 1;
-            LED_on_dev_board2 = 1;
-            lock_feedback_LED = 0;
-        }*/
 
         if (connection_test_sensorplate == 1) {
             piezo_electric_array[0] = piezo_electric_adc.readADC_SingleEnded(0);            // First PE readout.
 
-//            for (uint8_t k = 0; k < 4; ++k) {
-//                piezo_resistive_array[k] =    piezo_resistive_adc1.readADC_SingleEnded(k);  // First 4 PR readout.
-//            }
+            for (uint8_t k = 0; k < 4; ++k) {
+                piezo_resistive_array[k] =    piezo_resistive_adc1.readADC_SingleEnded(k);  // First 4 PR readout.
+            }
             if (test_mode == 1) {
                 usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
             }
@@ -827,9 +639,9 @@
 
             piezo_electric_array[1] = piezo_electric_adc.readADC_SingleEnded(0);            // Second PE readout.
 
-//            for (uint8_t k = 0; k < 4; ++k) {
-//                piezo_resistive_array[k+4] =  piezo_resistive_adc2.readADC_SingleEnded(k);  // Last 4 PR readout.
-//            }
+            for (uint8_t k = 0; k < 4; ++k) {
+                piezo_resistive_array[k+4] =  piezo_resistive_adc2.readADC_SingleEnded(k);  // Last 4 PR readout.
+            }
             if (test_mode == 1) {
                 usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
             }
@@ -837,31 +649,30 @@
             while(piezo_electric_sample_timer.read_us()<(2*(total_readout_cycle_time_us/5))) {} // Wait untill 40% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
 
             piezo_electric_array[2] = piezo_electric_adc.readADC_SingleEnded(0);            // Third PE readout.
-
-//            angle_device_sensorplate.getAccelero(accelerometer_sensorplate);                // Get accelerometer data.
-//            angle = accelerometer_sensorplate[2]*100;
-//            if(angle == 0) {
-//                MPU6050 angle_device_sensorplate(PB_9, PB_8);
-//               angle_device_sensorplate.getAccelero(accelerometer_sensorplate);
-//                angle = accelerometer_sensorplate[2]*100;
-//            }
-//            angle_device_sensorplate.getGyro(gyroscope_sensorplate);                        // Get gyroscope data.
+            angle_device_sensorplate.getAccelero(accelerometer_sensorplate);                // Get accelerometer data.
+            angle = accelerometer_sensorplate[2]*100;
+            if(angle == 0) {
+                MPU6050 angle_device_sensorplate(PB_9, PB_8);
+               angle_device_sensorplate.getAccelero(accelerometer_sensorplate);
+                angle = accelerometer_sensorplate[2]*100;
+            }
+            angle_device_sensorplate.getGyro(gyroscope_sensorplate);                        // Get gyroscope data.
             if (test_mode == 1) {
                 usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
             }
-            //if (test_belt == 1) {
-//                angle_device_reference_belt.getGyro(gyroscope_reference_belt);              // Get gyroscope data from Belt.
-//                angle_device_reference_belt.getAccelero(accelerometer_reference_belt);      // Get accelerometer data from belt.
-//            }
+            if (test_belt == 1) {
+                angle_device_reference_belt.getGyro(gyroscope_reference_belt);              // Get gyroscope data from Belt.
+                angle_device_reference_belt.getAccelero(accelerometer_reference_belt);      // Get accelerometer data from belt.
+            }
 
-//            if (connection_test_sensorplate == 1) {
-//                if (test_belt == 0) {                                                     // If statement for sending serial information sensorplate data when connection test is active.
-//                    pi_serial.printf("?,%f,%f,%f,%f,%f,%f,0,0,0,0,0,0,\n", accelerometer_sensorplate[0], accelerometer_sensorplate[1], accelerometer_sensorplate[2], gyroscope_sensorplate[0], gyroscope_sensorplate[1], gyroscope_sensorplate[2]);
-//                    } else{
-//                // Receiving order sensor information: 3 accelero sensors & 3 gyroscope sensors from sensorplate; 3 accelero sensors & 3 gyroscope sensors from belt. Is splitted in two parts - part 2/2.
-//                pi_serial.printf("?,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n", accelerometer_sensorplate[0], accelerometer_sensorplate[1], accelerometer_sensorplate[2], gyroscope_sensorplate[0], gyroscope_sensorplate[1], gyroscope_sensorplate[2], accelerometer_reference_belt[0], accelerometer_reference_belt[1], accelerometer_reference_belt[2], gyroscope_reference_belt[0], gyroscope_reference_belt[1], gyroscope_reference_belt[2]);
-//                }
-//            } // binair print and convert in pi
+            if (connection_test_sensorplate == 1) {
+                if (test_belt == 0) {                                                     // If statement for sending serial information sensorplate data when connection test is active.
+                    pi_serial.printf("?,%f,%f,%f,%f,%f,%f,0,0,0,0,0,0,\n", accelerometer_sensorplate[0], accelerometer_sensorplate[1], accelerometer_sensorplate[2], gyroscope_sensorplate[0], gyroscope_sensorplate[1], gyroscope_sensorplate[2]);
+                    } else{
+                // Receiving order sensor information: 3 accelero sensors & 3 gyroscope sensors from sensorplate; 3 accelero sensors & 3 gyroscope sensors from belt. Is splitted in two parts - part 2/2.
+                pi_serial.printf("?,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n", accelerometer_sensorplate[0], accelerometer_sensorplate[1], accelerometer_sensorplate[2], gyroscope_sensorplate[0], gyroscope_sensorplate[1], gyroscope_sensorplate[2], accelerometer_reference_belt[0], accelerometer_reference_belt[1], accelerometer_reference_belt[2], gyroscope_reference_belt[0], gyroscope_reference_belt[1], gyroscope_reference_belt[2]);
+                }
+            } // binair print and convert in pi
 
             while(piezo_electric_sample_timer.read_us()<(3*(total_readout_cycle_time_us/5))) {}  // Wait untill 60% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
 
@@ -874,19 +685,8 @@
         if (test_mode == 1) {
             usb_serial.printf("Loop time after timer_functions: %d ms\n",piezo_electric_sample_timer.read_ms());
         }
-        /*batteryvoltage_current = batteryvoltage_last;
-        powervoltage_current = powervoltage_last;
-        read_voltage();*/                                                                 // Read_voltage function to control alarm.
-        if (test_mode == 1) {
-            usb_serial.printf("Loop time after read_voltage: %d ms\n",piezo_electric_sample_timer.read_ms());
-        }
-        /*if (test_mode == 1) {
-            usb_serial.printf("Voltage = %d   ,   %d\n", batteryvoltage_current, powervoltage_current);
-        }*/
 
-        uint32_t val = 0;
         colour_select_indicating_LED_wall(LED_colour);                                  // Function to select colour.
-//        indicator_LEDs.update(generate, NUMBER_LED_FRONT, val);                         // Function to set the LED's which shines to the wall (indicating change patient position).
         set_userinterface_LED();                                                        // Set LED's of user interface (LED's above buttons).
 
         while(piezo_electric_sample_timer.read_us()<(4*(total_readout_cycle_time_us/5))) {}  // Wait untill 80% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
@@ -914,7 +714,7 @@
         if (test_mode == 0) {                                                            // If statements for test purposal (untill * mark).
             usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
         }
-        
+
         while(piezo_electric_sample_timer.read_us()<(total_readout_cycle_time_us)) {}  // Wait untill 100% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
         //if (test_pin == 1) {
 //            test_mode = 1;