Remove these ads by
Signing UpStep 1Things you will need
Mini Breadboard
3 Breadboard Jumpers
6 LED's
Solid Core Wire
8 Pin Header
Perf Board
Potentiometer (optional)
| « Previous Step | Download PDFView All Steps | Next Step » |
Remove these ads by
Signing Up| « Previous Step | Download PDFView All Steps | Next Step » |

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.
great 'ible btw!
*hope this code formats correctly*
//Cylon_Scanner_with_PotControl
//Created by: Zachary Gehrken
//Heavily Modified by Vick Jr to use logarithmic PWM and smooth eye movement interpolation (fancy *&%&!)
//led stuff
/***make vertualSize larger for smoother but slower movement***/
const unsigned int vertualSize = 80; //the eye will move along a vertual track with this many units. Ideally the logarithmic lookup table would have the same amount
const unsigned int ledPins[] = {3,5,6,9,10,11}; //array of LED pins used with PWM
const unsigned int lastIndex=5; //last index of the led array (total pins-1)
unsigned int ledPositions[6]; //vertual positions of the LEDs along the vertual track
int index=0; //index for looping through arrays
//input stuff
const float speedCalibrator=0.2; //the input of the POT (range 0->255) is multiplied by this to get a reasonable delay amount between updates
const unsigned int analogPin = 2; //input pin for potentiometer
unsigned int sensorValue = 0; //value of the analog speed potentiometer
//eye and movement stuff
/***CHANGE eyeRadius TO ADJUST EYE SIZE***/
const int eyeRadius=40; //LEDs more than this many vertual units away from the center will be off.
boolean moveRight; //specifies if the eye is moving right (may be left. whatever)
int eyePosition; //the current position of the center of the eye along vertual track
int dist; //vertudistance from a given led to the eye center
//PWM stuff
int dutyCycle; //PWM dutyCycle for a given pin (inversly related to distance)
float scaler =(16.0/eyeRadius); //maps brightness values in the range [0, eyeRadius] to values in the range [0, 16]
int brightness; //brightness of led on a scale from 0 to 15
//logarithmic look-up table to convert desired brightness to pwm dudy cycle. 17 values. last index=16
int PWMDC[] = {0,1,2,3,4,6,8,12,16,23,32,45,64,90,128,180,255};
void setup() {
// Serial.begin(9600); // initialize serial communications at 9600 bps:
float space=vertualSize/(lastIndex); // vertual space between LEDs
for (index=0;index<=lastIndex;index++){//loop though pins
pinMode(ledPins[index], OUTPUT); // set them for output
ledPositions[index]=space*index; // calculate the vertual positions along vertual track
}
}
void loop() {
//get input from POT and delay accordingly
sensorValue = speedCalibrator*analogRead(analogPin);//update the speed if neccisary
delay(sensorValue);//delay according to the speed
/*
Serial.print("\ndelay:" );
Serial.print(sensorValue);
*/
//update eye position by 1 step along vertual track
// Serial.print("\tMOVING:" );
if (moveRight){//if the eye is moving right
if (eyePosition<vertualSize){//and it's not past the last led on the right
eyePosition++;//add to the position
// Serial.print("R");
}else{//but if it's at or past that position
moveRight=false;//start moving left next time. stay put this time
// Serial.print("L");
}
}
else{//if the eye is moving left
if (eyePosition>0){//and it's not past the last led on the left
eyePosition--;//subtract from the position
// Serial.print("L");
}else{//but if it's at or past that position
moveRight=true;//start moving right
// Serial.print("R");
}
}
/*
Serial.print("\tEYE:" );
Serial.print(eyePosition);
*/
//make the LEDs light up appropriatly for the position of the eye
for (index=0;index<=lastIndex;index++){//loop though leds
dist=eyePosition-ledPositions[index]; //vertual distance from the eye to the LED (range 0 through vertualSize)
dist=abs(dist);//distance cannot be negative
//Serial.print("\t" );
//Serial.print(index);
if (dist<=eyeRadius){ //if this LED is within the eye
brightness=eyeRadius-dist; //as distance decreases, brightness increases (range [0, eyeRadius])
brightness*=scaler;
brightness+=0.5;
//map(brightness,0,eyeRadius,0,16);
//dutyIndex=int((brightness*scaler)+0.5);//calc the closes index to use
dutyCycle=PWMDC[brightness/*dutyIndex*/];//scale the brightness to get a value range [0,16]. use this as index to get corresponding logarithmic duty cycle
/*
Serial.print(":D:");
Serial.print(dist);
Serial.print("\tB:");
Serial.print(brightness);
//Serial.print("\tI:");
//Serial.print(dutyIndex);
Serial.print("\tC:");
Serial.print(dutyCycle);
*/
}else{
dutyCycle=0;
// Serial.print(":NA\t\t");
}
analogWrite(ledPins[index],dutyCycle);//set that pin at that brightness
}
}
*nope. didn't format. oh well. arduino ide has an auto-format tool.*