GAHJj

        МаниПуляция -- дистанционное управление человеком
        посредством взяток (money) и принуждения (пуля).

1. Arduino Multifunctional Shield
=================================

Leftmost and Left   -- Stored Potentiometer value (Setup, HEX, 1Byte)
Right and Rightmost -- Current Potentiometer value (Loop, HEX, 1Byte)

      A            A            A            A
     ---          ---          ---          ---
  F |   | B    F |   | B    F |   | B    F |   | B
  G  ---  G    G  ---  G    G  ---  G    G  ---  G
  E |   | C    E |   | C    E |   | C    E |   | C
     ---          ---          ---          ---
      D            D            D            D

  0 (Leftmost) 1 (Left)     2 (Right)    3 (Rightmost)

  #include <EEPROM.h>

  /* Define shift register pins used for seven segment display */

  #define LATCH_DIO 4
  #define CLK_DIO 7
  #define DATA_DIO 8

  #define Pot1 0

  /* Segment byte maps for "0 - F" "A - Z" " " "_" "-" "="*/

  const byte MYFONT[] = {
      0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
      0x80, 0x90, 0x88, 0x83, 0xA7, 0xA1, 0x86, 0x8E,
      0xC2, 0x8B, 0xE6, 0xF2, 0x89, 0xC7, 0xC9, 0xAB,
      0xA3, 0x8C, 0x98, 0xAF, 0x93, 0x87, 0xC1, 0xE3,
      0x81, 0xEB, 0x91, 0xB6, 0xFF, 0xF7, 0xBF, 0xB7
  };

  /* Segment byte maps for numbers 0 to F */

  const byte SEGMENT_MAP[] = {
      0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90,
      0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
  };

  /* Byte maps to select digit 1 to 4 */
  const byte SEGMENT_SELECT[] = {0xF1,0xF2,0xF4,0xF8};

  void setup ()
  {
    /* Set DIO pins to outputs */
    pinMode(LATCH_DIO,OUTPUT);
    pinMode(CLK_DIO,OUTPUT);
    pinMode(DATA_DIO,OUTPUT);

    EEPROM.write(0, analogRead( Pot1)/4.);
  }

  /* Write a decimal number between 0 and 9 to one
     of the 4 digits of the display */

  void WriteNumberToSegment(byte Segment, byte Value)
  {
    digitalWrite(LATCH_DIO,LOW);
    shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_MAP[Value]);
    shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_SELECT[Segment] );
    digitalWrite(LATCH_DIO,HIGH);
  }

  /* Main program */
  void loop()
  {
    int PotValue;
    int Eeprom;
    Eeprom=EEPROM.read(0);
    PotValue = analogRead(Pot1)/4.;
    /* Update the display with the current counter value */
    WriteNumberToSegment(0 , (Eeprom / 16) % 16);
    WriteNumberToSegment(1 , Eeprom % 16);
    WriteNumberToSegment(2 , (PotValue / 16) % 16);
    WriteNumberToSegment(3 , PotValue % 16);
  }

2. Разборка и Сборка числа типа float на байты
==============================================

  #include <stdio.h>
  #include <stdlib.h>

  int bytes2float( unsigned char * abytes, float * afloat) {
      union {
          float f;
          unsigned int ui;
      } u;

      u.ui =
        (abytes[3] << 24) + (abytes[2] << 16) + (abytes[1] << 8) + abytes[0];

      // Debug:
      printf("%02x %02x %02x %02x -> %08x -> %f\n",
              abytes[3], abytes[2], abytes[1], abytes[0], u.ui, u.f);

      *afloat = u.f;

      return 0;
  }

  int float2bytes( float afloat, unsigned char abytes[]) {
      union {
          float f;
          unsigned int ui;
      } u;
      int i;

      u.f = afloat;

      for (i=0; i<4; i++)
          abytes[i] = (u.ui & 0xff << i*8) >> i*8;

      // Debug:
      printf("%f -> %08x -> %02x %02x %02x %02x\n",
              u.f, u.ui, abytes[3], abytes[2], abytes[1], abytes[0]);

      return 0;
  }

  int main()
  {
      float fin = 7.13, fout;
      unsigned char bytes[4];

      float2bytes( fin, bytes);
      bytes2float( bytes, &fout);

      // Debug
      printf("fin=%f fout=%f\n", fin, fout);

      return 0;
  }

Links
-----

  http://arduinolearning.com/code/multi-function-shield-examples.php

  https://www.arduino.cc/en/Tutorial/EEPROMWrite
  #include <EEPROM.h>
  ...
  EEPROM.write(addr, val);

  https://www.arduino.cc/en/Tutorial/EEPROMRead
  #include <EEPROM.h>
  ...
  EEPROM.read(addr);

