Good Day all.
iam new to using the Every
I have this code which runs fine on the Nano but I wanted to run the Every for a higher clock speed.
but when I upload it the display doesnt work, I Think the interrups are causing an issue.
Can someone explain whats wrong?
Thank you
Brendan
#include <TM1638.h>
#include <TM16xxDisplay.h>
#include <TM16xxButtons.h>
#include "HX711.h"
#include <Encoder.h>
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 4;
const int LOADCELL_SCK_PIN = 5;
// HX711 calibration and setup
#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
HX711 scale;
//Pins for Encoder
Encoder Wheelencoder(2, 3);
//Pins for LED and Keys display
const int STB = 10; // Strobe pin
const int CLK = 11; // Clock pin
const int DIO = 12; // Data pin
TM1638 module(DIO, CLK, STB);
TM16xxDisplay display(&module, 8); // TM16xx object, 8 digits
TM16xxButtons buttons(&module); // TM16xx object
float Diameter = 1; // 88.9; // tire radius (in mm)- CHANGE THIS
float circumference;
int keynumber = 16;
int keycount = 0;
bool running = false;
unsigned long myTime = 0;
// for RPM calculation
int interval = 1000; //time between calculations of rpm
long currentMillis = 0;
long previousMillis = 0;
int rpm = 0;
long previousWheelCount = 0;
unsigned long WheelCount = 0;
int EncoderCount = 1024; //Encoder Steps per full revolution
float mph = 0.00;
float MaxSpeed = 0.00;
float AverageSpeed = 0.00;
float AverageSpeedSum = 0.00;
int AverageCounter = 1;
float distance = 0.00;
void setup() {
Serial.begin(9600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
circumference = 3.14 * Diameter / 1000; // /1000 to convert to metres
module.setupDisplay(true, 2); // on=true, intensity-2 (range 0-7)
module.clearDisplay();
module.setDisplayToString("brendan");
delay(1000);
buttons.attachClick(fnClick);
}
void loop() {
buttons.tick();
int nTime = (((millis() - myTime) / 1000) / 60) * 100 + ((millis() - myTime) / 1000) % 60; // convert time to minutes+seconds as integer
//update values every second
currentMillis = millis();
Serial.print("Reading: ");
Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
Serial.print(" kgs"); //You can change this to lbs but you'll need to refactor the calibration_factor
Serial.println();
if (currentMillis - previousMillis > interval) { //has inveryal elapsed if yes then calculate RPM
//calculate RPM
WheelCount = Wheelencoder.read();
rpm = (float)((WheelCount - previousWheelCount) * 60 / EncoderCount); //i.e.number of pulses per second * 60 divided by pulses per revolution
previousMillis = currentMillis; //reset previous millis time
previousWheelCount = WheelCount; //reset previous encloder value
// Calculate speeds
mph = Diameter * rpm;
if (mph > MaxSpeed) MaxSpeed = mph; // record max speed
AverageSpeed = (mph + AverageSpeedSum) / AverageCounter; // record Avg speed
++AverageCounter;
// Calculate Distance
distance = WheelCount * circumference;
}
switch (keynumber) {
case 16:
running = !running; // invert the running tag from false to true or vise versa.
module.setLEDs((1)); // TIME ELASPSED
module.setDisplayToDecNumber(nTime, _BV(3)); // display milliseconds with dot on digit 4
break;
case 18: // HALL EFFECT COUNT
module.setLEDs((2));
module.setDisplayToDecNumber(distance); // Displaying number of turns on wheel.
break;
case 20:
module.setLEDs((4));
module.setDisplayToDecNumber(rpm); // Displaying RPM of wheel.
break;
case 22:
module.setLEDs((8));
module.setDisplayToDecNumber(mph); // Displaying Spped of wheel.
break;
case 17:
module.setLEDs((16));
module.setDisplayToDecNumber(MaxSpeed); // Displaying Max Speed recorded of wheel.
break;
case 19:
module.setLEDs((32));
module.setDisplayToDecNumber(AverageSpeed); // Displaying Max Speed recorded of wheel.
break;
case 21:
module.setLEDs((64));
module.setDisplayToDecNumber(scale.get_units(), 1);
break;
case 23: //// RESET TIME AND DISTANCE
module.setLEDs((128));
module.clearDisplay();
module.setDisplayToString("Reset");
myTime = millis();
mph = 0.00;
MaxSpeed = 0.00;
AverageSpeed = 0.00;
AverageSpeedSum = 0.00;
AverageCounter = 1;
Wheelencoder.write(0);
WheelCount = 0;
distance = 0.00;
currentMillis = 0;
previousMillis = 0;
rpm = 0;
previousWheelCount = 0;
keycount = 0;
keynumber = 16; // reset keynumber to 16 so everything doesnt keep reseting
break;
}
}
void fnClick(byte nButton) {
keynumber = nButton;
if (keycount == 2) keycount = 0;
else keycount++;
}