So this code finally works, how do i change it from a light sensor to infrared sensor?
int dirPin = 8;
int stepperPin = 7;
void setup() {
Serial.begin(9600);
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
pinMode(A0, INPUT);
}
void step(boolean dir,int steps){
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i<steps;i++)
{
digitalWrite(stepperPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepperPin, LOW);
delayMicroseconds(800);
}
}
void loop()
{
int s0 = analogRead(A0);
int lowVal = 200;
int highVal = 250;
Serial.print(" s0 ");
Serial.print(s0);
if (s0 >= highVal)
{
step(true,900);
delay(500);
step(false,0);
delay(500);
}
else if (s0 <= lowVal)
{
step(true,0);
delay(500);
step(false,900);
delay(0);
}
}
After getting your code in a useful layout,
int dirPin = 8;
int stepper = 7;
void setup() {
Serial.begin(9600);
pinMode(dirPin, OUTPUT);
pinMode(stepperPin,OUTPUT);
}
void step(boolean dir, int steps) {
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i=highVal) {
step(true,400); delay(1000);
step(false,400); delay(1000);
}
else if (s0 >= lowVal)
delayMicroseconds(800);
digitalWrite(stepperPin);
delayMicroseconds(800);
}
void loop() {
int lowVal = 60;
int highVal = 300;
int s0 = analogRead(A0);
if (s0 >= highVal) {
step(true,400);
delay(1000);
step(false,400);
delay(1000);
}
else if (s0 >= lowVal) {
step(true,0);
delay(1000);
step(false,0);
delay(1000);
}
I will attempt to correct some of the problems with the above code.
int dirPin = 8;
int stepperPin = 7;// changed from stepper
// moved
int lowVal = 60;
int highVal = 300;
void setup() {
Serial.begin(9600);
pinMode(dirPin, OUTPUT);
pinMode(stepperPin,OUTPUT);
// required??
pinMode(A0, INPUT);
}
void step(bool dir, int steps) {//changed boolean to bool
digitalWrite(dirPin,dir);
delay(50);
// was originally
// for{int i=0;i=highVal}
for(int i=0;i=highVal;++i) {
step(true,400); delay(1000);
step(false,400); delay(1000);
}
// you have no preceding if. You will need to fix this.
else if (s0 >= lowVal) {
delayMicroseconds(800);
digitalWrite(stepperPin);
delayMicroseconds(800);
}
void loop() {
// defining lowVal, highVal here makes them only visible to
// this function. If they need to be known about outside this
// function you will need to define them above with the dirPin
// and stepperPin
//~ int lowVal = 60;
//~ int highVal = 300;
int s0 = analogRead(A0);
if (s0 >= highVal) {
step(true,400);
delay(1000);
step(false,400);
delay(1000);
}// end of if
else if (s0 >= lowVal) {
step(true,0);
delay(1000);
step(false,0);
delay(1000);
}// end of else if
}// end of void loop(). This was missing.
Thanks
I really appreciate it, my code looked crazy, because i posted it from my phone while at work. Thanks once more, will go test it out.
The code update you have posted is still not right.
You have left out the void loop() function completely, and, your for loop is missing its closing bracket. That is to say that the closing bracket for it is somewhere near the bottom of your code. You would be best off to copy the listing I said was the best I could do and work from that. Move what you feel you need to, and, let us know how it works.
Sharp infra red sensor
I discovered that the light sensor isn’t that active, can you please help me with a code assuming I will be using a sharp infrared sensor with the stepper motor instead of a light sensor. Thank you.
code not working
int dirPin = 8;
int stepperPin = 7;
int lowVal = 60;
int highVal = 300;
void setup()
{
Serial.begin(9600);
pinMode(dirPin, OUTPUT);
pinMode(stepperPin,OUTPUT);
pinMode(A0, INPUT);
}
void step(bool dir, int steps)
{
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i=highVal;++i)
{
step(true,400); delay(1000);
step(false,400); delay(1000);
}
void loop()
{
int s0 = analogRead(A0);
if (s0 >= highVal)
{
step(true,400);
delay(1000);
step(false,400);
delay(1000);
}
else if (s0 >= lowVal)
{
step(true,0);
delay(1000);
step(false,0);
delay(1000);
}
}
Quick question
I am going to assume you are using a $5 geared ebay stepper (the one with the darlington driver board), is this correct?
Also, did you get the stepper working just by itself? I mean, did the stepper work properly before you started adding the LDR stuff?