This is the Analog lab that we worked on in class. We used an LED light, an audio speaker and a servo, which were all manipulated by a potentiometer.

This is the code:

#define pot A0
#define redLED 6
#define speaker 4
#define servoPin 9

#include <Servo.h>

//global servo;
Servo servo1;

void setup() {
pinMode(pot, INPUT);
pinMode(speaker, OUTPUT);
servo1.attach(servoPin);

}

void loop() {
int val = analogRead(pot);
int pos = map(val,0,1023,0,180);
int pitch = map(val,0,1023,400,2000);
byte ledBrightness = val>>2;
servo1.write(pos);
tone(speaker,pitch);
analogWrite(redLED, ledBrightness);
delay(50);

}

Here is the video of it working!

Advertisements