Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Revision:
43:d09814c177a0
Parent:
42:673ddef4cfa4
Child:
44:dcbde3175a37
--- a/Sensorplate/main.cpp	Thu Nov 30 09:02:49 2017 +0000
+++ b/Sensorplate/main.cpp	Fri Dec 01 10:05:01 2017 +0000
@@ -8,10 +8,10 @@
 Date of modification:                   18-10-2017
 Purpose of this file:                   Code for LPC1768 microcontroller for controlling buttons, LED's and communicate to PI
 Update ‘what’s new in this version?’:   New structure added.
-                                        Readability improved.                                        
+                                        Readability improved.
                                         Code optimized (variables and functions).
 Todo:                                   -> Fix LED issue (yellow and red flashes at random moments);
-                                        -> Optimize functions / improve readability; 
+                                        -> Optimize functions / improve readability;
                                         -> Split functions in seperate files?;
                                         -> Fix when sensorplate is not connected;
                                         -> Rule 570: if statement change to turn off LED's when power is plugged out (also related to rule 106).
@@ -19,9 +19,9 @@
 Source file:                            http://mbed.com/
 
 Information files:
-(1) Flowchart:                          
+(1) Flowchart:
 (2) Table serial communication:         https://docs.google.com/spreadsheets/d/1kHlithHxtoMDGvbcdH8vwSw5W5ArxlwDPsyfra1dtQM/edit?usp=drive_web
-(3) Technical manual CU-/software:                   
+(3) Technical manual CU-/software:
 */
 
 /************************ CONFIG ***********************************/
@@ -30,36 +30,46 @@
 #include "Adafruit_ADS1015.h"
 #include "MPU6050.h"
 #include "MPU6050_belt.h"
-#include "neopixel.h"
+#include "PixelArray.h"
+#include "WS2812.h"
 
+#define WS2812_BUF 3
+#define NUM_COLORS 5
+#define NUM_LEDS_PER_COLOR 3
 #define NUMBER_LED_FRONT (3)  // declaren waarvoor dient                                                     
 #define ONE_COLOR
 
-InterruptIn button_lock(PC_0);                                                       // Input on intterupt base decleration. 
+InterruptIn button_lock(PC_0);                                                       // Input on intterupt base decleration.
 InterruptIn button_reposition(PC_1);
 InterruptIn button_mute(PC_2);
 InterruptIn button_new_patient(PC_3);
 
-DigitalOut  LED_on_dev_board1(LED1);                                                // Decleration of digital outputs.                                          
+DigitalIn intensity_code(PA_12);
+DigitalIn colour_code_1(PA_11);
+DigitalIn colour_code_0(PB_12);
+
+DigitalOut  LED_on_dev_board1(LED1);                                                // Decleration of digital outputs.
 DigitalOut  LED_on_dev_board2(LED2);
 DigitalOut  LED_on_dev_board3(LED3);
 DigitalOut  LED_on_dev_board4(LED4);
-DigitalOut speaker1(PC_8);               // relatie aangeven!
-DigitalOut speaker2(PC_6);
-//neopixel::PixelArray indicator_LEDs(PA_7);                                           
+DigitalOut speaker1(PC_12);               // relatie aangeven!
+//neopixel::PixelArray indicator_LEDs(PA_7);
+PixelArray px(WS2812_BUF);
+WS2812 ws(PA_7, WS2812_BUF, 3, 9, 9, 6);
 
-PwmOut lock_feedback_LED(PB_13);                                                      // Declaration of pulse with modulation outputs.                                
+
+PwmOut lock_feedback_LED(PB_13);                                                      // Declaration of pulse with modulation outputs.
 PwmOut mute_feedback_LED(PB_1);
 PwmOut new_patient_feedback_LED(PB_14);
 PwmOut reposition_feedback_LED(PB_15);
 
 Timer button_lock_hold_timer;                                                       // Timer for time lock button should be pressed.
 Timer button_calibration_hold_timer;                                                // Timer for calibration function (new patient holding 5 seconds).
-Timer delay_between_button_pressed;                                                 // Timer for time between two buttons (to prevent pressing buttons simultaneously). 
+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.
 
