Mosquito demo

This is a demo showing a swarm of mosquitoes flying around an the uLCD screen. The mosquitoes are normally calm but start flying around sporadically when their nest is perturbed (something comes close to the Sharp IR distance sensor).

include the mbed library with this snippet

#include "mbed.h"
#include "uLCD_4DGL.h"

uLCD_4DGL uLCD(p9,p10,p11);//p28, p27, p29); // create a global lcd object

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

AnalogIn   Sensor(p20);

double dist(int x0, int y0, int x1, int y1) {
    double arg = (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0);
    return sqrt(arg);    
}

#define N_BLOBS 50
int xs[N_BLOBS];
int ys[N_BLOBS];
int colors[N_BLOBS];

int main() {
    int i;
    int r;
    int side;
    int grav_x = 64;
    int grav_y = 64;
    int spread = 10;
    float pertub = 0;
    int grav_change_count = 0;
    double distance;
    int update_time = 0.1;
    for(i = 0; i < N_BLOBS; i++) {
        xs[i] = rand()%127;
        ys[i] = rand()%127;
        dist(64, 64, xs[i], ys[i]); // Find distance from center of screen.
        colors[i] = distance > 50 ? RED : distance > 25 ? GREEN : BLUE;
    }
    
    set_time(1256729737);
    
    uLCD.text_width(2); //4X size text
    uLCD.text_height(2);
    uLCD.color(RED);

    led1 = 0; 
    led2 = 0; 
    led3 = 0; 
    led4 = 0; 
    while(1) {
        time_t seconds = time(NULL);
        
        uLCD.locate(1,2);

        if(grav_change_count++ > 30) {
            grav_change_count = 0;
            //grav_x = rand() % 127;
            //grav_y = rand() % 127;
        }
        
        update_time = 0.009/(Sensor+0.00001); // Max update rate is 0.009, min rate is 900.
        pertub = 10*Sensor;

        for(i = 0; i < N_BLOBS; i++) {
            uLCD.pixel(xs[i],ys[i],BLACK);
            //r = (rand() % 4);
            r = (rand() % 4)*(1 + pertub);
            side = rand() > (RAND_MAX/2);
            
            xs[i] -= (xs[i] - grav_x)/spread; // += or -= determines replusive or attractive force
            xs[i] += side ? r : -r;
            xs[i] = xs[i] < 0 ? 0 : xs[i] > 127 ? 127 : xs[i];
            r = (rand() % 4)*(1 + pertub);
            side = rand() > (RAND_MAX/2);
            
            ys[i] -= (ys[i] - grav_y)/spread;
            ys[i] += side ? r : -r;
            ys[i] = ys[i] < 0 ? 0 : ys[i] > 127 ? 127 : ys[i];
            
            distance = dist(64, 64, xs[i], ys[i]); // Find distance from center of screen.
            colors[i] = distance > 50 ? RED : distance > 25 ? GREEN : BLUE;
        
            uLCD.pixel(xs[i],ys[i],colors[i]);
        }

        //        led1 = (Sensor > 0.2) ? 1 : 0;
        //led2 = (Sensor > 0.4) ? 1 : 0;
        //led3 = (Sensor > 0.6) ? 1 : 0;
        //led4 = (Sensor > 0.8) ? 1 : 0;
        //wait(0.1);
        //led1 = !led1;
        wait(update_time);
//        uLCD.cls();
    }
}


Please log in to post comments.