Comparing Sensor Readings in NQC

Hey guys, I’m prototyping some designs using the LEGO Mindstorms, and Im using Not Quite C . I’m new to this language, and have never worked with C before. That said, I need some help. What I need is tips/help on creating a program that reads the raw value of SENSOR_1, saves it, and then takes another reading and compares it to the origional to see if there was a change. Basically i need a program to monitor the sensor, and then start another task if there is a change in the reading. Any help would be much appreicated.
P.S. If you are going to post code, please include comments that explain it, as i am trying to learn this language. Thanks.

I haven’t used NQC in like 2 years but if i remeber correctly you should be able to take the value of the sensor and store it in a variable and compare that variable to the current value, if the values are different then you can tell it to do something using an if else statement.

Look around on the net i know that there are some good tutorials out there if you look for them.

Goodluck and sorry i couldn’t be more helpful.

int rawvalue;

task main()
  rawvalue = SENSOR_1;
  until (rawvalue != SENSOR_1);

Alright another mindstormer!

I think the above code is what you need. I will explain it line by line:

int rawvalue; - creates a variable named rawvalue in which I will store to reading from sensor_1

SetSensor(SENSOR_1,SENSOR_LIGHT); - Sets sensor port 1 to a light sensor

rawvalue = SENSOR_1; - takes the value of sensor 1 and stores it in the variable

OnFwd(OUT_A) - turns motor A on and runs it forward

until (rawvalue != SENSOR_1); - the program will not continue until the rawvalue doesnot equal sensor 1 (In other words the sensor value has changed from when you last stored it)

OnRev(OUT_A); - once the value of sensor 1 changes motor A will be run in reverse

I hope this helps/is what you needed. If not just respond.

Yep, thanks guys. Thats basically exactly what i needed. Now i just need to write a function to convert the raw value of the sensor into an angle reading, and then another task for motor control. :slight_smile:

You’re welcome, glad I could help. But do you mind if I ask what it is that you are prototyping?

A camera-equipped rover to aid the mobility impaired, particually those in wheelchairs. I need that code for motor control, because although we are using an R/C remote for the link, im gonna need a microcontroller to mix the signals together, because im using tank-type steering.