Arduino/Teensy

From Omnia
Revision as of 17:36, 8 March 2015 by Kenneth (talk | contribs) (→‎Minimum)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Pinout

https://www.pjrc.com/teensy/pinout.html

Code

Minimum

void setup() {
  // put your setup code here, to run once
}

void loop() {
  // put your main code here, to run repeatedly
}

Blink

int led = 11;  // on board LED
void setup() {
  pinMode(led, OUTPUT);
}
void loop() {
  digitalWrite(led, HIGH);
  delay(1000);
  digitalWrite(led, LOW);
  delay(1000);
}

Fade LED

int led = 12;          // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup()  {
  pinMode(led, OUTPUT);
}

void loop()  {
  // set the brightness
  analogWrite(led, brightness);
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}

Combination Blink and Fade

int blink_led = 11;    // on board LED
int blink_counter = 0;

int fade_led = 12;     // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup() {
  pinMode(blink_led, OUTPUT);
  pinMode(fade_led, OUTPUT);
}

void loop() {
  blink_counter++;
  if (blink_counter >= 60) {
    blink_counter = 0;
  }
  if (blink_counter == 0) {
    digitalWrite(blink_led, HIGH);
  }
  if (blink_counter == 30) {
    digitalWrite(blink_led, LOW);
  }

  // set the brightness
  analogWrite(fade_led, brightness);
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }

  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}

Input

int led_pin = 11;  // on board LED
int switch_pin = 20;  // pin for input
int switch_val = 0;  // variable for input pin value

void setup() {
  pinMode(led_pin, OUTPUT);
  pinMode(switch_pin, INPUT);
  digitalWrite(switch_pin, HIGH);   // turn on pullup resistor
}

void loop() {
  switch_val = digitalRead(switch_pin);
  digitalWrite(led_pin, switch_val);  // sets the LED to the button's value
}

References:

Links

keywords