Skip to content

Instantly share code, notes, and snippets.

@taboularasa
Created October 6, 2011 17:10
Show Gist options
  • Save taboularasa/1267980 to your computer and use it in GitHub Desktop.
Save taboularasa/1267980 to your computer and use it in GitHub Desktop.
IRTarget
String inString = ""; // string to hold input
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 13, irTarget = 2; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 0; // interval at which to blink (milliseconds)
void setup() {
// Initialize serial communications:
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(irTarget, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string,
// then the string's value:
if (inChar == '\n') {
Serial.print("Value:");
Serial.println(inString.toInt());
interval = inString.toInt();
Serial.print("String: ");
Serial.println(inString);
// clear the string for new input:
inString = "";
}
}
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
ledState = HIGH;
previousMillis = currentMillis;
interval = 0;
}
else ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
digitalWrite(irTarget, ledState);
Serial.println(interval);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment