something wrong in program logic.Please help

hi,
I am working on a project for which I need to check if blue color is detected.If detected THEN ONLY readings of ultrasonic sensor will be printed on hyper terminal and sent serially for further processing.

Now I have few issues:

Issue 1: I am checking if blue color is detected through if condition.

if(sensorValue>=1)
{…


}

since mostly output of color sensor module is below 1 volt when blue color is held against sensor. But then output was not correct…something was wrong.

so i observed output of sensor on serial port and then accordingly set threshold.

if (sensorValue<=100)
{…


}
and THIS worked…!!! I dont know …but I ahve feeling that something is wrong. Please help.

2)Issue 2: I wan to detect blue color and THEN start taking readings of ultrasonic sensor and next time when it encounters blue again stop taking readings for ultrasonic sensor…But current code scans for blue color every time and then only takes readings from ultrasonic sensor.

How to fix this??
I am attaching code

blue_ultra_edited15022015.txt (1.25 KB)

You are trying to read the reflected pulse whether you sent one or not, that is not going to work.

if(sensorValue >= 1 ) is almost guaranteed to be true because the analogue read returns a value between 0 and 1023 so the only time you do not give a pulse is when you have a very odd reading.

What is this sensor?

its color sensor module…bought it from local market.

well..i got answer for my 1st question...What can i do for second one....??

pardhimegha21: its color sensor module...bought it from local market.

I wish we had a market like that :)

...R

2)Issue 2: I wan to detect blue color and THEN start taking readings of ultrasonic sensor and next time when it encounters blue again stop taking readings for ultrasonic sensor.

So write the code like that and not like you have done. Use a state variable to control if you take readings. When the previous state of the colour sensor was not blue and the current state is blue then toggle this variable. Then simply use the variable to determine if you take readings.

can you elaborate a little more..??

This guy explains his sketch for a using a similar (not sure exactly how similar 8) ) sensor: it might help. Watch the video to understand his thought process.

pardhimegha21:
can you elaborate a little more…??

No not without some input from you. You have to say what about that last post you do not understand.

Hi,
Attached, spec for your sensor.
This is your colour sensor, please read it, the output is not analog.
So before we go off hypothesizing, you need to understand what your sensor is telling you.

The output is a square wave (50% duty cycle) with frequency directly proportional to light intensity (irradiance).

It has programming jumpers as well.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom… :slight_smile:

1847 (1).pdf (584 KB)

Hi, sorry , wrong model.

Sorry..Tom...... :'( I can't access the provotech site, my firewall won't let me.

@Grumpy_Mike: Hi ,ya sure...

you said that use state variable.. Do you mean .."use variable type static ,long etc.." or you mean "use flags...".. ??

Mike means state variable in the sense of flags. This example is typical of their use, where it’s important to see if (in the example) if the switch has just become pressed, as opposed to is pressed.

Is it okay to use static variable…???

void loop()
{ 
    static int flag=0;
      
        if(sensorValue<=100 && flag=0)
          { 
             start ultrasonic sensor;
             store reading;
              convert and display;
              flag =1;           
           }
         else
           { 
              start ultrasonic sensor;
              store reading;
              convert and display; 
              flag=1;

           }
}

Is it okay to use static variable

Almost compulsory if you don’t want to use a global one.

So why the if statement that pseudo code does the same thing irrespective of the outcome of the if statement.

Use a state variable to control if you take readings.
that means just use the state variable to decide if you take readings or not.

Then a different part of the code has you:-
When the previous state of the colour sensor was not blue and the current state is blue then toggle this variable.

Most if statements do not need an else associated with it. You do not need an else in the code you need to write either.

Hi How many of the sensor outputs are you monitoring? Just blue or all three? How blue is your blue target?

Tom.... :)

TomGeorge: Hi How many of the sensor outputs are you monitoring? Just blue or all three? How blue is your blue target?

Tom.... :)

right now ..Just one sensor output. And about "how blue..." : Color sensor which I am using,gives output in volts unlike traditional TCS3200 .So if you are excepting blue saturation in terms of TCS3200 then I cant provide that. But just to give you rough idea I am using sky blue.

Hi, Have you programmed a simple sketch to read the three outputs of the sensor and send them using serial.print to your serial monitor. This way you will see what the actual data is that you are dealing with.

Tom.... :)