STM32L4 and accelerometer program

223 views Asked by At

I'm trying to make program for service of MEMS accelerometer. I'm using three Pins of Port E of joystick (joy_left, joy_right, joy_down) for change of axis x, y and z. The end goal of the program is to show on 4 digits 7-segments displayer:

  • first digit representing one of axis (x, y, z) with usage of number from range from 1 to 3
  • digits from the second to the last must represent three digits of accelerometer value for it's axis

My code in main.c:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "kamami_l496_mems.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

/* USER CODE BEGIN PFP */
void switchInit(void);
void baseTransistInit(void);
void segmentsInit();
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  __HAL_RCC_GPIOG_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */
  HAL_PWREx_EnableVddIO2();
  switchInit();
  baseTransistInit();
  segmentsInit();
  mems_init(MEMS_ACC_DATARATE_10HZ, MEMS_ACC_FULLSCALE_2G);

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */
  SysTick_Config(SystemCoreClock / 1000); // SysTick_Handler() called within 1ms

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */



void switchInit(void)
{
    GPIO_InitTypeDef Joys;
    Joys.Mode = GPIO_MODE_INPUT;
    Joys.Pin = GPIO_PIN_1 /* JOY_LEFT */ | GPIO_PIN_0 /* JOY_RIGHT */ | GPIO_PIN_3 /* JOY_DOWN*/;
    Joys.Pull = GPIO_PULLUP;
    Joys.Speed = GPIO_SPEED_LOW;
    Joys.Alternate = 0;
    HAL_GPIO_Init(GPIOE, &Joys);
}

void baseTransistInit(void)
{
    GPIO_InitTypeDef Transistors;
    Transistors.Mode = GPIO_MODE_OUTPUT_PP;
    Transistors.Pin = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
    Transistors.Pull = GPIO_NOPULL;
    Transistors.Speed = GPIO_SPEED_LOW;
    Transistors.Alternate = 0;
    HAL_GPIO_Init(GPIOB, &Transistors);
}

void segmentsInit()
{
    GPIO_InitTypeDef Segments;
    Segments.Mode = GPIO_MODE_OUTPUT_PP;
    Segments.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_9;
    Segments.Pull = GPIO_NOPULL;
    Segments.Speed = GPIO_SPEED_LOW;
    Segments.Alternate = 0;
    HAL_GPIO_Init(GPIOG, &Segments);
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

I'm trying to use SysTick interrupt, so I'll show code in file "stm32l4xx_it.c here:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32l4xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32l4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "kamami_l496_mems.h"
#include  <string.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define SEG_A 0
#define SEG_B 1
#define SEG_C 2
#define SEG_D 3
#define SEG_E 4
#define SEG_F 5
#define SEG_G 6
#define SEG_DP 9
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
int buttonMs = 0;
int buttonDelay = 500;
const uint16_t  tableOfSegments[] =  {
        1 << SEG_A | 1 << SEG_B | 1 << SEG_C | 1 << SEG_D | 1 << SEG_E | 1 << SEG_F,              // DIGIT 0
        1 << SEG_B | 1 << SEG_C,                                                                  // DIGIT 1
        1 << SEG_A | 1 << SEG_B | 1 << SEG_E | 1 << SEG_D | 1 << SEG_G,                           // DIGIT 2
        1 << SEG_A | 1 << SEG_B | 1 << SEG_C | 1 << SEG_D | 1 << SEG_G,                           // DIGIT 3
        1 << SEG_B | 1 << SEG_C | 1 << SEG_G | 1 << SEG_F,                                        // DIGIT 4
        1 << SEG_A | 1 << SEG_F | 1 << SEG_G | 1 << SEG_C | 1 << SEG_D,                           // DIGIT 5
        1 << SEG_A | 1 << SEG_F | 1 << SEG_G | 1 << SEG_C | 1 << SEG_D | 1 << SEG_E,              // DIGIT 6
        1 << SEG_A | 1 << SEG_B | 1 << SEG_C,                                                     // DIGIT 7
        1 << SEG_A | 1 << SEG_B | 1 << SEG_C | 1 << SEG_D | 1 << SEG_E | 1 << SEG_F | 1 << SEG_G, // DIGIT 8
        1 << SEG_A | 1 << SEG_B | 1 << SEG_C | 1 << SEG_D | 1 << SEG_F | 1 << SEG_G,              // DIGIT 9

};
int showX = 1;
int showY = 0;
int showZ = 0;

float x, y, z;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

void inputHandling();
void makeDigitFromFloatValue(int pos, float input, int * digitToSet);
void showDigit(const char * digitName);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
struct mems_xyz_res acc; // structure for accelerometr results
/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/

/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M4 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Prefetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */
  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
  static int miliSec = 0;
  inputHandling();
  int digitOf4Sec = miliSec % 4;
  switch(digitOf4Sec)
  {
      case 0:
          showDigit("axis");
          break;
      case 1: // digit 2
          showDigit("first");
          break;
      case 2: // digit 3
          showDigit("second");
          break;
      case 3: // digit 4
          showDigit("third");
          break;
  }
  if(miliSec != 0)
  {
      if(miliSec / 1000 == 4)
          miliSec = 0;
      if(miliSec % 1000 == 0)
      {

          x = acc.x * 2.0f / MEMS_ACC_MAXVAL;
          y = acc.y * 2.0f / MEMS_ACC_MAXVAL;
          z = acc.z * 2.0f / MEMS_ACC_MAXVAL;
      }
  }
  mems_acc_read_xyz(&acc);
  miliSec++;

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32L4xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32l4xx.s).                    */
/******************************************************************************/

/* USER CODE BEGIN 1 */
void inputHandling()
{
    buttonMs++;
    if(buttonMs < buttonDelay)
        return;
    if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_0) == GPIO_PIN_RESET) //      JOY RIGHT, Y
    {
        showX = 0;
        showY = 1;
        showZ = 0;
    }
    else if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_1) == GPIO_PIN_RESET) // JOY LEFT,  X
    {
        showX = 1;
        showY = 0;
        showZ = 0;
    }
    else if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_2) == GPIO_PIN_RESET) // JOY DOWN,  Z
    {
        showX = 0;
        showY = 0;
        showZ = 1;
    }
    if(buttonMs == buttonDelay)
        buttonMs = 0;

}

void makeDigitFromFloatValue(int pos, float input, int * digitToSet)
{
    if(input < 0)
    {
        input *= -1;
    }

    int number = input * 100;
    if(input < 10)
    {
        if(pos == 1)
            *digitToSet = 0;
        else if(pos == 2)
            *digitToSet = 0;
        else
            *digitToSet = number % 10;
    }
    else if(input < 100)
    {
        if(pos == 1)
            *digitToSet = 0;
        else if(pos == 2)
        {
            number /= 10;
            *digitToSet = number % 10;
        }
        else
            *digitToSet = number % 10;
    }

}

void showDigit(const char * digitName)
{
    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_9, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5, GPIO_PIN_RESET);
    if(strcmp(digitName, "axis") == 0)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
        if(showX)
        {
            HAL_GPIO_WritePin(GPIOG, tableOfSegments[1], GPIO_PIN_SET);
        }
        else if(showY)
        {
            HAL_GPIO_WritePin(GPIOG, tableOfSegments[2], GPIO_PIN_SET);
        }
        else if(showZ)
        {
            HAL_GPIO_WritePin(GPIOG, tableOfSegments[3], GPIO_PIN_SET);
        }
        return;
    }
    int digitToShow;
    if(strcmp(digitName, "first") == 0)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
        makeDigitFromFloatValue(1, x, &digitToShow);
    }
    else if(strcmp(digitName, "second") == 0)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
        makeDigitFromFloatValue(2, x, &digitToShow);
    }
    else if(strcmp(digitName, "third") == 0)
    {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
        makeDigitFromFloatValue(3, x, &digitToShow);
    }
    HAL_GPIO_WritePin(GPIOG, tableOfSegments[digitToShow], GPIO_PIN_SET);
}


/* USER CODE END 1 */

At the moment I encountered following problem:

  • only the first digit is showing responsible for axis. No other digit beyond that. Changing axis with joystick doesn't work also. I'm guessing it's because of line in SysTickHandler interrupt function:
mems_acc_read_xyz(&acc);

User @atune correctly pointed to the fact that getting data for accelerate values take longer than 1ms and that is in collision with SysTick interrupt which is called every 1ms. With the task I'm having I can't add anything to infinite loop of main() function, so I need to do it with the usage of interrupts only. I moved:

mems_acc_read_xyz(&acc);

to the if, where x, y and z are initialised with accelerate values:

      if(miliSec % 1000 == 0)
      {
          mems_acc_read_xyz(&acc);
          x = floorf((acc.x * 2.0f / MEMS_ACC_MAXVAL) * 100) / 100; //acc.x * 2.0f / MEMS_ACC_MAXVAL;
          y = floorf((acc.y * 2.0f / MEMS_ACC_MAXVAL) * 100) / 100; //acc.y * 2.0f / MEMS_ACC_MAXVAL;
          z = floorf((acc.z * 2.0f / MEMS_ACC_MAXVAL) * 100) / 100; //acc.z * 2.0f / MEMS_ACC_MAXVAL;
      }

Now change of axis is showing and accelerate values also. But are these correct? When I'm moving board i'm getting values for each axises, but only from 0.00 to 0.09. I think something is wrong also here, and max range should be mich higher? Is that problem connected to my function makeDigitFromFloatValue(int pos, float input, int * digitToSet)?

Thank you in advance for all your answers.

1

There are 1 answers

4
atune On

First of all you should go through your code and think what each line does. Perhaps first think of what you want the code to do, and also document it by commenting your intention in the code.

Just at a glance, in your example stm32l4xx_it.c, first SysTick_Handler:

static int miliSec = 0;

miliSec is a signed integer, it can become a negative value. Keep that in mind if you decide to do keep it incrementing until it overflows.

You take the remainder of dividing miliSec by 4 into digitOf4Sec. In other words digitOf4Sec can only be 0, 1, 2 or 3.

digitOf4Sec = miliSec % 4;
// 0 % 4 = 0
// 1 % 4 = 1
// 2 % 4 = 2
// 3 % 4 = 3
// 4 % 4 = 0
// 5 % 4 = 1
// etc.

Then you use digitOf4Sec and only enter your switch-case if digitOf4Sec divided by 4 is 0. It is always 0 because of integer truncation.

// (digitOf4Sec CAN ONLY BE 0, 1, 2, or 3 like stated before)
// 0 / 4 = 0
// 1 / 4 = 0
// 2 / 4 = 0
// 3 / 4 = 0
if((digitOf4Sec / 4) == 0)
{
    switch(digitOf4Sec)
    {
        // digitOf4Sec is always 0
        case 0:
            showDigit("axis");
        ...

Answer 1.) ...so in your switch-case you can never enter anything but case 0, because 'digitOf4Sec / 4' is always 0. Is this what is meant by "only the first digit is showing responsible for axis. No other digit beyond that."? If it is, there's your answer. The other cases are impossible.

Last you apparently want to clear the counter every 4 seconds and update the acceleration values every 1000 milliseconds?

if(miliSec != 0)
{
    if(miliSec / 1000 == 4)
        miliSec = 0;
    if(miliSec % 1000 == 0)
    {
        // x-y-z acceleration value update code
    }
}

miliSec++;

I don't understand the 4-second part, but I guess it works fine enough.

About the accelerometer values. I assume you're using KAmeleon-STM32L4 kit with LSM303C e-compass. In main.c you configure the accelerometer to be 2G full-scale. The accelerometer has 16-bit values so the range is -32768...32767 where -32767 is -2G, 0 is 0G and +32767 is +2G. In other words 1 LSB is (1/32767G) ~= 0.000061G.

So about your conversion code

x = acc.x * 2.0f / 32678.0f;
y = acc.y * 2.0f / 32678.0f;
z = acc.z * 2.0f / 32678.0f;

Answer 2.) It's ok and probably works just fine, but you have a typo. 32678 should be 32768 of course. Also it's likely more like 32767 but that's not a big issue, a drop in the bucket.

In the future you might want to consider making the values used explicit. What is 2.0, what is 32768.0, why are they there? What if you decide to change the full scale from 2G to 4G/8G or you want to test different ranges, do you always want to change the magic numbers by hand?

Oh and (Not-)Answer 3.) Changing axis with joystick doesn't work also. that one you need to think about yourself, it's not that easy to reason with your code, especially without the used hardware and the full picture. You need to debug your code and double-check what you're doing. By the way, is joystick down actually PE2. You're reading PE3 which is up?

Other comments:

a)

void showDigit(const char * digitName)

Why the c-string -based argument with only 4 valid values? The string isn't coming as plaintext from anywhere. You should probably use an enum instead.

b) In inputHandling(), you should clear buttonMs always when you've reached the delay, not just when the joystick's pressed to some direction.

Anyhoo that's all I got. There's a lot of info missing, it's hard to debug your program by just sight-reading. Mostly because it's not obvious what even is happening and what you want to happen.