-/* 
+/*
 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);
 -> The adress for the angle_device_sensorplate is set to 0x69 in the file MPU6050.h (rule number: 19);
@@ -67,17 +77,17 @@
 -> For detailed information/questions about this item, please read the technical manual or contact: Ricardo Molenaar | ricardo.molenaar@gmail.com
 */
 I2C i2c_sensorplate_adc(PB_9, PB_8);                                                  // I2C for sensorplate.
-I2C i2c_power_adc(PB_11, PB_10);                                                         // I2C for accupack. 
+I2C i2c_power_adc(PB_11, PB_10);                                                         // I2C for accupack.
 MPU6050 angle_device_sensorplate(PB_9, PB_8);                                          // i2c pins // i2c address hardcoded 0x68.
 MPU6050_belt angle_device_reference_belt(PB_9, PB_8);                                  // i2c pins // i2c address hardcoded 0x69.
 Adafruit_ADS1115 piezo_resistive_adc1(&i2c_sensorplate_adc, 0x48);                  // i2c pins, i2c address.
 Adafruit_ADS1115 piezo_resistive_adc2(&i2c_sensorplate_adc, 0x49);                  // i2c pins, i2c address.
 Adafruit_ADS1115 piezo_electric_adc(&i2c_sensorplate_adc, 0x4B);                    // i2c pins, i2c address.
 Adafruit_ADS1115 adsAccu(&i2c_power_adc, 0x48);                                     // i2c pins, i2c address.
-Serial usb_serial(SERIAL_TX, SERIAL_RX);                                            // tx, rx  
-Serial pi_serial(PC_10, PC_11);                                                     // tx, rx  
+Serial usb_serial(SERIAL_TX, SERIAL_RX);                                            // tx, rx
+Serial pi_serial(PC_10, PC_11);                                                     // tx, rx
 Ticker total_readout_cycle;                                                         // Polling cycle.
-// End of commentbox related to the serial configuration/ADC reading components. 
+// End of commentbox related to the serial configuration/ADC reading components.
 
 int boot_delay_ms = 500;
 int total_readout_cycle_time_us = 100000;                                           // Cycle time in us.
@@ -90,6 +100,7 @@
 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
 char LED_colour = 'g';                                                              // 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.
@@ -108,19 +119,21 @@
 int intensity_day = 40, intensity_night = 10;                                       // 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;
+
 /*************************** TEST ********************************/
 // Verify algoritm function: for belt activation, set test_belt 1 (connect pin p20 to 3.3V).
-Timer test_timer;                                                           
+Timer test_timer;
 DigitalIn test_pin(PA_11, PullDown);
 
-// Variable to set if belt is used to test algorithm: 
-bool test_belt = 0; 
+// Variable to set if belt is used to test algorithm:
+bool test_belt = 0;
 
-// Set test mode on (log functions to pc serial: interrupts, LED intensity and serial messages): 
+// Set test mode on (log functions to pc serial: interrupts, LED intensity and serial messages):
 bool test_mode = 0;
 
 // Variable for connection test (should be changed):
-int connection_test_sensorplate; 
+int connection_test_sensorplate;
 
 /*************************** CODE ********************************/
 
@@ -141,35 +154,46 @@
 
 void serial_read()                                                                  // Function for serial read for select LED intensity and colour.
 {
-    if (pi_serial.readable()) {                                                     // Function to check if pi is readable.
-        char message[10];
-        pi_serial.scanf("%s", message);
-        
-        if (test_mode == 1) {                                                       // If statement for test purposal. 
-            usb_serial.printf("Message = %s, Intensity_select = %d en LED_colour = %c\n", message, intensity_select, LED_colour);
-        }
-        
-        if (intensity_select != (message[0]-'0')) {                                 // Read intensity for LED's variable from PI. 
-            intensity_select = (message[0]-'0');
-        }
+    intensity_select = intensity_code;
+    colour_code = (colour_code_1 << 1 | colour_code_0);
+    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;
+    }
+    //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 (LED_colour != message[1]) {                                             // Read character from PI to set LED_colour.
-            LED_colour = message[1];
-        }
+    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 == 1) {
-            usb_serial.printf("Message: %s\n", message);
-            usb_serial.printf("Intensity_select = %d en LED_colour = %c\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);
+
     }
 }
 
 void serial_log()                                                                   // Function for serial logging. See link to table with code declarations above in code.
 {
-    if (mute_flag == 1) {                                                           // If statement to control logging for mute button. 
+    if (mute_flag == 1) {                                                           // If statement to control logging for mute button.
         pi_serial.printf(">01\n");
 
-        if (test_mode == 1) {                                                       // If statement for test purposal. 
+        if (test_mode == 1) {                                                       // If statement for test purposal.
             usb_serial.printf(">01\n");
         }
 
@@ -179,27 +203,27 @@
     if (new_patient_flag == 1) {                                                    // If statement to control logging for new patient button.
         pi_serial.printf(">03\n");
 
-        if (test_mode == 1) {                                                       // If statement for test purposal. 
+        if (test_mode == 1) {                                                       // If statement for test purposal.
             usb_serial.printf(">03\n");
         }
 
         new_patient_flag = 0;
     }
 
-    if (reposition_flag == 1) {                                                     // If statement to control logging for reposition button. 
+    if (reposition_flag == 1) {                                                     // If statement to control logging for reposition button.
         pi_serial.printf(">02\n");
 
-        if (test_mode == 1) {                                                       // If statement for test purposal. 
+        if (test_mode == 1) {                                                       // If statement for test purposal.
             usb_serial.printf(">02\n");
         }
 
         reposition_flag = 0;
     }
 
-    if (batteryvoltage_current != batteryvoltage_last) {                            // If statement to control logging for batteryvoltage.           
+    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. 
+        if (test_mode == 1) {                                                       // If statement for test purposal.
             usb_serial.printf("%%" "%d\n", batteryvoltage_current);
         }
 
@@ -214,7 +238,7 @@
                 usb_serial.printf("&04\n");
             }
         }
-        
+
         if (LED_red_state == 0) {
             pi_serial.printf("&40\n");
             LED_red_logged = LED_red_state;
@@ -225,7 +249,7 @@
     }
 
     if (LED_yellow_logged != LED_yellow_state) {                                    // If statement to control logging for LED_yellow.
-        if (LED_yellow_state == 1) {    
+        if (LED_yellow_state == 1) {
             pi_serial.printf("&06\n");
             LED_yellow_logged = LED_yellow_state;
             if (test_mode == 1) {
@@ -245,36 +269,36 @@
         if (LED_green_state == 1) {
             pi_serial.printf("&05\n");
             LED_green_logged = LED_green_state;
-        
+
             if (test_mode == 1) {
                 usb_serial.printf("&05\n");
             }
         }
-        
+
         if (LED_green_state == 0) {
             pi_serial.printf("&50\n");
             LED_green_logged = LED_green_state;
-        
+
             if (test_mode == 1) {
                 usb_serial.printf("&50\n");
             }
         }
     }
 
-    if (speaker_logged != speaker_state) {                                          // If statement to control logging for speaker. 
+    if (speaker_logged != speaker_state) {                                          // If statement to control logging for speaker.
         if (speaker_state == 1) {
             pi_serial.printf("&07\n");
             speaker_logged = speaker_state;
-            
+
             if (test_mode == 1) {                                                   // If statement for test purposal.
                 usb_serial.printf("&07\n");
             }
         }
-        
+
         if (speaker_state == 0) {
             pi_serial.printf("&70\n");
             speaker_logged = speaker_state;
-            
+
             if (test_mode == 1) {                                                   // If statement for test purposal.
                 usb_serial.printf("&70\n");
             }
@@ -284,27 +308,27 @@
     if (power_plug_logged != power_plug_state) {                                    // If statement to control the logging for the state of the power plug.
         if (power_plug_state == 1) {
             pi_serial.printf("#08\n");
-            
+
             if (test_mode == 1) {                                                   // If statement for test purposal.
                 usb_serial.printf("#08\n");
             }
             power_plug_logged = power_plug_state;
         }
-        
+
         if (power_plug_state == 0) {
             pi_serial.printf("#80\n");
-            
+
             if (test_mode == 1) {                                                   // If statement for test purposal.
                 usb_serial.printf("#80\n");
             }
             power_plug_logged = power_plug_state;
         }
     }
-    
-    if (connection_test_sensorplate == 1) {                                                     // If statement for sending serial information sensorplate data when connection test is active. 
-        // Receiving order sensor information: 8 resistive sensors, 5 electric readings. Is splitted in two parts - part 1/2. 
+
+    if (connection_test_sensorplate == 1) {                                                     // If statement for sending serial information sensorplate data when connection test is active.
+        // Receiving order sensor information: 8 resistive sensors, 5 electric readings. Is splitted in two parts - part 1/2.
         pi_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[0], piezo_resistive_array[1], piezo_resistive_array[2], piezo_resistive_array[3], piezo_resistive_array[4], piezo_resistive_array[5], piezo_resistive_array[6], piezo_resistive_array[7], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4]); // print all to serial port
-        
+
         if (test_mode == 1) {
             usb_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[0], piezo_resistive_array[1], piezo_resistive_array[2], piezo_resistive_array[3], piezo_resistive_array[4], piezo_resistive_array[5], piezo_resistive_array[6], piezo_resistive_array[7], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4]); // print all to serial port
         }
@@ -312,24 +336,53 @@
 
 }
 
-void colour_select_indicating_LED_wall(char LED_colour)                             // Function to select the colour for LED's to wall (values comes from algorithm).
+void colour_select_indicating_LED_wall(char nLED_colour)                             // Function to select the colour for LED's to wall (values comes from algorithm).
 {
     set_intensity_LEDs();                                                           // Call function set_intensity_LEDs to set the intensity for LED's to wall and above buttons.
-    
+    ws.setII(2.55*intensity);
+    switch(nLED_colour) {
+        case 'r' :
+            px.SetAll(colourbuf[0]);
+            break;
+        case 'g' :
+            px.SetAll(colourbuf[1]);
+            break;
+        case 'b' :
+            px.SetAll(colourbuf[2]);
+            break;
+        case 'y' :
+            px.SetAll(colourbuf[3]);
+            break;
+        default  :
+            px.SetAll(colourbuf[4]);
+    }
+
+    if (calibration_flash >= 1) {
+        if ((calibration_flash % 2) == 0) {
+            px.SetAll(colourbuf[4]);
+        } else {
+            ws.setII(0);
+        }
+        calibration_flash--;
+    }
+    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_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_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);
@@ -338,7 +391,7 @@
         } else {
             LED_green_state = 0;
         }
-    
+
         if (LED_colour == 'g') {                                                    // Set LED_colour to green.
             LED_red_intensity = 0;
             LED_green_intensity = (2.55*intensity);
@@ -347,7 +400,7 @@
         } else {
             LED_green_state = 0;
         }
-    
+
         if (LED_colour == 'b') {                                                    // Set LED_colour to blue.
             LED_red_intensity = 0;
             LED_green_intensity = 0;
@@ -355,7 +408,7 @@
         }
     }
 
-    if (calibration_flash >= 1) {                                                   // If statement for flashing LED's (colour = white) when calibration is active. 
+    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;
@@ -369,6 +422,7 @@
         }
         calibration_flash--;
     }
+    */
 }
 
 void trigger_lock()                                                                 // If rising edge lock button is detected start locktimer.
@@ -376,7 +430,7 @@
     if (test_mode == 1) {
         usb_serial.printf("Lock triggered.\n");
     }
-    
+
     button_lock_hold_timer.reset();
     button_lock_hold_timer.start();
     delay_between_button_pressed.reset();
@@ -385,7 +439,7 @@
 
 void end_timer_lock_button()                                                        // End timer lock.
 {
-    if (test_mode == 1) {                                                           // If statement for test purposal. 
+    if (test_mode == 1) {                                                           // If statement for test purposal.
         usb_serial.printf("Lock released.\n");
     }
     lock_flag = 0;                                                                  // Set lock_flag off.
@@ -399,7 +453,7 @@
     } else {
         delay_between_button_pressed.reset();
         delay_between_button_pressed.start();
-        if (test_mode == 1) {                                                       // If statement for test purposal. 
+        if (test_mode == 1) {                                                       // If statement for test purposal.
             usb_serial.printf("Reposition triggered.\n");
             LED_on_dev_board1 = !LED_on_dev_board1;
         }
@@ -409,9 +463,9 @@
     }
 }
 
-void rise_reposition()                                                              // Interrupt for rising edge reposition function (deactivation; active low). 
+void rise_reposition()                                                              // Interrupt for rising edge reposition function (deactivation; active low).
 {
-    if (test_mode == 1) {                                                           // If statement for test purposal. 
+    if (test_mode == 1) {                                                           // If statement for test purposal.
         usb_serial.printf("Reposition released.\n");
     }
     reposition_feedback_LED = 0;
@@ -426,14 +480,14 @@
         delay_between_button_pressed.reset();
         delay_between_button_pressed.start();
         mute_state = !mute_state;
-        
+
         if (mute_state == 1) {                                                      // If statement for if mute_state is active, set mute feedback LED active.
             mute_feedback_LED = control_LED_intensity;
         } else {
             mute_feedback_LED = 0;
         }
-        
-        if (test_mode == 1) {                                                       // If statement for test purposal. 
+
+        if (test_mode == 1) {                                                       // If statement for test purposal.
             usb_serial.printf("Mute triggered %d.\n",mute_state);
             LED_on_dev_board1 = !LED_on_dev_board1;
         }
@@ -450,8 +504,8 @@
         button_calibration_hold_timer.reset(); // inline ?
         button_calibration_hold_timer.start();
         new_patient_feedback_LED = control_LED_intensity;;
-        
-        if (test_mode == 1) {                                                       // If statement for test purposal. 
+
+        if (test_mode == 1) {                                                       // If statement for test purposal.
             usb_serial.printf("New patient triggered.\n");
         }
     }
@@ -459,7 +513,7 @@
 
 void activate_new_patient_function()                                                            // Timer calibration function.
 {
-    if (test_mode == 1) {                                                           // If statement for test purposal. 
+    if (test_mode == 1) {                                                           // If statement for test purposal.
         usb_serial.printf("New patient released.\n");
     }
     new_patient_feedback_LED = 0;
@@ -468,14 +522,14 @@
         new_patient_flag = 1;
     }
 
-    button_calibration_hold_timer.stop();                                           // Timer reset for calibration function of new patient button. 
+    button_calibration_hold_timer.stop();                                           // Timer reset for calibration function of new patient button.
     button_calibration_hold_timer.reset();
 
     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.
     } else {
         if (calibration_flag == 0) {
 
-            if (LED_on_dev_board1 == 0) {                                           // If statement for test purposal. 
+            if (LED_on_dev_board1 == 0) {                                           // If statement for test purposal.
                 LED_on_dev_board1 = 1;
             } else {
                 LED_on_dev_board1 = 0;
@@ -487,14 +541,14 @@
     }
 }
 
-void timer_functions()                                                              // Function which contains statements using timers. 
+void timer_functions()                                                              // Function which contains statements using timers.
 {
     if ((button_lock_hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && button_lock == 0) { // If statement for lock function.
         lock_flag = 1;
         LED_on_dev_board2 = !LED_on_dev_board2;
         lock_state = !lock_state;
-        
-        if (lock_state == 0) {                                                      // If statement to control lock feedback LED above button. 
+
+        if (lock_state == 0) {                                                      // If statement to control lock feedback LED above button.
             lock_feedback_LED = control_LED_intensity;
         } else {
             lock_feedback_LED = 0;
@@ -504,11 +558,11 @@
     if ((button_calibration_hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && button_new_patient == 0 && lock_state == 0) { // If statement for calibration algorithm.
         calibration_flag = 1;
         calibration_flash = 11;
-        
-        if (test_mode == 1) {                                                       // If statement for test purposal. 
+
+        if (test_mode == 1) {                                                       // If statement for test purposal.
             usb_serial.printf("Calibrate triggered.\n");
         }
-        
+
         pi_serial.printf(">30\n");                                                  // Print statement for serial communication to inform algorithm to calibrate.
     }
 
@@ -519,12 +573,12 @@
     }
 }
 
-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 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).
 {
@@ -555,55 +609,53 @@
     }
 
 
-    if (alarm == 1 && mute_state == 1 && (batteryvoltage_current > alarm_voltage)) {// Set speaker on for 750 ms. Use PWM? => Split in more functions. 
+    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.
-        speaker2 = 0;
     }
 
     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.
-        speaker2 = 1;
         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).
-        speaker2 = 0;
     }
 
     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, 
+    // 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. 
+    if (powervoltage_current < digital_value_ADC_powervoltage_unplugged) {          // If statement to set LED's to blue.
         power_plug_state = 0;
-        LED_colour = 'b';
+        //LED_colour = 'b';
     } else {
         power_plug_state = 1;
     }
-}   
+}
 
-void read_adc() {
+void read_adc()
+{
     return;
     piezo_electric_sample_timer.reset();                                            // Clock gebruiken o.i.d.?
     piezo_electric_sample_timer.start();
     connection_test_sensorplate = angle_device_sensorplate.testConnection();
-    
+
     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.
 
@@ -636,12 +688,12 @@
             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. 
+
+        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.
@@ -652,31 +704,31 @@
     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.     
+    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. 
+    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. 
-    
+
+    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());
     }
@@ -691,86 +743,88 @@
 
     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. 
+    }
+    // * End of if statements for test purposal.
 }
 
-int main() {                                                                        // Main function. inline function "Momo Init" bijvoorbeeld
+int main()                                                                          // Main function. inline function "Momo Init" bijvoorbeeld
+{
     wait_ms(boot_delay_ms);                                                         // Wait to boot sensorplate first.
     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. 
+    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);                                     
+    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. 
+    pi_serial.format(8, SerialBase::None, 1);                                       // Set serial communication line with PI.
 
-    button_lock.mode(PullUp);   
-    button_reposition.mode(PullUp);   
-    button_mute.mode(PullUp);   
-    button_new_patient.mode(PullUp);   
-    
+    button_lock.mode(PullUp);
+    button_reposition.mode(PullUp);
+    button_mute.mode(PullUp);
+    button_new_patient.mode(PullUp);
+
     button_lock.fall(&trigger_lock);                                                // Interrupt for rising edge lock button.
     button_lock.rise(&end_timer_lock_button);
     button_reposition.fall(&reposition_button_triggered);
     button_reposition.rise(&rise_reposition);
     button_mute.fall(&mute_button_triggered);
     button_new_patient.fall(&trigger_new_patient);                                  // New patient/calibration button rising event.
-    button_new_patient.rise(&activate_new_patient_function);                                    // Falling edge for calibration algorithm option.
+    button_new_patient.rise(&activate_new_patient_function);                        // Falling edge for calibration algorithm option.
     delay_between_button_pressed.reset();                                           // Delaytimer reset en start.
     delay_between_button_pressed.start();
 
-    set_intensity_LEDs();                                                                // Initialize intensity for user interface LED's and LED's shines to wall.  
+    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.
 
-
-//    total_readout_cycle.attach_us(&read_adc, total_readout_cycle_time_us);          // Call function to start reading sensorplate and other functionalities.
+//    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.
 
     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 = angle_device_sensorplate.testConnection();
-        
+
         if (test_mode == 1) {
             usb_serial.printf("Connection test sensorplate = %d\n", connection_test_sensorplate);
         }
-        if (test_mode == 0) {
+        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.
             }
-            if (test_mode == 0) {
+            if (test_mode == 1) {
                 usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
-            } 
+            }
             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 (test_mode == 0) {
+            if (test_mode == 1) {
                 usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
-            } 
-            
+            }
+
             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) {
@@ -779,64 +833,68 @@
                 angle = accelerometer_sensorplate[2]*100;
             }
             angle_device_sensorplate.getGyro(gyroscope_sensorplate);                        // Get gyroscope data.
-            if (test_mode == 0) {
+            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 (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. 
+
+            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.
         }
-        if (test_mode == 0) {
+        if (test_mode == 1) {
             usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
-        } 
+        }
         timer_functions();
-        if (test_mode == 0) {
-            usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
-        } 
+        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 == 0) {
-            usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
-        } 
+        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.     
+        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. 
+
+        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.
 
-        if (test_mode == 0) {
+        if (test_mode == 1) {
             usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
-        } 
-        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).
+        }
+
+        if (test_mode == 0) {                                                            // If statements for test purposal (untill * mark).
+            usb_serial.printf("Loop time pre serial: %d ms\n",piezo_electric_sample_timer.read_ms());
+        }
+        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 == 0) {                                                            // 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) {
@@ -847,9 +905,5 @@
 //            test_mode = 0;
 //            usb_serial.printf("%d\n",test_mode);
 //        }
-    
-        if (test_mode == 0) {
-            usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
-        }  
     }
 }
\ No newline at end of file