Two photo resistors are positioned on opposite sides of a rectangular beam. It the beam is pointing at a light source both photoresistors produce equal values. If not aligned this balance is upset by the shadow of the beam and the difference in photo resistor output is used to rotate the beam using a stepper motor.
Gnd-- 10k--AnalogInPin--Photoresistor--3.3V
The code
//SeekSun1.js
// 7 Mar 2017
// add stepper motor code
// 4 Mar 2018
// Uses 2 photoresistors and 10k resistors to provide
// two analog inputs
// the LED color changes depending on the sign of the
// difference between the analog inputs.
//
// |
// pr1----|====pr2
// the vertical bar casts a shadow on the pr
// this creates a difference in input values
// use this to rotate the assembly so that the
// difference in inputs is within the band
// Stepper Motor Interface Configuration
var action=0;
var Step=A8;//LED1;//A8;
var Dir=B7;
var pp=new Uint8Array(32); // 16 microsteps *2, see BigEasy docs
// Photo resistor Configuration
var P1=B1;
var P2=A7;
pp.fill(1); //pulse time in ms
pinMode(Step,"output");
pinMode(Dir,"output");
digitalWrite(Step,0);
digitalWrite(Dir,0^action);
clearInterval();
function move(d){
digitalWrite(Dir,d^action);
digitalPulse(Step,0,pp);
}
var verbose=1;//0;//1;
function clog(a){
if(verbose>0)console.log(a);
}
var band=0.01;
pinMode(P1,'analog');
pinMode(P2,'analog');
clog(analogRead(P1));
clog(analogRead(P2));
var a,b,c,d;
var sss="";
setInterval(function(){
a=analogRead(P1);
b=analogRead(P2);
sss=a.toString()+",";
sss+=b.toString()+",";
sss+=(a-b).toString();
clog(sss);
c=a-b;
d=c;
if(d<0)d=d*-1;
if(d<band){
digitalWrite(LED2,0);
digitalWrite(LED1,0);
//nomotion();
}else{
if(c<0){
digitalWrite(LED1,0);
digitalWrite(LED2,1);
//rotleft();
move(0);
}
if(c>0){
digitalWrite(LED2,0);
digitalWrite(LED1,1);
//rotright();
move(1);
}
}//end else
},1000);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Light Seeking System
Two photo resistors are positioned on opposite sides of a rectangular beam. It the beam is pointing at a light source both photoresistors produce equal values. If not aligned this balance is upset by the shadow of the beam and the difference in photo resistor output is used to rotate the beam using a stepper motor.
Gnd-- 10k--AnalogInPin--Photoresistor--3.3V
The code
Stepper Motor Stuff
Big Easy
or
BigEasy2
or for smaller stepper motors
EasyDriver
Wiring
Pico Bigeasy
Gnd Gnd
B7 Dir
A8 Step
Powered the BigEasy board with 12V
1 Attachment