Two 28BYJ-48 stepper motors + arduino nano + 4 LDRs (code not working)

Hi,
I was working with this instructable below and I used user spellmal’s code.

It worked for me before on the arduino uno with my continuous rotation servos.

I was previously using servo motors but I want to move away from them because the solar cells I am powering them with aren’t strong enough to supply the initial stall current.

That’s why I switched to:

Two 28BYJ-48 stepper motors

I’ve modified my code to use these stepper motors
However, my code keeps giving me this error:

void value not ignored as it ought to be

I am thinking it has to do with my void setup code. Can someone tell me where I may have gone wrong? Thanks.

#include <Stepper.h>// include stepper library
// using unipolar stepper motors 28BYJ-48 with ULN2003 driver breakout board

const int stepsPerRevolution = 4076; // the number of steps per revolution for your motor

//initialize the stepper library on pins 3 through 10
Stepper myStepperH(stepsPerRevolution, 3, 4, 5, 6); //azimuth adjustment

int stepperhstop = myStepperH.setSpeed(0);

int stepperhleft = myStepperH.step(-stepsPerRevolution);;

int stepperhright = myStepperH.step(stepsPerRevolution);

Stepper myStepperV(stepsPerRevolution, 7, 8, 9, 10); //elevation adjustment

int steppervstop = myStepperV.setSpeed(0);

int steppervup = myStepperV.step(stepsPerRevolution);

int steppervdown = myStepperV.step(-stepsPerRevolution);

// LDR pin connections

// name = analogpin;

int ldrtopright = 4; //LDR top right

int ldrtopleft = 3; //LDR top left

int ldrbottomleft = 5; //LDR bottom left

int ldrbottomright = 6; //LDR bottom right

int dtime = 25; //delay time in milliseconds

int tolerance = 100; //difference tolerance between ldr readings

int count = 0; //millis(); //start millisecond count

void setup() 
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  Serial.begin(9600);  // initialize the serial port:
  myStepperH.setSpeed(20);
  myStepperV.setSpeed(20);
}

void loop()

{

//int count = millis(); //start millisecond count

int tr = analogRead(ldrtopright);
int tl = analogRead(ldrtopleft);
int bl = analogRead(ldrbottomleft);
int br = analogRead(ldrbottomright);

//print resistance values to serial monitor for debugging

Serial.println(tr);

Serial.println(tl);

Serial.println(bl);

Serial.println(br);

Serial.println();

count++;

int avt= (tr + tl) / 2; // average value top

int avd = (bl + br) / 2; // average value down

int avl = (tl + bl) / 2; // average value left

int avr = (tr + br) / 2; // average value right

int avgTot = (tr+tl+bl+br)/4;

int dvert = avt - avd; // check the difference of top and bottom LDRs

int dhoriz = avl - avr;// check the difference of left and right LDRs

if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change vertical angle

{

if (avt > avd) //if average resistance on top is greater than on bottom

{

steppervup;

}

else //avg resistance on bottom is greater than on top

{

steppervdown;

}

}

else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop vertical stepper

{

steppervstop;

}

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else change horizontal angle

{

if (avl > avr)

{

stepperhleft;

}

else

{

stepperhright;

}

}

else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal

{

stepperhstop;

}

delay(dtime);

}
int stepperhstop = myStepperH.setSpeed(0);

You are trying to get a value from a function that does not return one. Use

myStepperH.setSpeed(0);

instead

Ok thanks I got it.
I thought perhaps the value from the function would be equal to 0.

I’ve replaced areas of the code that used that as a stored integer value.
I’ve also deleted
int stepperhstop and int steppervstop

All the elseif statements now have
myStepperV.setSpeed(0);
or
myStepperH.setSpeed(0);

However, I still get the errors below:

Arduino: 1.6.3 (Windows 7), Board: “Arduino Nano, ATmega328”
solar.tracker.twosteppermotors.4LDRs.REV2.ino:12:55: error: void value not ignored as it ought to be
solar.tracker.twosteppermotors.4LDRs.REV2.ino:13:55: error: void value not ignored as it ought to be
solar.tracker.twosteppermotors.4LDRs.REV2.ino:14:52: error: void value not ignored as it ought to be
solar.tracker.twosteppermotors.4LDRs.REV2.ino:15:55: error: void value not ignored as it ought to be
Error compiling.
This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

I’m not sure what this error means or what line(s) of code it’s referring to.
I am thinking I haven’t declared something somewhere in the code???
Any thoughts?

I’ve tried to clean up my code, and make it shorter for easier viewing:

// using unipolar stepper motors 28BYJ-48 with ULN2003 driver breakout board
// include stepper library
#include <Stepper.h>

// the number of steps per revolution for your motor
const int stepsPerRevolution = 4076; 

//initialize the stepper library on pins 3 through 10
Stepper myStepperH(stepsPerRevolution, 3, 4, 5, 6); //azimuth adjustment
Stepper myStepperV(stepsPerRevolution, 7, 8, 9, 10); //elevation adjustment

int stepperhleft = myStepperH.step(-stepsPerRevolution);
int stepperhright = myStepperH.step(stepsPerRevolution);
int steppervup = myStepperV.step(stepsPerRevolution);
int steppervdown = myStepperV.step(-stepsPerRevolution);

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;

int ldrtopright = 4; //LDR top right
int ldrtopleft = 3; //LDR top left
int ldrbottomleft = 5; //LDR bottom left
int ldrbottomright = 6; //LDR bottom right

