Quick Programming Question

In EasyC v2, how do I save a number from say a light sensor or ultrasonic sensor in a variable to use later in the program. Is this possible?

Yes…


#include "Main.h"

void main ( void )
{
      unsigned int ultrasonic_001 = 0; 
      unsigned int light_001 = 0; 

      StartUltrasonic ( 1 , 1 ) ;
      ultrasonic_001 = GetUltrasonic ( 1 , 1 ) ;
      StopUltrasonic ( 1 , 1 ) ;
      light_001 = GetAnalogInput ( 1 ) ;
      while ( 1 ) // Loop Forever
      {
            if ( light_001 <= 300 )
            {
            }
            if ( ultrasonic_001 <= 250 )
            {
            }
            else
            {
            }
      }
}


If you need the stored Variable in a different Function from the one that it was saved in, the Variables will either need to be Global, or you will need to Pass the Variable to the Function that you are using it in.

If I use:

      StartUltrasonic ( 1 , 1 ) ;
      ultrasonic_001 = GetUltrasonic ( 1 , 1 ) ;
      StopUltrasonic ( 1 , 1 ) ;

It doesn’t save the number in the variable for me. o.O

It should. Could you paste your exact source code in the forum so we can see it?


      {
            // Get base data 
            StartUltrasonic ( 1 , 1 ) ;
            Base = GetUltrasonic ( 1 , 1 ) ;
            StopUltrasonic ( 1 , 1 ) ;
            while ( 1 == 1 )
            {
                  // Get values 
                  StartUltrasonic ( 1 , 1 ) ;
                  StartUltrasonic ( 2 , 2 ) ;
                  Ultra1 = GetUltrasonic ( 1 , 1 ) ;
                  Ultra2 = GetUltrasonic ( 2 , 2 ) ;
                  if ( Ultra1 < Base )
                  {
                        Clockwise ( ) ;
                  }
                  if ( Ultra1 > Base )
                  {
                        CounterClockwise ( ) ;
                  }
                  if ( Ultra2 < Base )
                  {
                        CounterClockwise ( ) ;
                  }
                  if ( Ultra2 > Base )
                  {
                        Clockwise ( ) ;
                  }
                  else if ( Ultra1 == Base && Ultra2 == Base )
                  {
                        Left ( ) ;
                  }
            }
      }

The Clockwise, CounterClockwise, and Left are user functions.

Following the instructions in the UltraSonic Sensor Kit - Inventors Guide Insert, there is some Background information.

But Double Clicking on the
[FONT=“Courier New”]StartUltrasonic ( 1 , 1 ) ;
or
GetUltrasonic ( 1 , 1 ) ;
or
StopUltrasonic ( 1 , 1 ) ;[/FONT]

in EasyC will bring up a Dialog Box, which then you can click on the Help Button, showing a Screen like this, Ultrasonic Sensor.

These are obviously at odds with one another, because the “UltraSonic Sensor Kit - Inventors Guide Insert” shows both the Ultrasonic connectors in the Interrupts Ports, while EasyC v2. shows one connector in the Interrupt Port, and one in the Analog/Digital Port. Also, the Input and Output connectors are different, and must be in the Correct Port.

IIRC, the Input needs to be in an Interrupt Port, and the Output needs to be in a Digital Output Port. Also remember that the Analog Input Ports must be the Lowest Ports, so if you need 6 Analog Inputs, the first Digital Port available would be Port 7.

So with Port 7 for the Output, and Port 1 for the Input, you would need to change all the Ultrasonic Functions to [FONT=“Courier New”](1, 7)[/FONT]

i.e.
[FONT=“Courier New”]StartUltrasonic ( 1 , 7 ) ;
or
GetUltrasonic ( 1 , 7 ) ;
or
StopUltrasonic ( 1 , 7 ) ;[/FONT]

Give this a try, and don’t hesitate to ask questions… :wink:
Intelitek EasyC V2 - Ultrasonic Sensor.pdf (55.8 KB)