Analog data via UDP?

Hello,

I have finally managed to get my Ethernet shield talking to my computer. I am using the “UDP receive” object in Max/MSP but can’t seem to work out how to convert and send my photocell reading to my computer.

I have the reading in the serial monitor, but can anyone tell me how to get it to my PC?

Here is what I have so far

#define LAZER 2           //LAZER connected to digital pin 2
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider

#include <SPI.h>
#include <Ethernet.h>
#include <Z_OSC.h>

byte myMac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x4B, 0x9F };//MAC address of Sebastian's Ethernet sheild
byte myIp[]  = { 192, 168, 1, 66 };//IP address of Sebastian's Arduino

byte destIp[] =  { 192, 168, 1, 64 };//IP address of Tom's wireless router
int  destPort = 10000;



char oscAdr[] = "/z-osc/test/123";//message to send
char oscAdr2[] = "/z-osc/test2/abc";//message to send

int      iCount  = 0;     
long int liCount = 0;      
float    fCount  = 0.0;    
char     str[]   = "abcd"; 

Z_OSCClient client;

void setup(){
  
  pinMode(LAZER, OUTPUT);
  Serial.begin(9600);
  Ethernet.begin(myMac ,myIp);  
  
}

void loop(){
  
  digitalWrite(LAZER, HIGH);  //turn on LAZER
  photocellReading = analogRead(photocellPin);
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);
  
  sendProcess();
  
  Z_OSCMessage mes;  
  
  mes.setAddress(destIp,destPort);
  mes.setZ_OSCMessage(oscAdr2 ,"s" ,"55");
  
  
  client.send(&mes);
  
  mes.flush();  
  
  delay(100);
}


void sendProcess(){
  
   
  long int tmp=(long int)iCount; 
  
  Z_OSCMessage message;  
  
  message.setAddress(destIp,destPort);
  message.setZ_OSCMessage(oscAdr ,"iifs" ,&tmp ,&liCount ,&fCount ,str);
  
  client.send(&message);


  if(iCount++  > 1000)  iCount =0;
  if(liCount++ > 1000)  liCount=0;
  fCount += 0.1;
  if(fCount  > 100.0) fCount =0.0;
  
}

Hello,

I am a beginner and indeed don't understand what all the lines of code are doing, I cobbled this together using the "simple send" example from the Z_OSC library here Arduino Playground - HomePage and a sketch for using a photocell as a light sensor I got from here How to use photocells, LDRs, CdS cells, photoresistors!

Any help much appreciated

I have managed to get the "udpreceive" in Max/MSP to receive the following message.

/z-osc/test2/abc 55

I can tell that this comes form these 2 lines of code:

char oscAdr[] = "/z-osc/test/123";//message to send
char oscAdr2[] = "/z-osc/test2/abc";//message to send

and this

mes.setZ_OSCMessage(oscAdr2 ,"s" ,"55")

I have tried replacing the "55" with "photocellReading in the same way that serial print does but no joy

I have tried replacing the "55" with "photocellReading in the same way that serial print does but no joy

How have you tried to replace the value "55" with the photocellReading variable?

What are the required arguments for Z_OSCMessage::setZ_OSCMessage()? If the third argument must be a string, you can use itoa() to convert the value in photocellReading to a char array that Z_OSCMessage::setZ_OSCMessage() will accept.

What does no joy mean, anyway? What happened when you tried to change the code? Did the compiler complain? Did the wrong data get sent? Did the wring data get received?

  digitalWrite(LAZER, HIGH);  //turn on LAZER

Once it's on, it stays on. It is not necessary to turn the pin on in every pass through loop.

Thanks Paul,

you can use itoa() to convert the value in photocellReading to a char array that Z_OSCMessage::setZ_OSCMessage() will accept.

itoa() was indeed the answer! I was also missing a whole sendProcess0(); command. Everything works perfectly now.

What does no joy mean, anyway?

It means I had no luck

digitalWrite(LAZER, HIGH); //turn on LAZER
Once it’s on, it stays on. It is not necessary to turn the pin on in every pass through loop.

I don’t see why? Anyway I’ve just connected it directly to power and ground now.

Here’s the working code

#include <SPI.h>
#include <Ethernet.h> // version IDE 0022

#include <Z_OSC.h>

byte myMac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x4B, 0x9F };//This is a invented Mac address for ethernet sheild
byte myIp[]  = { 192, 168, 2, 9 };//assigned IP for the Arduino.

byte destIp[] =  { 192, 168, 2, 2 };//IP address of 'server', basically the IP address of the computer who receives the data into Max via a udpreceive
int  destPort = 10000; //the Max/MSP object udpreceive has to listen to this port.

int pin0 = 0; //define variable for reading of pin0
int pin1 = 1; //define variable for reading of pin1

char oscAdr0[] = "/analog/0_photo"; //define OSCaddress for reading of pin0
char oscAdr1[] = "/analog/1_photo"; //define OSCaddress for reading of pin1


char      bufPin0[5]; //prepare a string to carry the reading of pin0 over udp
char      bufPin1[5]; //prepare a string to carry the reading of pin1 over udp

Z_OSCClient client;

void setup(){
  
  Serial.begin(19200);

  Ethernet.begin(myMac ,myIp);  
  
}

void loop(){
  
    sendProcess0(); //execute sendProcess0, see code below
    sendProcess1(); //execute sendProcess1, see code below
  
  delay(5); //delay between each sensor reading.
}

void sendProcess0(){
  
    Z_OSCMessage mes0;  //define OSCmessage
  
 int valuePin0 = (analogRead(pin0)); //get reading of pin0
 
 itoa(valuePin0-0, bufPin0, 10);  //convert the int of the value read from pin0 into the string defined above
 
  mes0.setAddress(destIp,destPort);
  mes0.setZ_OSCMessage(oscAdr0 ,"s" ,bufPin0); //assamble OSCmessage
  
  client.send(&mes0);
  
  mes0.flush();  
  
}

void sendProcess1(){
  
    Z_OSCMessage mes1;  
  
 int valuePin1 = (analogRead(pin1));
 
 itoa(valuePin1-0, bufPin1, 10);
 
  mes1.setAddress(destIp,destPort);
  mes1.setZ_OSCMessage(oscAdr1 ,"s" ,bufPin1);
  
  client.send(&mes1);
  
  mes1.flush();  
  
}

I don't see why?

The digital pins are toggle switches, just like the light switches in your house. Turn them on, they stay on. Turn them off, they stay off. There is no need to turn a switch on when it is already on, or to turn it off when it is already off.

You are missing the point. Saying “no joy” or “I had no luck” sounds like whining. It is no help at all. If you had said “I did X and expected A and I got B”, then we would have something to work on.

It’s definitely not your place to tell someone that they sound like they are whining. If you don’t have the patience for people who are new to this or for answering questions that you feel are not articulated properly then why bother to answer at all?

OK fair enough, no harm done. I'll do my best to be as precise as possible from now on.

Thanks for the good advice.