Record and repeat bot

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
groskiff1994
Posts: 5
Joined: 06 Oct 2012, 20:57

Record and repeat bot

Post by groskiff1994 »

Hello,
I wanted to share a little program I made (really small ^^). It records several push & release of a touch button (port n°1) and repeat it at the exact same rate and length (with beeps).
So there is the video :
http://www.youtube.com/watch?v=d8C8CcKC_d4
And there is the source :

Code: Select all

#define VOL 3
#define TMELO 100

struct t_note
{
    unsigned int length;
    unsigned int silence;
};

struct t_melody
{
    int nbNote;
    t_note notes[TMELO];
};

void playNote(t_note n)
{
    PlayToneEx(500,n.length, VOL, false);
    Wait(n.silence);
}

void addNote(t_melody &m, t_note n)
{
    m.notes[m.nbNote]=n;
    m.nbNote++;
}

bool empty(t_melody &m)
{
    return m.nbNote==0;
}

bool full(t_melody &m)
{
    return m.nbNote==TMELO;
}

/*t_note retirerNote(t_melody &m){
       m.nbNote--;
       return m.notes[m.nbNote];
}*/

void playMelody(t_melody &m)
{
     if(!empty(m)){
                 int i;
                 for(i=0; i<m.nbNote; i++){
                          playNote(m.notes[i]);
                 }
     }
}

void recordMelody(t_melody &m){
     t_note tmp;
     tmp.length=0;
     tmp.silence=0;
     
     t_note app;
     app.length=0;
     app.silence=0;
     
     long beg;
     long end;
     long next;
     
     bool done=false;
     bool pressed=false;
     while(!done)
     {

        /* nouvel appui */
        if((SENSOR_1 == 1) && !pressed)
        {
            pressed=!pressed;
            if(tmp.length != 0)
            {
                next=CurrentTick();
                tmp.silence=next-beg;
                addNote(m, tmp);
            }

            beg=CurrentTick();

        }
        /* relachement */
        else if((SENSOR_1 == 0) && pressed)
        {
            pressed=!pressed;
            end=CurrentTick();
            tmp.length=end-beg;
        }

        if(ButtonPressed(BTNCENTER,false))
        {
            done=true;

            if(pressed)
            {
                end=CurrentTick();
                tmp.length=end-beg;
                tmp.silence=tmp.length;
            }
            else
            {
                next=CurrentTick();
                tmp.silence=next-beg;
            }
            addNote(m, tmp);
        }

        /* play le son tant que la touche est appuyée */
        if(pressed)
        {
            playNote(app);
        }
    }
}

task main()
{
    SetSensorTouch(S1);
    
    t_melody myMelo;
    myMelo.nbNote=0;
    ArrayInit(myMelo.notes, 0, TMELO);
    
    recordMelody(myMelo);
    Wait(1000);
    playMelody(myMelo);
}
that's it, it may be useful for some :)

hope you enjoyed it.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests