Solutions

Button and LED

int led = 14;
int but = 15;

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

void loop() {
  if (digitalRead(but) == LOW){
    digitalWrite(led, HIGH); 
  }
  else {
    digitalWrite(led, LOW);
  }
}

Toggle

int led = 14; // led pin
int but = 15; // button pin
int but_state = 1; // state of button
int prev_but = 1; // previous loop button state
int led_state = 0; // state of led

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

void loop() {
  but_state = digitalRead(but); // read the button
  // if the button was just pressed
  if (but_state == 0 && prev_but == 1){
    // toggle the led
    if (led_state == 0){
      led_state = 1;
    }
    else {
      led_state = 0;
    }
  }

  if (led_state == 1){
    digitalWrite(led, HIGH);
  }
  else {
    digitalWrite(led,LOW);
  }
  // remember the button state
  prev_but = but_state;
}

Interrupt

int led = 14; // led pin
int but = 15; // button pin
volatile int led_state = 0; // variables used in intterupts must be declared volatile

void setup() {
  pinMode(led, OUTPUT);
  pinMode(but, INPUT);
  // when but pin goes HIGH to LOW call the function but_pressed()
  attachInterrupt(digitalPinToInterrupt(but), but_pressed, FALLING);
}

void loop() {
  if (led_state == 1) {
    digitalWrite(led, HIGH);
  }
  else {
    digitalWrite(led, LOW);
  }
}

void but_pressed() {
  if (led_state == 1) {
    led_state = 0;
  }
  else {
    led_state = 1;
  }
}

Pot LED Dimmer

int led = 14; // led pin

void setup() {
  Serial.begin(9600);

  // configure LED PWM functionalitites
  ledcSetup(0, 5000, 8); // ledChannel, freq, resolution
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(led, 0); // ledPin, ledChannel
}

void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  //analogWrite(led, sensorValue/16); // this is how most other Arduino work
  ledcWrite(0, sensorValue/16); // ledChannel, dutyCycle
  delay(10);
}

Mic 1000 values

void setup() {
  Serial.begin(115200); // use a faster baud when sending a lot of data fast
}

void loop() {
  int sensorValues[1000]; // an array to store the data
  int i = 0; // an index to remember where you are in the array

  // get 1000 data points
  for(i=0; i<1000; i++){
    sensorValues[i] = analogRead(A0);
    delayMicroseconds(10); // 10us delay = 100kHz sampling
  }

  // send the data to the computer
  for(i=0; i<1000; i++){
    Serial.println(sensorValues[i]);
  }

  // wait half a second so the user can see the plot
  delay(500);
}

Serial Pot Button

int but = 15; // button pin

void setup() {
  Serial.begin(9600);
  pinMode(but, INPUT);
}

void loop() {
  if (digitalRead(but) == LOW){
    Serial.print("Pot V = ");
    Serial.println(analogRead(A0)*3.3/4095); // be careful fixing int and float math!
  }

  delay(10);
}

Serial Read

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()){
    char c = Serial.read();
    if (c == 'r'){
      int i = 0;
      for (i=0; i<100; i++){
        Serial.println(analogRead(A0));
        delay(10);
      }
    }
  }
}

Servo Potentiometer

#include <Servo.h>

Servo myservo;

void setup()
{
  myservo.attach(33);
}

void loop()
{
  int pot = analogRead(A0);
  int pos = map(pot, 0, 1023, 0, 180);
  myservo.write(pos);
  delay(100);
}

Neopixels

#include <OctoWS2811.h>

const int numPins = 1;
byte pinList[numPins] = {34};
const int ledsPerStrip = 8;
DMAMEM int displayMemory[ledsPerStrip * numPins * 3 / 4];
int drawingMemory[ledsPerStrip * numPins * 3 / 4];
const int config = WS2811_GRB | WS2811_800kHz;
OctoWS2811 leds(ledsPerStrip, displayMemory, drawingMemory, config, numPins, pinList);

#define RED    0xFF0000
#define GREEN  0x00FF00
#define BLUE   0x0000FF
#define YELLOW 0xFFFF00
#define PINK   0xFF1088
#define ORANGE 0xE05800
#define WHITE  0xFFFFFF

void setup() {
  leds.begin();
  leds.show();
}

void loop() {
  int i;
  for (i = 0; i < leds.numPixels(); i++) {
    leds.setPixel(i, RED);
  }
  leds.show();
  delay(1000);

  for (i = 0; i < leds.numPixels(); i++) {
    leds.setPixel(i, GREEN);
  }
  leds.show();
  delay(1000);

  for (i = 0; i < leds.numPixels(); i++) {
    leds.setPixel(i, BLUE);
  }
  leds.show();
  delay(1000);
}

Neopixels Potentiometer

#include <OctoWS2811.h>

const int numPins = 1;
byte pinList[numPins] = {34};
const int ledsPerStrip = 8;
DMAMEM int displayMemory[ledsPerStrip * numPins * 3 / 4];
int drawingMemory[ledsPerStrip * numPins * 3 / 4];
const int config = WS2811_GRB | WS2811_800kHz;
OctoWS2811 leds(ledsPerStrip, displayMemory, drawingMemory, config, numPins, pinList);

void setup() {
  leds.begin();
  leds.show();
}

void loop() {
  int i;
  int brite = analogRead(A0)/4;
  for (i = 0; i < leds.numPixels(); i++) {
    leds.setPixel(i, Color(brite, brite, brite)); // BRG for some reason
  }
  leds.show();
  delay(10);
}

unsigned int Color(byte b, byte r, byte g)
{
  return ( ((unsigned int)r & 0xFF ) << 16 | ((unsigned int)g & 0xFF) << 8 | (unsigned int)b & 0xFF);
}

IMU Plot

{{#include ./code/huzzah32-imu-plot.c}}

IMU Neopixel

{{#include ./code/huzzah32-imu-neopixels.c}}

Play Sound

{{#include ./code/huzzah32-play-sound.c}}