Showing posts with label Potentiometer. Show all posts
Showing posts with label Potentiometer. Show all posts

Monday, March 25, 2013

Arduino Basics - Pot Controlled LED Array W/ Reverse Switch And Processing Graphics

The Schematic




The Code


[sourcecode language="c"]
int but = 2;
int bval = 0;
bool state = 0;
int pot = A2;
int pval = 0;
int leds[10] = {12,11,10,9,8,7,6,5,4,3};

void setup()
{
Serial.begin(9600);
pinMode(but, INPUT);
for(int x = 3; x <= 12; x++) pinMode(x, OUTPUT);
}

void loop()
{
bval = digitalRead(but);
if (bval == 1) state = !state;

if (state == 1)
{
Serial.println(analogRead(pot));
pval = analogRead(pot);
pval = map(pval, 0, 1023, 0, 9);

for (int x = 9; x >= 0; x--) digitalWrite(leds[x], LOW);
for (int x = 9; x >= pval; x--) digitalWrite(leds[x], HIGH);
}
else if (state == 0)
{
Serial.println(analogRead(pot));
pval = analogRead(pot);
pval = map(pval, 0, 1023, 0, 9);

for (int x = 0; x <= 9; x++) digitalWrite(leds[x], LOW);
for (int x = 0; x <= pval; x++) digitalWrite(leds[x], HIGH);
}

delay(100);
}
[/sourcecode]

The Implementation




Adding Graphics With Processing




The Processing Code


[sourcecode language="c"]
import processing.serial.*;

Serial myPort;
int xPos = 1;

void setup () {
size(400, 300);
println(Serial.list());

myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');

background(0);
}

void draw () {
// everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');

if (inString != null) {
inString = trim(inString);

float inByte = float(inString);
float hue1 = inByte;
float hue2, hue3;
hue1 = map(inByte, 0, 1023, 0, 255);
inByte = map(inByte, 0, 1023, 0, height);

// Have a little fun with the colors using
// the potentiometer as input
if (hue1 <= 200) {
hue2 = hue1 + 50;
} else {
hue2 = hue1 - 50;
}

if (hue1 >= 55) {
hue3 = hue1 - 50;
} else {
hue3 = hue1 + 50;
}

// Draw the line
stroke(hue1, hue2, hue3);
line(xPos, height, xPos, height - inByte);

if (xPos >= width) {
xPos = 0;
background(0);
} else {
xPos++;
}
}
}
[/sourcecode]

In Action


http://youtu.be/1nVp5KJoAPI

Saturday, February 23, 2013

Arduino Basics - Speed Controlled RGB LED Array

The Schematic


Speed Controlled RGB LED Array


The Code


[sourcecode language="java"]
// p is our potentiometer
// r, g, & b our led's
int r=10,g=11,b=12,p=1,pval=0;
void setup(){
Serial.begin(9600);
pinMode(r,1);
pinMode(g,1);
pinMode(b,1);
}
void loop(){
// read the potentiometer
pval=analogRead(p);
// pass the value to cycle function
cycle(pval);
}
void cycle(int interval){
// map the pot value to something
// more managable
interval=map(interval,1,1023,30,300);
// write to serial for debugging
Serial.println(interval);
// flash values to rgb pins
flashPin(r,interval);
flashPin(g,interval);
flashPin(b,interval);
}
void flashPin(int pin,int interval){
// turn led on
digitalWrite(pin,1);
// read interval val from pot
delay(interval);
// turn led off
digitalWrite(pin,0);
delay(interval);
}
[/sourcecode]

The Implementation


Arduino Basics - Speed Controlled RGB LED Array
http://youtu.be/YZBKMzh6Bg4

Saturday, February 16, 2013

Arduino Basics - Controlling A Servo Motor With A Potentiometer

Cautionary word of advice: the limits I set in the code are based on the thresholds of my particular servo motor, you may need to adjust them to avoid damaging your motor.
[sourcecode language="c"]
pval=map(pval,0,1023,0,165); // The 165 in here may need to be adjusted
// Default limits are 0 - 180
// See below code for details
[/sourcecode]

The Circuit



Potentiometer Controlling a Servo Motor


The Code


[sourcecode language="c"]
#include <Servo.h>
Servo s;
int p = 0; // Potentiometer
int pval = 0;

void setup(){
s.attach(10);
Serial.begin(9600);
}
void loop(){
pval=analogRead(p);
pval=map(pval,0,1023,0,165); // <-- Adjust to motor thresholds
Serial.println(pval);
delay(20);
s.write(pval);
delay(10); // Smooth it a bit
}
[/sourcecode]

Circuit Implementation



Servo Potentiometer Circuit Setup