element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino + Processing = HELP
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 2 replies
  • Subscribers 391 subscribers
  • Views 208 views
  • Users 0 members are here
  • processing
  • sonar
  • arduino
Related

Arduino + Processing = HELP

Former Member
Former Member over 10 years ago

I'm am using a ultrasonic sensor make a sonar on processing. I have it all working except for the serial communications. processing thinks that the reading is always 57 then 17 then 10. I tested it out on Arduino serial  moniter and that works but for some reaso processing cant read it.

 

 

Processing:

 

import processing.serial.*;

import ddf.minim.*;
AudioPlayer sonar;
Minim minim;
float sensor;
Serial s;
void setup()
{

s = new Serial (this,9600);
minim= new Minim(this);
background(31,88,8);
size(720,360);
sonar = minim.loadFile("teamgrab.wav");
}

void draw()
{
if(s.read() == 0)
{
 sensor = s.read();
}
if(s.read() == 1)
{
  sensor = s.read() + (s.read() * 10);
}



float s = map(90, 0, 180, -PI, 0) - TWO_PI;
float lineX = cos(s) * 357.5 + height;
float lineY = sin(s) * 357.5 + width/2;
stroke(255);
strokeWeight(1.5);
line(height,width/2,lineX,lineY);
float pointX = cos(s) * sensor + height;
float pointY = sin(s) * sensor + width/2;
ellipseMode(CENTER);
noStroke();
fill(46,46,46);
//ellipse(height,width/2,720,720);
fill(13,39,3);
noStroke();
ellipse(height,width/2,40,40);
noStroke();
fill(31,88,8);
ellipse(pointX,pointY,4,4);



}

void setY()
{

}

void setX()
{

}

void placepoint()
{
noStroke();
fill(31,88,8);
ellipse(360,300,4,4);
}

 

 

 

 

 

 

 

arduino:

 

#include <NewPing.h>

#include <Servo.h>

NewPing sonar(A1,13,200);
Servo rotate;
int pos = 0;
int direction = 1;
int sense;
void setup()
{
rotate.attach(10);
Serial.begin(9600);
}
void loop()
{
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
sense = uS / US_ROUNDTRIP_CM;
Serial.println(sense);
/* if (sense > 0 && sense < 10)
{
 Serial.print(0);
}
if (sense > 9 && sense < 21)
{
 Serial.print(1);
}
if(sense> 0 && sense < 20)
{
Serial.print(sense);
}
if(sense > 20 || sense == 0)
{
Serial.print("u");
}// Convert ping time to distance in cm and print result (0 = outside set distance range)
if (rotate.read() < 10)
{
 Serial.print(0);
}
if (rotate.read() > 9 && rotate.read() < 100)
{
 Serial.print(1);
}
if (rotate.read() > 99)
{
 Serial.print(2);
}
Serial.println(rotate.read());*/
if(rotate.read() >= 180)
{
 direction = -1;
}
if (rotate.read() == 0)
{
 direction = 1;
}
pos += direction;
rotate.write(pos);
delay(10);
}

  • Sign in to reply
  • Cancel

Top Replies

  • markvenn
    markvenn over 10 years ago +1
    Hi Like Paul here I am afraid, don't have ultrasonic sensor but code looks OK to me with my limited experience in this field. I also noted the dual use of variable name s, change one of these, Am I right…
  • gadget.iom
    0 gadget.iom over 10 years ago

    I don't have a sonar sensor, so I can't test the application directly.

     

    I have noticed you define 's' twice in your processing code on line 11 and 31. This may be causing issues.

    You could rename one to prevent any confusion.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • markvenn
    0 markvenn over 10 years ago

    Hi

    Like Paul here I am afraid, don't have ultrasonic sensor but code looks OK to me with my limited experience in this field. I also noted the dual use of variable name s, change one of these, Am I right in thinking that this is java? I assume that this is why we start with import rather than #include? How about the setX() and setY() functions? I know they are empty but I am assuming that they will be used later and are just there as placeholders.

     

    What happens after the reading of 10? Does it just stop or carry on decreasing? I wondered if there was some kind of correlation between the rate of decay.

    50 - 17 - 10 - ????

    float s = map(90, 0, 180, -PI, 0) - TWO_PI;  
    float lineX = cos(s) * 357.5 + height;  
    float lineY = sin(s) * 357.5 + width/2;

    this is after s has been set for serial comms.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube