Skip to content

100 khz misses data? #3

@JessicaMulein

Description

@JessicaMulein

My code only updates the display when the twist dial is moved, and it sometimes misses bytes or something and the display updates incorrectly or does not update. A second update fixes it.
Artemis Nano BLE.
What can I do to improve this? speed 10,000 doesn't work.

#include <Wire.h>
#include "SparkFun_Qwiic_Twist_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_Twist
#include "Qwiic_LED_Stick.h" // Click here to get the library: http://librarymanager/All#SparkFun_Qwiic_LED_Stick

LED LEDStick;
TWIST twist;

const byte twistInterruptPin = 4; //Can be any GPIO

void setup() {
  Wire.begin();
  Wire.setClock(100000);
  Serial.begin(115200);

  pinMode(twistInterruptPin, INPUT_PULLUP);

  if(twist.begin() == false)
  {
    Serial.println("Twist does not appear to be connected. Please check wiring. Freezing...");
    while(1);
  }

  int currentVersion = twist.getVersion();

  if (currentVersion < 0x0201) //v1.2 in two byte form
  {
    Serial.print("The current firmware version is: ");
    Serial.print(currentVersion & 0xFF);
    Serial.print(".");
    Serial.println(currentVersion >> 8);

    Serial.println("This feature is not supported. Please consider upgrading the firmware on your Qwiic Twist. Freezing.");
    while (1); //Freeze
  }

  twist.setLimit(100); 
  twist.connectRed(0);
  twist.connectGreen(0);
  twist.connectBlue(0);
  twist.setColor(0, 0, 0); //Set Red and Blue LED brightnesses to half of max.

    //Start up communication with the LED Stick
  if (LEDStick.begin() == false){
    Serial.println("Qwiic LED Stick failed to begin. Please check wiring and try again!");
    while(1);
  }
  LEDStick.LEDOff();
}

void setArray(uint8_t* arr, int twistFraction) {
  for (int i=0; i<10; i++) {
    arr[i] = twistFraction >= i ? 255 : 0;
  }
}

void loop() {
  if (digitalRead(twistInterruptPin) == LOW)
  {
    int twistCount = twist.getCount();
    int twistFraction = (twistCount/10);
  
    Serial.print("Count: ");
    Serial.print(twistCount);
    Serial.println();
    Serial.print("Fraction: ");
    Serial.print(twistFraction);
    Serial.println();


    if(twist.isMoved())
    {
      Serial.print("Moved Count:");
      Serial.print(twistCount);
      Serial.println();

      twist.clearInterrupts(); //Clear any interrupt bits
      twist.setColor(0, 0, (twistCount/100)*255);
      byte redArray[10] = {0,0,0,0,0,0,0,0,0,0};
      byte blueArray[10] = {0,0,0,0,0,0,0,0,0,0};
      byte greenArray[10] = {0,0,0,0,0,0,0,0,0,0};

      setArray(blueArray, twistFraction);
      LEDStick.setLEDColor(redArray, greenArray, blueArray, 10);
    }

    if(twist.isPressed())
    {
      Serial.print("Pressed");
    }

    if(twist.isClicked())
    {
      Serial.print("Clicked!");
    }

    Serial.println();
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions