Line follower Array v1.0 in ultimate 2.0


#1

Hi, can somebody help me with line follower array in ultimate 2.0. I don’t know how to program it in arduino IDE and there are no extension in mBlock. I have look at sample program (https://store.makeblock.com/me-line-follower-array) but i don’t get what the function do getValue do and how to make something. So if you can give me some link with better example or something else i will be thankful.


#2

Here is the sample program with some comments:

// select data pin 2 for the sensor connection
const int DataPin = 2;

// declare an array of unsigned short integers for the sensor data
uint8_t Sensor_Data[3];

void setup() 
{
    // open the serial port at 9600-N-8-1
    Serial.begin(9600);

    // wait one second
    delay(100);

    // print the start message to the serial monitor
    Serial.println("Start.....");
}

void loop()
{
    // print the value retrieved from the sensor
    Serial.println(getValue(),BIN);

    // wait one second
    delay(100);
}

uint8_t getValue()
{  
    long time_out_flag = 0;

    // set the pin mode to output and set the pin value to LOW
    pinMode(DataPin, OUTPUT);
    digitalWrite(DataPin, LOW);

    // wait .98 seconds then set the pin value to HIGH
    delayMicroseconds(980);
    digitalWrite(DataPin, HIGH);

    // wait 40 ms
    delayMicroseconds(40);

    // set the pin to a known good pullup value for a baseline
    pinMode(DataPin, INPUT_PULLUP);

    // wait 50 ms
    delayMicroseconds(50); 

    // prepare a non-blocking timer
    time_out_flag = millis();

    // get the pin value
    while((digitalRead(DataPin) == 0)&&((millis() - time_out_flag) < 6)); 
    time_out_flag = millis();

    // get another reading for the pin value
    while((digitalRead(DataPin) == 1)&&((millis() - time_out_flag) < 6));

    
    for(uint8_t k=0; k<3; k++)
    {
        // zero out the array element
        Sensor_Data[k] = 0x00;
        
        for(uint8_t i=0;i<8;i++)
        {
            // set the timeout flag
            time_out_flag = millis(); 

            // wait for 6 ms (non-blocking)
            while(digitalRead(DataPin) == 0&&((millis() - time_out_flag) < 6));

            // get the current time in microseconds
            uint32_t HIGH_level_read_time = micros();

            // reset the timeout flag
            time_out_flag = millis();

            // wait for 6ms (non-blocking) 
            while(digitalRead(DataPin) == 1&&((millis() - time_out_flag) < 6));

            // get the elapsed interval in microseconds
            HIGH_level_read_time = micros() - HIGH_level_read_time;

            // if the elapsed time is between 50-100 microseconds
            if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100)  
            {
                // 'or' the value into the array element using
                // a right shift by the current loop counter value
                Sensor_Data[k] |= (0x80 >> i);
            }
        }
    }

    // test to see whether the value of array element one is the 
    // same as the negated value of array element zero.
    if (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0]))
    {
       // if the values are the same return the value of array element zero
       return Sensor_Data[0];
    }
}

I would suggest that you compile and load the program then test it by putting white and black strips under the sensors. Often this is the only real way to understand what’s going on.

In short, after opening the serial port at 9600-N-8-1 for output, the program goes into a loop that continuously reads the line array sensor ever 100 milliseconds.

The code above does not have the include file called out because the code itself can be used for any of the microcontroller boards so you’ll need to put an #include <MeMegaPi.h> at the top of the file to compile the code for the MegaPi board.

Before anyone gets up in arms over the lack of comments in the example, please remember that it is difficult to write comments that translate across multiple languages.


#3

@chuckmcknight thank you for reply, I will try it :slight_smile: :slight_smile:


#4

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.