Arduino Serial-Protocoder Questions

138 views
Skip to first unread message

mso797

unread,
Oct 10, 2014, 11:37:26 PM10/10/14
to proto...@googlegroups.com
Hey Victor,

Protocoder is awesome!

Im using it for a project of mine and I have two questions.

First, half the time when I run this bit of code
var arduino = boards.startArduino(9600, function(data) {
   text.setText(data);
});
I get the error "null pointer exception"
Is their any way that I can get rid of this error.

Second, I'm using protocoder to read a file on an http server and then forward that data over serial port to an arduino.
This program is able to do that(at least when it doesnt get the null exception error).
//read from http server 
network.httpGet("http://192.168.1.105:801/keyLog.txt", function(status, response) { 
    var text = ui.addText(" ", 0, 0);
    
    //set up arduino
    var arduino = boards.startArduino(9600, function(data) {
        text.setText(data);
    });
    
    //make variable equal to contents of file on http server
    var x = response[0];
    
    //write character to arduino over serial port
    if(x == "w"){
        arduino.write("w");
    }
    else if(x=="a"){
        arduino.write("a");
    }
    else if(x == "s"){
        arduino.write("s");
    }
    else if(x == "d"){
        arduino.write("d");
    }
    else if(x == " "){
        arduino.write(" "); 
    }
    else{
        arduino.write("j");
    } 
});

 I now want this program to repeat infinitely.
I tried to put the program in a while loop like this but the it crashes when  I try to run it.
//read from http server 
function main(){
    network.httpGet("http://192.168.1.105:801/keyLog.txt", function(status, response) { 
        var text = ui.addText(" ", 0, 0);
        
        //set up arduino
        var arduino = boards.startArduino(9600, function(data) {
            text.setText(data);
        });
        
        //make variable equal to contents of file on http server
        var x = response[0];
        
        //write character to arduino over serial port
        if(x == "w"){
            arduino.write("w");
        }
        else if(x=="a"){
            arduino.write("a");
        }
        else if(x == "s"){
            arduino.write("s");
        }
        else if(x == "d"){
            arduino.write("d");
        }
        else if(x == " "){
            arduino.write(" "); 
        }
        else{
            arduino.write("j");
        } 
    });
}

//repeating infinitely
while(1==1){
    main();
}
Is their any way that I can repeat the program infinitely. 


Sorry for such a long post.

Swaleh

Víctor Díaz

unread,
Oct 11, 2014, 10:22:41 AM10/11/14
to proto...@googlegroups.com
Hello Swaleh, 

1) In this section of code, 
var arduino = boards.startArduino(9600, function(data) {
   text.setText(data);
});
do you get the error if you remove the inner text.setText(data) line?
Another cause might be that your android doesnt recognize sometimes the arduino, might be? I had problems before with some usb OTG adapters. 

2) Trying to do a while() {} you will execute the funcion as fast as the processor can which is not a good idea! specially when the network operation is waaaaay slower than the rest of operations :) 
For this matter protocoder has a really handy function that repeats a function with a given frequency.

 // this function will repeat hello every 500 ms :) 
  var looper = util.loop(500, function() {
      console.log("hello");
   }); 

  //and here some handy functions 
  looper.stop();
  looper.pause(true);
  looper.setDelay(150);


I hope it works for you! and let me know more about the Arduino part. 


Btw: Just opened a Protocoder G+ community, come by and say hi! :D

Cheers 

Victor
    
Reply all
Reply to author
Forward
0 new messages