Skip to content

Instantly share code, notes, and snippets.

@theinventor
Created February 22, 2018 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theinventor/87cafadca18937ce73a14991a837e267 to your computer and use it in GitHub Desktop.
Save theinventor/87cafadca18937ce73a14991a837e267 to your computer and use it in GitHub Desktop.
Simple arduino code to handle pwm and drive rc car headlights (LEDs) from a receiver (power and signal)
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
To upload to your Gemma or Trinket:
1) Select the proper board from the Tools->Board Menu
2) Select USBtinyISP from the Tools->Programmer
3) Plug in the Gemma/Trinket, make sure you see the green LED lit
4) For windows, install the USBtiny drivers
5) Press the button on the Gemma/Trinket - verify you see
the red LED pulse. This means it is ready to receive data
6) Click the upload button above within 10 seconds
*/
byte PWM_PIN = 3;
const int ledPin = 13;
const int headLights = 6;
const int lowBeams = 60;
const int highBeams = 255;
int rxval;
void setup() {
pinMode(PWM_PIN, INPUT);
pinMode(ledPin, OUTPUT);
// Serial.begin(115200);
}
void loop() {
rxval = pulseIn(PWM_PIN, HIGH);
// Serial.println(rxval);
if (rxval >= 990 && rxval <= 1450)
{
//low value
analogWrite(headLights, LOW);
digitalWrite(ledPin, LOW);
}
else if (rxval >= 1451 && rxval <= 1850)
{
//medium value (middle)
analogWrite(headLights, lowBeams);
digitalWrite(ledPin, HIGH);
}
else if (rxval >= 1851 && rxval <= 2250)
{
//high value
analogWrite(headLights, highBeams);
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment