Last active
January 15, 2019 19:00
-
-
Save kellishaver/2e4ecc7499de7d2a9f69dd139457ddce to your computer and use it in GitHub Desktop.
Flappy Guy Army Control Code
This file contains hidden or 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 <IRremote.h> | |
// FanGates control MOSFET transistors | |
int FanGates[5] = {3,5,6,9,10}; | |
// Individual states for each Gate (on 1/off 0) | |
int FanState[5] = {0}; | |
// Array of power levels for each fan | |
int FanSpeeds[5] = {0}; | |
// Power level for fan on high speed | |
int high_power = 180; | |
// Power level for fan on low speed | |
int low_power = 50; | |
// Millisecond delay storage vars for the IR receiver | |
// and fan speed control timing | |
unsigned long prev_ir_ms = 0; | |
unsigned long prev_ms[5] = {0}; | |
// Duration intervals for IR read and fan motor speeds | |
unsigned long ir_interval = 1000; | |
unsigned long fan_interval = 2000; | |
// Receives input from IR remote | |
int IR_RECV_PIN = 11; | |
// Set up the receiver | |
IRrecv irrecv(IR_RECV_PIN); | |
decode_results results; | |
void setup() { | |
// Start the IR receiver | |
irrecv.enableIRIn(); | |
// Set pin modes and initial state for Fan gates | |
for (int i = 0; i < 5; i = i + 1) { | |
pinMode(FanGates[i], OUTPUT); | |
digitalWrite(FanGates[i], LOW); | |
} | |
} | |
void loop() { | |
// Run control loop on each fan | |
for (int i = 0; i < 5; i = i + 1) { | |
control_fan(i); | |
} | |
// Toggle fan state if we receive IR input | |
if (irrecv.decode(&results)) { | |
toggle_fan_state(results.value); | |
unsigned long cur_ms = millis(); | |
// Delay reading more IR input for the duration of | |
// the IR interval - basically debouce IR | |
if (cur_ms - prev_ir_ms >= ir_interval) { | |
// Update millisecond storage var and restart | |
// reading of IR input | |
prev_ir_ms = cur_ms; | |
irrecv.resume(); | |
} | |
} | |
} | |
// Set fan states based on IR input. | |
void toggle_fan_state(int fan) { | |
if (fan == 14535) { | |
for (int i = 0; i < 5; i = i + 1) { | |
FanState[i] = 0; | |
} | |
} else { | |
// Raw ints from the IR remote | |
int FanBtns[5] = {16753245,16736925,16769565,16720605,16712445}; | |
// Find the fan gate that corresponds to the button. | |
int index = 100; // Default out of range value | |
for (int i = 0; i < 5; i = i + 1) { | |
if (FanBtns[i] == fan) { | |
index = i; | |
} | |
} | |
// Ignore any uncomfigured buttons (index still out | |
// of range) | |
if (index < 100) { | |
FanState[index] = (FanState[index] == 0)? 1 : 0; | |
} | |
} | |
} | |
// Control fan gates based on timing and their state as | |
// set via remote input. | |
void control_fan(int fan) { | |
// If the fan gate has been toggled on, get current | |
// millisecond timestamp and set fan motor to either | |
// high speed or low speed depending on timing. | |
if(FanState[fan] == 1) { | |
unsigned long cur_ms = millis(); | |
if (cur_ms - prev_ms[fan] >= fan_interval) { | |
prev_ms[fan] = cur_ms; | |
if(FanSpeeds[fan] != high_power) { | |
FanSpeeds[fan] = high_power; | |
analogWrite(FanGates[fan], high_power); | |
fan_interval = random(2000,4000); | |
} else { | |
FanSpeeds[fan] = low_power; | |
analogWrite(FanGates[fan], low_power); | |
fan_interval = random(1000,2000); | |
} | |
} | |
// If the fan gate has been toggled off, set its | |
// pin to LOW, toggling the gate. | |
} else { | |
if(FanSpeeds[fan] > 0) { | |
FanSpeeds[fan] = 0; | |
digitalWrite(FanGates[fan], LOW); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment