What do i need to do?! I can't get my head to it.


I’m new to Arduino and I really hope some one can help me out because i’m going crazy!
I know how it has to work in my head but i’m still struggeling with the language…
I will try to explain it as detailed as possible.

I’m trying to control a stepper with a LDR sensor.
I want it to take a certain amount of steps when it gets darker. Then when it has taken those steps i want it to check of it gets darker(then take more steps), stays the same (then do nothing) or gets lighter again(then go some steps back again). This untill it reaches it’s max light value from the sencor.

I succeeded to let the stepper work with the LDR sensor but not in the order i describe above. I think i might be easy to solve but i don’t know which functions to use…

Here’s my code:

int LDR = A1; //analog pin to which LDR is connected
int LDRValue = 0; //that’s a variable to store LDR values
int light_sensitivity = 350; //Step 1 (can only take certain steps forward)
int light_sensitivity2 = 500; //Step 2 (stay here (if light stays the same),or take further steps (if it gets darker), or move steps back when it gets light again.)
int light_sensitivity3 = 600; //Step 3 Wait untill value gets lower, then take steps back

#include “Stepper.h”
#define STEPS 32 // Number of steps per revolution of Internal shaft
int Steps2Take; // 2048 = 1 Revolution
// In1, In2, In3, In4 in the sequence 1-3-2-4

Stepper small_stepper(STEPS, 2, 4, 3, 5);

void setup()
Serial.begin(9600); //start the serial monitor
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);

void loop()
LDRValue = analogRead(LDR); //reads the ldr’s value through LDR
Serial.println(LDRValue); //prints the LDR values to serial monitor
delay(50); //This is the speed by which LDR sends value to arduino

if (LDRValue < light_sensitivity) //set steps when it gets darker
{ Serial.println(“kaas”);
small_stepper.setSpeed(700); //
Steps2Take = 2048; // Rotate CW

//here i want arduino to wait when the light sensitivity stays the same,takes steps back when it gets light again, or go further when it gets even darker
if (LDRValue < light_sensitivity2)
if (LDRValue < light_sensitivity3)


The code must remember which direction it last moved and what the light value was.

At this point you have proven that both the sensor and motor work, put those actions into self-contained functions. Then maybe write functions like left() and right() to further simplify. Your main code will then be much easier to write and understand.