Solutions

Button and LED

int led = 3;
int but = 4;

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

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

Toggle

int led = 3; // led pin
int but = 4; // 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 = 3; // led pin
int but = 4; // 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 = 3; // led pin

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

void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  analogWrite(led, sensorValue/4);
  delay(10);
}

Serial Pot Button

int but = 4; // 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/1023);
  }

  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);
}