Last active
October 3, 2021 09:35
-
-
Save mokjpn/35d0f2df3b56cc811d31c9f604af4d0c to your computer and use it in GitHub Desktop.
M5Atom Lite + PWM Kit + Fader Unit Z-gauge train model driver
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; | |
uint16_t rawADC = 0; | |
// ATOM PWM Kit | |
#define SIGNAL 22 | |
int freq = 500; | |
int ledChannel1 = 0; | |
int resolution = 8; | |
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, 6, CRGB::Blue); | |
fill_gradient_RGB(leds, 7, CRGB::Blue, 13, CRGB::Red); | |
// PWM setup | |
ledcSetup(ledChannel1, freq, resolution); | |
ledcAttachPin(SIGNAL, ledChannel1); | |
} | |
void loop() { | |
// get Fader input, set brightness | |
rawADC = analogRead(INPUT_PINS); // Read ADC value | |
brightness = map(rawADC, 0, 4095, 0, 128); // Map ADC value to LED brightness: set maximum to half brightness | |
FastLED.setBrightness(brightness); //Adjust the brightness of the FADER LED | |
FastLED.show(); | |
// set PWM duty by fader input | |
uint32_t duty = map(rawADC, 0, 4095, 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. | |
Serial.printf("rawADC: %d, Duty: %d\n", rawADC, duty); | |
ledcWrite(ledChannel1, duty); | |
delay(2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment