Skip to content

Instantly share code, notes, and snippets.

@yvbbrjdr
Last active October 22, 2018 06:42
Show Gist options
  • Save yvbbrjdr/c813f0719bed2f6a5af2fb52299c5f83 to your computer and use it in GitHub Desktop.
Save yvbbrjdr/c813f0719bed2f6a5af2fb52299c5f83 to your computer and use it in GitHub Desktop.
#define NUM_OF(x) (sizeof(x) / sizeof(x[0]))
static const int BUTTON = P2_1;
static const int BUTTON_PRESSED = 0;
static const int BUTTON_RELEASED = 1;
static const int FANS[] = {P2_0, P2_5, P2_4, P1_5, P1_4};
static const int NUM_OF_FANS = NUM_OF(FANS);
static const int FAN_FULL_SPEED = 0xFF;
static unsigned char pwm = FAN_FULL_SPEED;
static int button_state = BUTTON_RELEASED;
static void set_fan_speed(void);
static void print_pwm(const char *prompt);
void setup()
{
pinMode(BUTTON, INPUT_PULLUP);
for (int i = 0; i < NUM_OF_FANS; ++i)
pinMode(FANS[i], OUTPUT);
set_fan_speed();
Serial.begin(9600);
print_pwm("Initial");
}
void loop()
{
if (Serial.available()) {
pwm = Serial.read();
set_fan_speed();
print_pwm("Adjust");
}
if (digitalRead(BUTTON) == BUTTON_PRESSED) {
button_state = BUTTON_PRESSED;
} else if (button_state == BUTTON_PRESSED) {
button_state = BUTTON_RELEASED;
print_pwm("Current");
}
}
void set_fan_speed(void)
{
for (int i = 0; i < NUM_OF_FANS; ++i)
analogWrite(FANS[i], pwm);
}
void print_pwm(const char *prompt)
{
Serial.print(prompt);
Serial.print(" fan PWM: ");
Serial.print(pwm);
Serial.println("/255");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment