Pages: 1 [2]   Go Down
Author Topic: Serial.available acting funny  (Read 1870 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think its more of a format issue.

If you don't want to fix the problems being pointed out then it seems pointless taking the effort to try to help you find them.

woah there buddy. thats why I said "Will try both solutions tonight."
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You should be able to send and receive on the same serial port.

In your code you only seem to be trying to read from the Serial port when the distance reading is in range. I don't know why you're doing that - is it deliberate? Do you know that the value actually is in range? You need to know what's going on inside your sketch, and to understand that you really need to know what it's outputting to the Serial port and what your Processing application is writing to it.

For simplicity while testing, you could set the Processing application aside and use the Arduino serial monitor to send input to the Arduino and display whatever the Arduino outputs.

yeah I know it's in range. It works fine on arduino alone, it's when I send data to processing and get it back that is the issue. if you fall below 400 it causes inaccurate readings so I had to cap it. I was monitoring the serial connection and the values were between 400 and 750, I didnt want the motor jumping around all over the place because of an analog signal, so I capped it. I feel I have a good understanding what is *basically* going on other than recieving data, this is still forign to me.

I have already tested this all on arduino, everything is fine. I agree to start simple, I always try to start simple and build up, I got stuck at the part where I send data back from processing.
« Last Edit: October 22, 2012, 07:27:02 pm by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code doesn't compile:

Code:
sketch_oct22b.cpp: In function 'void loop()':
sketch_oct22b:54: error: expected `}' at end of input

Please note that, at present, the String library has bugs as discussed here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).


oh shoot nick you're right! I had JUST made an edit and commented out a "}" and a conditional, but somehow only undid the conditional. code is "fixed" (at least compiles, but isn't fixed fixed) now, and it compiles again--sorry about that.
« Last Edit: October 22, 2012, 07:23:43 pm by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 float distance = analogRead(1);
The analogRead() function returns an int. Why are you storing that in a float?

Code:
 //map float to an int
  int x = (int) distance;
There's no "mapping" going on here.

Code:
           data[counter] = String(serialDataIn);
The serialDataIn variable is already a String. There is no reason to make another String instance, and then invoke the copy constructor on that instance, and then delete that instance. That is just uselessly gambling with an already dodgy class.


ooo, I was under impression that arduino needs that as a float since its reading volts, guess I was wrong in my assumption. That actually brings up a weird issue I am not noticing where I am casting the float to an int then in processing going all over the place too. I think it was left over from when I had it just in arduino and was trying to smooth the analog signal (without much success so I moved on for sake of keeping it simple). I had converted it to int so there wouldn't be as much floating point randomness.

the mapping happens in processing here:

edit: posted most up to date code below.
« Last Edit: October 22, 2012, 08:39:28 pm by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Couple of remarks, hope they can help.

First, the line '  //}' seems weird, with the comment marks it does NOT compile. Taking out the '//' compiles ok.

Next, does it actually print the "length" string? If x is not inside your range, nothing is done, whatever you receive.

Then, the line 'float distance = analogRead(1);', again hmm, analogRead returns a int, not a float. Not critical, but not clean.

But, most critical, you are not looping yourself, but using the Arduino background code to repeatedly call loop(). This means you are not in full control over what the system is doing. Even in the Arduino the microcontroller 'rule' the main function never exits is still a good one to follow.

Now, i am not sure, but it looks like the serial buffer is reset upon entering loop().

If i place the main block in a while(true) { .. } loop, it seems to work; at least it sees the incoming data etc.

Oh, and finally, the lines
        // say what you got:
        Serial.print("I received: ");
        Serial.println(incomingByte, DEC);
make no sense, inComingByte is initialise to 0 and never set.


HTH,
Guido

Yes, you are totally right, I just noticed the weird "//}" as well when I went to check if it compiles. It does print the length string. X shouldn't be in range-- all the values outside of that are nonsense (it's an IR sensor), so I just filter them out.

Ah...I see what you are saying, really I could just do
Code:
int x = analogRead(IRpin);
right? that is a bit silly now that I look at it...I think I was assuming it comes in a float as default, but its probably bytes...? I don't know much about this language...I come from JS background, so this is really foreign to me. I was following some code posted up on stack exchange, but apparently with no luck and I should have caught that silly error too, I didn't notice it was being reset. I usually use split to split this type of data, but I have no idea how to do it in C style

this part confused me the most because I am still terrible at using bytes. Not quite sure how to match "Data" in bytes, or even what best practice is. I'm ok with using bytes if this is fast and standard.

Code:
if(inbyte >= '0' && inbyte <= '9')

the serial coming back into arduino looks something like this: Data,0,20,
where second data set "0" would be between 0 and 180 ( I map this in processing)
« Last Edit: October 22, 2012, 08:00:10 pm by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

here is the updated code I am working with, simplifying it even more--I took out the bit about parsing it, to see if it was a data type issue. I still need to convert string to a c style string (completely new concept to me, so its a lot to take in at the moment)

arduino
Code:
//SERVO stuff
#include <Servo.h>
Servo myservo;  // create servo object, max 8 servo objects

//IR portion
int IRpin = 1;
//int IRpin2 = 2;

// incoming serial data from processing
String serialDataIn;
String data[3];
int counter,
    inbyte,
    newPosX;

void setup() {
  Serial.begin(9600);
  myservo.attach(3);  // server is on pin 3
  counter = 0;
  int pos = 0;
}

void loop() {
  //float distance = analogRead(IRpin);
  int x = analogRead(IRpin);
  int y = 20;

  Serial.print("Data,");
  Serial.print(x);
  Serial.print(",");
  Serial.print(y);
  Serial.print(",\n");

  // SERVO stuffs
  //int incomingByte = 0;
  if( x > 0 && x < 180){
    //Serial.write("passed");
    if (Serial.available() > 0) {
        Serial.write("serial avail");
        inbyte = Serial.read();
        /*
        if(inbyte >= '0' && inbyte <= '9')
            serialDataIn += inbyte;
        if (inbyte == ','){  // Handle delimiter
            data[counter] = String(serialDataIn);
            serialDataIn = String("");
            counter = counter + 1;
        }
        if(inbyte ==  '\r'){  // end of line
          //myservo.write(x);
        }
        */     
        // say what you got:
        newPosX = (int) inbyte;
        //Serial.print("I received: ");
        //Serial.println(incomingByte, DEC);
        myservo.write(inbyte);
    }
    delay(25); 
  }
}


everything seems to come over fine (if I print "data array") except the end of the line I get "-65", which might be the line break...? One weird thing I am noticing is I have no idea how I am getting an array...I don't push and values at any point, does the serial connection compile an array every time something is printed, then send it at end of loop? or is that what bufferUntil does? sorry this serial stuff is really new to me still...Starting to realize there are a lot of gaps in my understanding of how it works.

Quote
data :[0] "Data"
[1] "328"
[2] "20"
-65
data :[0] "Data"
[1] "328"
[2] "20"
-65
data :[0] "Data"
[1] "303"
[2] "20"
-65

Processing:

Code:
import java.awt.AWTException;
import java.awt.Robot;
import processing.serial.*;
import java.awt.MouseInfo;
import java.awt.event.InputEvent;
import java.awt.PointerInfo.*;

Serial myPort;   // Create object from Serial class
int windowHeight = 500;
int windowWidth = 500;
int lastVal = 1000;
int currentVal,
    posX,
    meetsThreshold;
float posXf, mappedPosXf;

void setup() {
  myPort = new Serial(this, Serial.list()[4], 9600);
  myPort.bufferUntil('\n');
  size(windowWidth, windowHeight);
}

void draw(){
}

void serialEvent(Serial p) {
  String message = myPort.readStringUntil('\n'); // read serial data
  if(message != null) {
    String [] data  = splitTokens(message,",\n"); // Split the comma-separated message
    if (data[0].equals("Data")){
      posX = Integer.parseInt(data[1]);
      print("X:"); println(posX);
      if( data.length > 2){
        posXf = float(posX);
        mappedPosXf = map(posXf, 150, 740, 0, 180);
        posX = int(mappedPosXf);
        println(posX);
        myPort.write(posX);
      }
    }
  }
 
}

int downSample(int handSensor) {
    if (handSensor % 3 == 0){
      int meetsThreshold = 1;
      println("meets threshold");
    } else if (handSensor % 3 != 0){
      meetsThreshold = 0;
      println("doesnt meet threshold");
    }
    return meetsThreshold;
}

cleaning it up a bit, I realized that I still really have no idea how to monitor serial connection when I am sending and receiving to arduino. What is the most viable way of doing this? I cannot open serial monitor because it sends two sets of data...I could debug easier if I could monitor it :/
« Last Edit: October 23, 2012, 12:56:35 am by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/serial
Logged


Offline Offline
Edison Member
*
Karma: 29
Posts: 2455
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"Serial.available acting funny"

Was it doing standup? - smiley-grin
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"Serial.available acting funny"

Was it doing standup? - smiley-grin

terrible breakfast humor actually..... *birum chi*
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


:O this is good stuff. I like the other post you linked as well. thanks!
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Pages: 1 [2]   Go Up
Jump to: