• Light sensor program in C

    WinMan Member

    Beginner in C here and was tasked with working with a light sensor (attached to a Lego Mindstorms Robot) in C.

    I am needed to work with 3 objects that vary different light values and therefore I am using an array and working with if statements to declare what action to take between certain values that the objects are defined.

    All of these commands work apart from one,when hitting an object (2), the black line, which when hit the robot is meant to reverse and change direction.

    The code used within that area:

    if((SensorValue[myLight]) <= (colour [2]+ 7)) { // if black line found
             count [2]++; // count array
             motor[motorB] = -35;// reverse
             motor[motorC] = -35;
              wait10Msec(100);//for 1 second
             motor[motorB] = 0;//turn right
             motor[motorC] = 35;
             wait10Msec(random(100)+150); //for a random interval but at least 1.5 sec

    The actions performed here do work but only sparsely and was wondering if anyone could provide a quick and easy solution.

    And to those wondering the average value of array [2] is normally 30-35, while the next closest light value is between 45-50

  • Adelaid Member

    The code looks reasonable, assuming that the motor commands are correct. This would indicate that something else is wrong. Possible problems are:

        myLight points to the wrong sensor
        SensorValue[myLight] returns a value other than what you expect
        colour[2] contains the wrong value

    One other possibility is that the preceding code has an error that causes this code to be skipped.

  • SapnaVishwas Member

    myLight is connected to the correct sensor, as for your other two points I am unsure how it can contain or return a different value as future strings of code that execute the colour[0] and [1]’s specific requirements work as expected and the method of recording those values are the same as colour[2]

    Currently looking to see if any possible errors could be caused to skip the code, so here’s the preceding section for those to gander at.

    task main() {
      //variable setup//
      int colour [3];
      int count [3];
      bool exit_cond = false;
      int range = 5;
      //end variable setup//
      Array Value 1 (0): Light Square (end game)
      Array Value 2 (1): Dark Square (counting)
      Array Value 3 (2): Black Line (wall)
      ////COUNT ARRAY VALUES////
      Array Value 1 (0): coloured square
      Array Value 2 (1): sonar avoidance
      Array Value 3 (2): wall bounce
      while(true){    //     loads the array with light values for colour
        for(int i = 0;i < 3; i++){
            if (SensorValue[myTouch] > 0) {
              colour [i] = SensorValue[myLight];
        nxtDisplayString(1,"Value: %i", colour [0]);  //Display array Value 1 (0): Light Square
        nxtDisplayString(2,"Value: %i", colour [1]);  //Display array Value 2 (1): Dark Square
        nxtDisplayString(3,"Value: %i", colour [2]);  //Display array Value 3 (2): Black Line
        if (SensorValue[myTouch] > 0) {exit_cond = true;}
        else {exit_cond = false;}
      while(true) {
        motor[motorC] = 30; // initial motor speed setup
        motor[motorB] =30; // initial motor speed setup
        if(SensorValue[mySonar] < 25){ // turns away 90* left if distance less than 30
          count [1]++;
          motor[motorC] = 0;
          motor[motorB] = 30;
          motor[motorB] = 0;
  • Abhey Member

    It looks like sensor problem. I don’t know what kind of sensors are used in lego mindstorm, and how thick are your lines. It might be that sensor exposure to object is to short. So try to slow down your robot. If then it will start to act properly, it was a sensor problem.

    If so, you can try different programing approach, based on digital signal processing (if it’s possible in lego mind storm).

Viewing 3 reply threads
  • You must be logged in to reply to this topic.