Servo in 2 positions with ldr

Hello,

I’m new on the forum and pretty new to arduino!

I’ve a question about my programm which I can’t get to work properly.

For my study i’m currently making a prototype in which 5 LDR’s and 4 Servo’s are integrated. I want to use a truth table to keep the overview.

Currently I try to make a programm for only one LDR and one servo but I can’t manage it to work. If this works I want to include the other LDR’s and Servo’s too.

What I try to do (in the end) in this program is to let the software read the truth table and checks which states the different ldr’s are and assign this to the different truth tables of the 4 servo’s.

e.g.
LDR5 LDR4 LDR3 LDR2 LDR1
0 0 0 0 0
0 0 0 0 1
0 0 0 1 0
(and so on, till all combinations are made)

for the servo’s the same

Servo 1 (2)

LEFT MID (RIGHT)
0 0 (1)
0 1 (0)
1 0 (0)

I hope that my explanation is understandable there my english isn’t very good!

current programm:

int LDR_1=A1;
int iIndex = 0;
int iServoSetting;
int iSelect;
#define THRES 20
#include <Servo.h>;
Servo SERVO_1;
#define LEFT  45
#define MID  60

int iWaarheidstabel_LDR1_1[2][1]={
  {0},
  {1}};
  
  
int iWaarheidstabel_SERVO_1[2][2] ={
{      0      ,      1      }      ,
{      1      ,      0      }      };
  
  void setup()
  {
    SERVO_1.attach(2);
    Serial.begin(9600);
  }
  
  void loop()
  {
    iIndex = analogRead(LDR_1);
   // iIndex = iIndex + readLDR(2) *2;
    //iIndex = iIndex + readLDR(3) *8;
    //iIndex = iIndex + readLDR(4) *16;
    //iIndex = iIndex + readLDR(5) *32;
    iServoSetting = iWaarheidstabel_SERVO_1 [iIndex][iIndex];
    SERVO_1.write(iServoSetting);
    delay(100);
    Serial.println(iIndex, DEC);
    
    
    switch(iSelect)
    {
      case '1':
            if(analogRead(LDR_1)>THRES)
              iWaarheidstabel_SERVO_1[2][2] = LEFT;
             
            
            else
              iWaarheidstabel_SERVO_1[2][2] = MID;
            
       break;
    }
  }

Thanks a lot in advance!

Kind regards,

Niels :smiley:

    iIndex = analogRead(LDR_1);

The analogRead() function returns a value between 0 and 1023.

    iServoSetting = iWaarheidstabel_SERVO_1 [iIndex][iIndex];

Your array is nowhere near big enough.

You are using the same value for the first and last index into the array? Why? What will ever only pick the values on the diagonal, defeating the purpose of a 2D array.

@ PaulS

Thanks for the fast reply.

But how should I improve the code to pick each value properly?

My knowledge about coding isn’t really good, therefore I need some help by understanding and writing code.

Thanks in advance!

Your first problem isn't with software. It is a hardware problem. You are expecting the LDR to provide a binary output - on or off. That is not what it does. It provides a value that tells you how much light it is seeing.

I want to use a truth table to keep the overview.

I don't understand this statement.

What I try to do (in the end) in this program is to let the software read the truth table and checks which states the different ldr's are and assign this to the different truth tables of the 4 servo's.

It sounds like what you want to do is move one or more servos to specific positions based on which LDRs are seeing enough light.

If that is the case, you need to define some threshold that defines what constitutes enough light. Then, you can set a bit on or off, based on the actual value output by the sensor. Perhaps the bitSet() function would be useful.

Sensor 1 sets bit 1, Sensor 2 sets bit 2, etc. This gives you 31 different values, with no lookup table required. Each value corresponds to a different set of servo positions. A switch statement with 31 cases, or 4 arrays, one for each servo, with 31 values in each array, would allow you to set the servos exactly where you want them.

Personally, I'd use the array method.

That's true (about the analog signal of the LDR's) but I tried to define a treshold, #define THRES. So when the LDR > THRES gives a binary output of 1.

What I try to do is the following. e.g. if LDR 1 is seeing enough light if LDR 2 is seeing enough light if LDR 3 isn't seeing enough light etc.

Servo 1 goes to position 1 Servo 2 goes to position 3 Servo 3 goes to position 2 etc.

Regards,

if LDR 1 is seeing enough light if LDR 2 is seeing enough light if LDR 3 isn't seeing enough light

There are 31 possibilities: LDR 1: 0 1 0 1 0 ... LDR 2: 0 0 1 0 0 ... LDR 3: 0 0 0 1 0 ... LDR 4: 0 0 0 0 1 ... LDR 5: 0 0 0 0 0 ...

Using a byte variable, index, and 5 calls to bitSet, where the arguments are the LDR # and 1 or 0, depending on whether the light value is high enough or not, you can set index to each of the 31 possible values.

Then, you can have 4 arrays for servo positions:

int servoPos1[] = {0, 10, 20, 0, 10, 20, ... 150}; // 31 values int servoPos2[] = {10, 20, 0, 10, 20, 0, ... 150}; // 31 values int servoPos3[] = {20, 0, 10, 20, ..., 140, 145, 150}; // 31 values int servoPos4[] = {30, 20, 10, 0, 10, 20, ... 150}; // 31 values

Then, it is just a matter of setting servoN to position servoPosN[index]

servoN.write(servoPosN[index]);

Thanks a lot of times PaulS,

Hope this is going to work! :D

Kind regards,

Niels