int dtime = 25; //delay time in milliseconds
int tolerance = 100; //range of tolerance between LDR readings
int count = 0; //start millisecond count

void setup() 
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  Serial.begin(9600);  // initialize the serial port:
  myStepperH.setSpeed(1);
  myStepperV.setSpeed(1);
}

void loop()

{

//int count = millis(); //start millisecond count

int tr = analogRead(ldrtopright);
int tl = analogRead(ldrtopleft);
int bl = analogRead(ldrbottomleft);
int br = analogRead(ldrbottomright);

//print resistance values to serial monitor for debugging

Serial.println(tr);
Serial.println(tl);
Serial.println(bl);
Serial.println(br);
Serial.println();

count++;

int avt= (tr + tl) / 2; // average value top
int avd = (bl + br) / 2; // average value down
int avl = (tl + bl) / 2; // average value left
int avr = (tr + br) / 2; // average value right
int avgTot = (tr+tl+bl+br)/4;

int dvert = avt - avd; // check the difference of top and bottom LDRs
int dhoriz = avl - avr;// check the difference of left and right LDRs

if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change vertical angle
{
if (avt > avd) //if average resistance on top is greater than on bottom
{
steppervup;
}
else //avg resistance on bottom is greater than on top
{
steppervdown;
}
}
else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop vertical stepper
{
myStepperV.setSpeed(0);
}

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else change horizontal angle
{
if (avl > avr)
{
stepperhleft;
}
else
{
stepperhright;
}
}
else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal
{
myStepperH.setSpeed(0);
}

delay(dtime);

}

int stepperhleft = myStepperH.step(-stepsPerRevolution);Does the step method return a value ?

Woo hoo thanks for the handy clue it compiles now
I’ve attached it for other people’s reference.
Now to see if it will work in the real world.
:sunglasses:

Although I am thinking of changing the steps per revolution in between the if else statements to rpm functions

solar.tracker.twosteppermotors.4LDRs.ino (2.64 KB)

Well I thought I was done but I guess not The code works but like I noticed before after I block one or two ldrs the serial port stops sending read outs on Windows. Unless I open it again. The thing that is happening is when I'm outdoors the motors will rotate after I block for example two top ldrs but they won't rotate at all after that if I attempt to block the ldrs again. The nano board needs to get reset everytime. Is there something wrong with my void loop code?

Can you please post the code here to avoid the need for everyone to download it.

Hi thanks, the void loop is not cycling through again unless I press reset on the arduino nano.

// using unipolar stepper motors 28BYJ-48 with ULN2003 driver breakout board
#include <Stepper.h> // include stepper library


const int stepsPerRevolution = 200; // the number of steps per revolution for your motor

//initialize the stepper library on pins 3 through 10
Stepper myStepperH(stepsPerRevolution, 3, 4, 5, 6); //azimuth adjustment
Stepper myStepperV(stepsPerRevolution, 7, 8, 9, 10); //elevation adjustment

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;
int ldrtopright = 4; //LDR top right
int ldrtopleft = 3; //LDR top left
int ldrbottomleft = 5; //LDR bottom left
int ldrbottomright = 6; //LDR bottom right

int dtime = 25; //delay time in milliseconds
int tolerance = 100; //range of tolerance between LDR readings
int count = 0; //start millisecond count

void setup() 
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  Serial.begin(9600);  // initialize the serial port:
  myStepperH.setSpeed(15);
  myStepperV.setSpeed(15);
}
void loop()
{
//int count = millis(); //start millisecond count
int tr = analogRead(ldrtopright);
int tl = analogRead(ldrtopleft);
int bl = analogRead(ldrbottomleft);
int br = analogRead(ldrbottomright);

//print resistance values to serial monitor for debugging
Serial.println(tr);
Serial.println(tl);
Serial.println(bl);
Serial.println(br);
Serial.println();

count++;

int avt= (tr + tl) / 2; // average value top
int avd = (bl + br) / 2; // average value down
int avl = (tl + bl) / 2; // average value left
int avr = (tr + br) / 2; // average value right
int avgTot = (tr+tl+bl+br)/4;

int dvert = avt - avd; // check the difference of top and bottom LDRs
int dhoriz = avl - avr;// check the difference of left and right LDRs

if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change vertical angle
{
if (avt > avd) //if average resistance on top is greater than on bottom
{
myStepperV.step(stepsPerRevolution);
}
else //avg resistance on bottom is greater than on top
{
myStepperV.step(-stepsPerRevolution);
}
}
else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop vertical stepper
{
myStepperV.setSpeed(0);
}

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else change horizontal angle
{
if (avl > avr)
{
myStepperH.step(-stepsPerRevolution);
}
else
{
myStepperH.step(stepsPerRevolution);
}
}
else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal
{
myStepperH.setSpeed(0);
}
delay(dtime);
}

Can u change this code for ardino uno ?

Is there a reason why it would not run on a Uno as it is ?

please can i get the modify code for arduino uno?

The code is applicable to all Arduinos. Just make sure the correct pins are used.

BIG UPDATE. AFTER TALKING TO A FEW OF MY PROGRAMMING FRIENDS I'VE COME TO THE CONCLUSION THAT THERE IS SOMETHING WRONG WITH THE STEPPER LIBRARY OR ACCELSTEPPER LIBRARY.

EITHER THAT OR MY FUNCTIONS ARE CAUSING THEM TO NOT WORK PROPERLY.

THIS CODE WORKS GREAT WITH DC MOTORS.