Bang Bang Control

 int pa0 = 0;    
int pa4 = 0;    
int diff = 512; // The difference between white and black. Equal to about 512.  

void setup()  
{
}

void loop()
{
    pa0 = analog(0); 
    pa4 = analog(4); 
    
    if( pa4 < diff )        // if the right sensor touch of black line.
    {
        sr(80);             // spin right.  Power 80%
    }
    else if( pa0 < diff )   // if the left sensor touch of black line.
    {
        sl(80);             // spin left. Power 80%
    }
    else                    // if other. Straddling the line.
    {
        fd(100);            // move forward. Power 100%
    }           

    sleep(50);              // wait 50 millisec.
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Improved Bang Bang