UDP.Write.

Just migrated to arduino from PicBasic, and im struggleing along a bit. i need to read the status of pins connected to relays, and return the Statuses in a UDP packet in text fomat eg. “Status =00101110”
this is the part i am struggleing with. it only sends the first relays status, i cant figure out how to loop through the pins and store the status of each one so i have all 8 statuses represented by a 1 or a 0

//Read the status of the pins
for (int i = 2; i<9; ++i){
readInput*=digitalRead(i);*
//***********************************************************************************************************************************************************

  • // Send a reply, to the IP address that sent us the packet we received*

  • // On Port 6000 (The TouchControl Listener)*

  • Udp.beginPacket(Udp.remoteIP(),6000);*

  • Udp.write(readInput); // the message to send*

  • // Udp.write(ReplyBuffer);*

  • Udp.endPacket();*
    //***********************************************************************************************************************************************************
    }

If you want an ascii 0 and 1, then you need to add 48 to the digitalRead.

    readInput=digitalRead(i) + 48;

Insure you terminate that array with a zero, or send only the 8 bytes.

Do this as 2 arrays:

//Read the status of the pins 
    for (int i = 2; i<9; ++i){
      readInput=digitalRead(i);


one for pins
byte pinsArray[] = {2,3,4,5,6,7,8,9,};

one for the data
byte readInput[8];

then
//Read the status of the pins 
    for (int i = 0; i<8; ++i){
      readInput[i]=digitalRead(pinsArray[i]);
    }

Or did you want readInput to be 1 byte representing all 8 pins?
In that case, you can read the two 8-bit ports D and B, and pull 6 bits from PORTD (D7,6,5,4,3,2) and 2 bits from PORTB (D9,8) and combine them:

readInput = (PORTD>>2) | ((PORTB & 0x03)<<6); // read port D, move 7-2 to positions 5-0, read port B move bits 1-0 to position 7-6, and combine together.

I dont think i explained what it is i am trying to do very well. If i was sending the status to a serial port i could use
int i;
for (i = 2; i < 9; i = i + 1) {
Serial.println(myPins*);*
}
so this would send the characters (0&1s) one at a time to the serial port until all 8 were sent, this is ok for serial comms, but i need to put all eight status’s into an array and send them over UDP as one string

8art-5imson:
I dont think i explained what it is i am trying to do very well. If i was sending the status to a serial port i could use
int i;
for (i = 2; i < 9; i = i + 1) {
Serial.println(myPins*);*
}
so this would send the characters (0&1s) one at a time to the serial port until all 8 were sent, this is ok for serial comms, but i need to put all eight status’s into an array and send them over UDP as one string
[/quote]
print and println are different than write. The write performs no translation. Try this test.
```
*int i;

for (i = 2; i < 9; i = i + 1) {
Serial.write(i);
}

Serial.println();

for (i = 2; i < 9; i = i + 1) {
Serial.print(i);
}*

```
edit: My bad. Don’t use the array, just the i.

i didn't mean to use println i use print as i dont want the new line all i want is to send "01010110" or what ever the status's are all in one go. if i type: Udp.write("Hello World"); It sends this all in one go. so i need to read the pin status's 2-->9 and put them into an array, and send them as a 8 character text string. Thanks

Udp.write(“Hello World”) requires no translation. Those are already ascii values.

The value returned from digitalRead() is a 0 or a 1, not 48 or 49 (ascii 0 and 1). So add 48 to each value!

int i;

for(i=2,i<9;i++) {
  Serial.write(i + 48);
}

edit: Or to fit your code:

int i;

for(i=2,i<9;i++) {
  Serial.write(digitalRead(i) + 48);
}

I don’t want serial.write i dont want/need any serial commands. i can send serial with no problem, in the correct format. i need to send the data over my network via UDP.

So you need to change this to send the array I showed you how to create above:

Udp.beginPacket(Udp.remoteIP(),6000);
Udp.write(readInput); // the message to send
// Udp.write(ReplyBuffer);
Udp.endPacket();

Change to this to loop thru the array and send it out:

    Udp.beginPacket(Udp.remoteIP(),6000);
for (i=0; i<8; i=i+1){
    Udp.write(readInput[i]);   // the message to send
}
   // Udp.write(ReplyBuffer);
    Udp.endPacket();

You are very dense. Since you can’t figure out how to do it on your own from my examples, then:

char tBuffer[9];

for(i=0;i<8;i++) {
  // save the values as ascii
  tBuffer[i] = digitalRead(i+2) + 48;
}

// terminate the array with a zero
tBuffer[8] = 0;

Udp.beginPacket(Udp.remoteIP(),6000);
Udp.write(tBuffer);   // the message to send
Udp.endPacket();

Thank you so much that worked perfect, i think i had messed around so much, my brain is fried.