Let's Make Robots!

Trying to use a light sensor to control a stepper motor

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);

  }

}

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

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?

 

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);

    }

}

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.

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.

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. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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.

The best I can do.