Here is my code. #include #include #include #include #define TFT_CS 10 #define TFT_RST 9 #define TFT_DC 8 #define ENCODER_PIN_A 4 #define ENCODER_PIN_B 2 #define PWM_PIN 3 #define INPUT_PIN A2 #define DUTY_CYCLE_MIN 10 #define DUTY_CYCLE_MAX 90 Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); Encoder encoder(ENCODER_PIN_A, ENCODER_PIN_B); unsigned long previousUpdateTime = 0; const unsigned long updateInterval = 500; // Update interval in milliseconds (0.5 second) void setup() { pinMode(ENCODER_PIN_A, INPUT_PULLUP); pinMode(ENCODER_PIN_B, INPUT_PULLUP); pinMode(PWM_PIN, OUTPUT); pinMode(INPUT_PIN, INPUT); TCCR2B = TCCR2B & B11111000 | B00000110; // Sets output freq to 122.5Hz tft.initR(INITR_BLACKTAB); tft.fillScreen(ST77XX_BLACK); Serial.begin(9600); // Initialize Serial for debugging (optional) } void updateDisplay(int dutyCycle, int pulseWidthNegative, int encoderValue) { // Clear the display tft.fillScreen(ST77XX_BLACK); // Print the duty cycle on the TFT display tft.setTextSize(3); tft.setTextColor(ST77XX_YELLOW); tft.setCursor(5, 10); tft.print("Speed: "); tft.print(100 - dutyCycle); tft.println("%"); tft.println(""); tft.setTextColor(ST77XX_GREEN); tft.print("Neg PW:"); tft.print(pulseWidthNegative); tft.print(" us"); // Print the encoder value to the Serial Monitor (optional) Serial.print("Encoder Value: "); Serial.println(encoderValue); } void loop() { // Read the encoder value int encoderValue = encoder.read();
// Read the input status of A2 int inputStatus = digitalRead(INPUT_PIN); // Map the encoder value to the duty cycle range (10% to 90%) int dutyCycle = map(encoderValue, -100, 100, DUTY_CYCLE_MIN, DUTY_CYCLE_MAX); // Calculate the pulse widths in microseconds static int pulseWidthPositive = 0; // Time for high phase in microseconds static int pulseWidthNegative = 0; // Time for low phase in microseconds pulseWidthPositive = dutyCycle * 100; pulseWidthNegative = (100 - dutyCycle) * 100; // Only generate output pulses when A2 is high if (inputStatus == HIGH) { // Set the duty cycle for Timer0 (using analogWrite) to generate PWM on pin D3 analogWrite(PWM_PIN, dutyCycle * 2.55); // Map 100% to 255 for 8-bit PWM } else { // If A2 is low, turn off the output analogWrite(PWM_PIN, 0); } // Check if it's time to update the display unsigned long currentMillis = millis(); if (currentMillis - previousUpdateTime >= updateInterval) { // Update the display once per 500 milliseconds previousUpdateTime = currentMillis; updateDisplay(dutyCycle, pulseWidthNegative, encoderValue); } }
Where did you copy the code from?
Do you have a wiring diagram you could share of the entire setup
I wrote the code. I can do a write up with code and connections soon
Here is my code.
#include
#include
#include
#include
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define ENCODER_PIN_A 4
#define ENCODER_PIN_B 2
#define PWM_PIN 3
#define INPUT_PIN A2
#define DUTY_CYCLE_MIN 10
#define DUTY_CYCLE_MAX 90
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Encoder encoder(ENCODER_PIN_A, ENCODER_PIN_B);
unsigned long previousUpdateTime = 0;
const unsigned long updateInterval = 500; // Update interval in milliseconds (0.5 second)
void setup() {
pinMode(ENCODER_PIN_A, INPUT_PULLUP);
pinMode(ENCODER_PIN_B, INPUT_PULLUP);
pinMode(PWM_PIN, OUTPUT);
pinMode(INPUT_PIN, INPUT);
TCCR2B = TCCR2B & B11111000 | B00000110; // Sets output freq to 122.5Hz
tft.initR(INITR_BLACKTAB);
tft.fillScreen(ST77XX_BLACK);
Serial.begin(9600); // Initialize Serial for debugging (optional)
}
void updateDisplay(int dutyCycle, int pulseWidthNegative, int encoderValue) {
// Clear the display
tft.fillScreen(ST77XX_BLACK);
// Print the duty cycle on the TFT display
tft.setTextSize(3);
tft.setTextColor(ST77XX_YELLOW);
tft.setCursor(5, 10);
tft.print("Speed: ");
tft.print(100 - dutyCycle);
tft.println("%");
tft.println("");
tft.setTextColor(ST77XX_GREEN);
tft.print("Neg PW:");
tft.print(pulseWidthNegative);
tft.print(" us");
// Print the encoder value to the Serial Monitor (optional)
Serial.print("Encoder Value: ");
Serial.println(encoderValue);
}
void loop() {
// Read the encoder value
int encoderValue = encoder.read();
// Read the input status of A2
int inputStatus = digitalRead(INPUT_PIN);
// Map the encoder value to the duty cycle range (10% to 90%)
int dutyCycle = map(encoderValue, -100, 100, DUTY_CYCLE_MIN, DUTY_CYCLE_MAX);
// Calculate the pulse widths in microseconds
static int pulseWidthPositive = 0; // Time for high phase in microseconds
static int pulseWidthNegative = 0; // Time for low phase in microseconds
pulseWidthPositive = dutyCycle * 100;
pulseWidthNegative = (100 - dutyCycle) * 100;
// Only generate output pulses when A2 is high
if (inputStatus == HIGH) {
// Set the duty cycle for Timer0 (using analogWrite) to generate PWM on pin D3
analogWrite(PWM_PIN, dutyCycle * 2.55); // Map 100% to 255 for 8-bit PWM
} else {
// If A2 is low, turn off the output
analogWrite(PWM_PIN, 0);
}
// Check if it's time to update the display
unsigned long currentMillis = millis();
if (currentMillis - previousUpdateTime >= updateInterval) {
// Update the display once per 500 milliseconds
previousUpdateTime = currentMillis;
updateDisplay(dutyCycle, pulseWidthNegative, encoderValue);
}
}
Where did you copy the code from?
Do you have a wiring diagram you could share of the entire setup