EMBEDDED PROGRAMMING

Final Project - Embedded programming - Sketch


Guide


  • Tests
  • Final sketch

What questions need to be answered?


  • Does the circuit design work as expected?
  • Does all the feed back ‘mechanisms’ work properly?
  • Does the input ‘mechanism’ work?
  • Does the sensor work as expected (at all)?
  • Is it possible to program all these to work together?

Software/Hardware used


  • Arduino IDE

Sketches


With continuity testing looking like everything connected properly, it comes down to programming and seeing if they are actually connected properly. These will be done in order of complexity and spiral to include all the features of the board.

Tests

  • Can it be programmed?

FINAL PROJECT

The basic function to get further with programming. Will it upload?

yes….too easy. A sketch containing no programming whatsoever, managed to turn on the Red and Green LED’s giving a constant yellow light (see below).

  • Motor

    A simple check if it vibrates. Nothing happened, even defining the pin as either 5 or A7 didn’t make a difference. (See issues electronics). Got it working eventually as pin A7. As the pin was both an Analogue/Digital pin, there would be the possibility to pass more subtle analogue values to the motor as well as including the Adafruit haptic library. Something for future development.

      void Haptic() {
        digitalWrite(MOTOR_pin, HIGH);
        delay(hapticDelay);
        digitalWrite(MOTOR_pin, LOW);
        delay(hapticDelay);
      }
    
  • RGB LED

    A simple ‘blink’ program shows that the Red and Green LED’s work individually, but not the Blue :(). (See issues electronics)

        void Blink(int whichLED) {
    
        // common anode - so LOW = on, HIGH = off.
        digitalWrite(whichLED, LOW);
        delay(LEDflash);
        digitalWrite(whichLED, HIGH);
      }
    
  • Button

A simple ‘digitalWrite’ instruction added to change modes or functionality.

     if (digitalRead(BTN_pin) == LOW) {

      } else {

        ChangeMode();

      }
  • 3 Axis Accelerometer Sensor

This takes several steps. Define the I2C address of the sensor and the various axis.

    /////// ADXL343 3 axis ///////
    #define BUFFER_SIZE 25
    #define ADXL343_ADDRESS                 (0x53)    // I2C ADXL343 ALT Address
    #define ADXL343_REG_DATAX0              (0x32)    // X-axis data 0
    #define ADXL343_REG_DATAY0              (0x34)    // Y-axis data 0
    #define ADXL343_REG_DATAZ0              (0x36)    // Z-axis data 0
    //////////////////////////////

Include the Wire library to allow i2C communication.

    ///////// INCLUDES ///////////
    // Library for I2C - to speak to sensor
    #include <Wire.h>

Open the I2C communications and register the sensor (with default settings). For more settings and greater control over the ADXL343_3 axis sensor you can install the Adafruit_ADXL343 library.

    //// I2C COMMS
      Wire.begin(); // join i2c bus (address optional for master)
      // register sensor
      uint8_t deviceId = adxl343ReadRegister(0x00);
      adxl343WriteRegister(0x2D, 0x08); // POWER_CTL register
      adxl343WriteRegister(0x31, 0x08); // Full res register

Collect the data and add it to the global variables.

    if (sensorCollectData == 1 ) {

        x = adxl343ReadWord(ADXL343_REG_DATAX0);
        y = adxl343ReadWord(ADXL343_REG_DATAY0);
        z = adxl343ReadWord(ADXL343_REG_DATAZ0);
        delay(50);
      }

Final sketch

Building on top of those tests above, several other functions were needed to make it all work together.

  • Interrupts

All the previous embedded programming i’ve done performed functions from start to finish and then back to the Loop cycle. By including an ‘interrupt’ to the button’s input, the cycle will stop what ever its doing and call another function instead of having to complete the cycle.

    attachInterrupt(digitalPinToInterrupt(BTN_pin), increaseMODUS, RISING);
  • Reverse numbers

The sensor would give values of between -512 and +512. To prepare those values for mapping to between 0 and 255x, I would have to make sure that ll the values were positive values (at least for the output functions i had decided upon). To to this, i use the simple trick of multiplying the value by ‘-1’.

    // reverse numbers
      if (x <= 0) {
        x = x * -1;
      }
      if (y <= 0) {
        y = y * -1;
      }
      if (z <= 0) {
        z = z * -1;
      }

Mistakes & Issues


Shared common anode - RGB LED

Doh! I forgot that this RGB LED has a shared anode - which means that turning them on an off is the other way around - HIGH = off, LOW = on!.

Blue LED not working

Yellow is a combination of Red and Green LEDs. Which means the Blue isn’t quite working right. Direct contact with the multimeter shows the LED to be working. But I can’t get a resistance reading from the resistor. I also re-checked to make sure the value is correct (it is). (See issues electronics)

Motor taking more current than expected

The idea was to combine visual and haptic feedback depending on the wearer’s behaviour. When testing this in the embedded programming, i found that the draw of current was too much for both the RGB LED’s and the motor to function at the same time. The strange results when i tried gave me the impression that the chip had ‘crashed’ just like a regular computer. And turning off the second form of feedback allowed the programming to work.

Button bounce

The good old button bounce! The interupt attached to the button worked really well. So well it would skip through the modes and i had difficulty returning the mode to its ‘off’ state. I did a little research and found some suggestions about setting a timer, delays or disabling interupts, but didn’t have anytime to implement/get the code working. Our electronics guru Erwin later suggested using a simple hardware fix instead of a complicated software solution. See Electronics issues for a more detailed explanation.

Z axis values always gives a value close to the middle

Upon testing the final sketch, i noticed that there wasn’t much variation in values coming from the Z axis. The sensor was registered to give values between -2g and +2g - however Z axis was giving relative values close to 1g all the time, where the other axes were able to give a value of 0g when stationary. There must be a simple physics explanation of this (its sensing gravity or something), but i didn’t have time to investigate. So I just subtracted 127 from any value it gave - which worked pretty well.


EVALUATION


Having dabbled in various scripting language for web development over the years, the basic programming concepts weren’t that new to me. What was new, was the different libraries used by the Arduino IDE to call different built in functions, controlling various things such as communication between the sensor etc. With that came the need to know the theory about how protocols and sensors work.

Its been an interesting process learning about embedded programming, all previous programming has been quite abstract, resulting in only creating things in the digital world. Whereas this bridges the gap from the digital world to the real world.


FURTHER DEVELOPMENT


Deep sleep mode - battery conservation

‘Putting it into deep sleep’ is a phrase i’ve heard when people were programming. As this circuit wouldn’t be used continually it would be a good idea to be able conserve power and extend the life of the battery as much as possible.

More feedback functions

It would be good to add more LED lighting programs and haptic signals based on the user’s behaviours, or desired therapeutic exercise. Depending on how much memory is available! See issues in Week13.


FILES


FILE DESCRIPTION
Arduino Sketch The Arduino sketch