Z-gaude rail model driver by M5Atom + HBridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "M5Atom.h" | |
#include "FastLED.h" | |
#include "pixeltypes.h" | |
// Fader Unit connected to Grove port of M5Atom Lite | |
#define NUM_LEDS_FADER 14 | |
#define DATA_PIN_FADER 26 | |
#define INPUT_PINS 32 | |
// Define the array of leds | |
CRGB leds[NUM_LEDS_FADER]; | |
uint8_t brightness = 70; | |
int16_t rawADC = 0; | |
// ATOM HBridge Kit | |
const int IN1_PIN = 19; | |
const int IN2_PIN = 23; | |
int freq = 500; | |
int ledChannel1 = 0; | |
int ledChannel2 = 1; | |
int resolution = 8; | |
bool direction = true; | |
int VIN_PIN = 33; | |
int FAULT_PIN = 22; | |
void setup() { | |
M5.begin(true, false, false); // Serial enable only | |
Serial.begin(115200); | |
// Fader set color | |
FastLED.addLeds<NEOPIXEL, DATA_PIN_FADER>(leds, NUM_LEDS_FADER); | |
delay(1000); | |
fill_gradient_RGB(leds, 0, CRGB::Red, 2, CRGB::Blue); | |
fill_gradient_RGB(leds, 4, CRGB::Blue, 6, CRGB::Red); | |
fill_gradient_RGB(leds, 7, CRGB::Red, 9, CRGB::Blue); | |
fill_gradient_RGB(leds, 11, CRGB::Blue, 13, CRGB::Red); | |
// PWM setup | |
ledcSetup(ledChannel1, freq, resolution); | |
ledcSetup(ledChannel2, freq, resolution); | |
ledcAttachPin(IN1_PIN, ledChannel1); | |
ledcAttachPin(IN2_PIN, ledChannel2); | |
pinMode(VIN_PIN,INPUT); | |
pinMode(FAULT_PIN,INPUT); | |
} | |
void loop() { | |
// get Fader input, set brightness | |
rawADC = 2048 - analogRead(INPUT_PINS); // Read ADC value | |
brightness = map(abs(rawADC), 0, 2047, 0, 128); // Map ADC value to LED brightness: set maximum to half brightness | |
if(abs(rawADC) < 150) brightness = 0; | |
FastLED.setBrightness(brightness); //Adjust the brightness of the FADER LED | |
FastLED.show(); | |
// set PWM duty by fader input | |
uint32_t duty = map(abs(rawADC), 0, 2047, 0, pow(2,resolution-3)); // Map ADC value to PWM duty: set maximum to 1/8: If input is 12V, max volume indicates 1.5V. | |
if(abs(rawADC) < 150) duty = 0; | |
Serial.printf("rawADC: %d, Duty: %d\n", rawADC, duty); | |
direction = rawADC > 0; | |
if(direction) { | |
ledcWrite(ledChannel1, duty); | |
ledcWrite(ledChannel2, 0); | |
} else { | |
ledcWrite(ledChannel1, 0); | |
ledcWrite(ledChannel2, duty); | |
} | |
delay(2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment