<commit_before>/*
 * Indicator Light for Motor Speed Setpoint:
 * Measures angular velocity of encoder in degrees per second (moving average over every 250ms)
 * and compares to arbitrary speed setpoint value. Lights up LED when difference is larger than arbitrary threshold.
 */

#define encoder0PinA  2
#define encoder0PinB  3
#define ledPin 13
#define errorThreshold 20
#define cpr 200 // Cycles per revolution of encoder disk

int targetSpeed = 40; // Target angulary velocity / setpoint in degrees per second

volatile long encoder0Pos = 0; // Delta of encoder position since last loop iteration
long oldTime = 0; // Time in ms from last loop iteration
int dps = 0; // Angular velocity in degrees per second

void setup() {
  pinMode(encoder0PinA, INPUT);
  pinMode(encoder0PinB, INPUT);
  pinMode(ledPin, OUTPUT);

  attachInterrupt(digitalPinToInterrupt(encoder0PinA), doEncoderA, CHANGE);
  attachInterrupt(digitalPinToInterrupt(encoder0PinB), doEncoderB, CHANGE);

  oldTime = millis();
  
  Serial.begin(9600);
}

void loop() {
  // Divide delta of position by 4*cpr (because 4X decoding of quadrature encoder)
  // and multiply by 360 for change in degrees, divide by delta time, & convert from ms^-1 to s^-1.
  dps = ((encoder0Pos) * 360 * 1000 / (4*cpr)) / (millis() - oldTime);
  Serial.println(dps);
  if (abs(dps - targetSpeed) > errorThreshold) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("CONGRATS");
  }
  oldTime = millis();
  encoder0Pos = 0;
  delay(250);
}

void doEncoderA() {
  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
    encoder0Pos--;
  } else {
    encoder0Pos++;<commit_msg>Changed for consistency with MotorShield.ino<commit_after>/*
 * Indicator Light for Motor Speed Setpoint:
 * Measures angular velocity of encoder in degrees per second (moving average over every 250ms)
 * and compares to arbitrary speed setpoint value. Lights up LED when difference is larger than arbitrary threshold.
 */

const int encoder0PinA = 2;
const int encoder0PinB = 3;
const int ledPin = 13;
const float deadband = 20.0; // Error threshold/deadband in ticks per second

// Following constants are taken from MotorShield.ino
const float ticksPerRev = 200.0; // Cycles/ticks per revolution of encoder disk
const float gearRatio = 32.0;
const float wheelCir = 1.092; // Meters
const float metersPerTick = wheelCir / (ticksPerRev * gearRatio);

float targetSpeed = 40; // Target velocity/setpoint in m/s
float targetAngSpeed = targetSpeed / metersPerTick; // Target angular velocity/setpoint in ticks per second

volatile int encoder0Pos = 0; // Delta of encoder position since last loop iteration
long oldTime = 0; // Time in ms from last loop iteration
float angSpeed = 0; // Angular velocity in ticks per second

void setup() {
  pinMode(encoder0PinA, INPUT);
  pinMode(encoder0PinB, INPUT);
  pinMode(ledPin, OUTPUT);

  attachInterrupt(digitalPinToInterrupt(encoder0PinA), doEncoderA, CHANGE);
  attachInterrupt(digitalPinToInterrupt(encoder0PinB), doEncoderB, CHANGE);

  oldTime = millis();
  
  Serial.begin(9600);
}

void loop() {
  // Divide delta of position by 4 (because 4X decoding of quadrature encoder)
  // to get ticks from edges, divide by delta time & convert from ms^-1 to s^-1.
  angSpeed = encoder0Pos * 1000 / 4.0 / (millis() - oldTime);
  Serial.println(angSpeed);
  if (abs(angSpeed - targetAngSpeed) > deadband) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("CONGRATS");
  }
  oldTime = millis();
  encoder0Pos = 0;
  delay(250);
}

void doEncoderA() {
  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
    encoder0Pos--;
  } else {
    encoder0Pos++;
{'fraction_non_alphanumeric': 0.09908700322234157, 'fraction_numerical': 0.02953813104189044, 'mean_word_length': 4.130853994490358, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4015491', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1124, 'n_words': 398}