[SOLVED] Struggling with Majenko's ICSC Library, Pointers, and Arrays

I'm trying to get the hang for majenko's ICSC library. I'm not a very experienced programmer and I am still learning how to handle pointers and arrays. If anyone could explain to me what I'm doing wrong here that would be much appreciated.

Right now I have a setup with two Arduinos and am trying to do this: 

- take a 10bit analog read value on Arduino1(master), 
- split it up into two pieces so it can fit into the char array for ICSC.send
- send the data to Arduino2(slave)
- Recombine the data on Arduino2 to get the original 10bit ADC value
- use that value to set the blink frequency of an LED on Arduino 2

I seem to consistently only get the first 7 bits of the ADC value on Arduino2, so there seems to be either a problem with how I'm trying to read the data in the array on Arduino2, or how I am storing data the data in the array on Arduino1. 

Master Code:
#include <ICSC.h>
uint16_t AnalogValue;
char datastream[2];
 
 
void setup()
{
  ICSC.begin(1, 115200); // assign device ID 1, baudrate
}
 
void loop()
{
 AnalogValue = analogRead(A0);
 datastream[0] = (char) AnalogValue & 0xff;
 datastream[1] = (char) AnalogValue >> 8;
 ICSC.send(2, 'F', 2, (char *)&datastream);
}

Slave Code:

#include <ICSC.h>
uint16_t blinktime = 300; // default blink frequency: 500ms
boolean led; // LED status on/off
 
 
void setup()
{
  ICSC.begin(2, 115200); // assign device ID 2, baudrate
  ICSC.registerCommand('F', &frequency); // command F deferrs to the frequency function
  pinMode(13, OUTPUT); // Pin 13 is our
  digitalWrite(13, LOW);
}
 
void loop()
{
  ICSC.process(); // listen for commands
  digitalWrite(13, led); // enforce led status
  led = !led; // change led status for next cycle
  delay(blinktime); // wait for <blinktime> amount of milliseconds
}
 
void frequency(unsigned char src, char command, unsigned char len, char *data)
{
 uint16_t value1 = (uint16_t) *(data);
 uint16_t value2 = (uint16_t) *(data+1);
 uint16_t value3 = value2 << 8;
 value1 = value1 + value3;
 blinktime = value1;// assign blinktime combined value
}

Added most recent code to parent.

Did someone say my name?

There should be no need to do any of that silly manipulation - all you need to do is cast the integer pointer as a char pointer:

 ICSC.send(2, 'F', 2, (char *)&AnalogValue);

and...

uint16_t *value = (uint16_t *)data;
blinktime = *value;

Cool, thank you so much for responding!

I’ll try to use what you’ve laid out as soon as I get home. I’m still a little weak on my pointers and all that, so lets see if I understand this correctly:

With ICSC.send(2, ‘F’, 2, (char *)&AnalogValue); I’m casting the address of AnalogValue, a 16bit integer, into a char pointer.

And with uint16_t *value = (uint16_t *)data; I’m casting the the address stored in the data char pointer into a int pointer called value , presumably so that incrementing the pointer will result in the right address (2 byte increment) for the int type data that was originally passed on through the ICSC.send function.

Does that sound about right?

That sounds about right.

The only bit you missed is the dereferencing:

blinktime = *value;

which takes the integer value from the location pointed to by the integer pointer and assigns it to blinktime.

Naturally!

Awesome. I'm excited to get this all up and running. Thank you so much for putting this library together. I'm working on what I hope will soon be a RS-485 based multipoint network for a home brew industrial control computer system :) This was going to be a lot more complicated w/o your help!

Cheers

EDIT: Just got home and tested it. Works like a charm! Thanks again 8)

hi guyz.. i have a little problem with this..

in the old library code we could use .begin() to init this library with parameters but in the latest library we init this library using constructor.. so my question is how to init this library in setup() function to set the node id (station) using eeprom data..

i have used a pointer

ICSC *icsc;

to set the node id (station) in the setup function as

    icsc = new ICSC(&rs485, myID , 10);

as im trying to use this lib with software serial at the same time.. normally without the pointer my code works but using this way this just seems to send empty data or doesnt send anything..

can someone point me to some direction about how to make this happen.. ??

thank you

I know this is an old topic, didn't see the need to start a new one, with the ICSC register command is it limited to a single char? Thanks

mindless: hi guyz.. i have a little problem with this..

in the old library code we could use .begin() to init this library with parameters but in the latest library we init this library using constructor.. so my question is how to init this library in setup() function to set the node id (station) using eeprom data..

i have used a pointer

ICSC *icsc;

to set the node id (station) in the setup function as

    icsc = new ICSC(&rs485, myID , 10);

as im trying to use this lib with software serial at the same time.. normally without the pointer my code works but using this way this just seems to send empty data or doesnt send anything..

can someone point me to some direction about how to make this happen.. ??

thank you

I can't help with the software serial directly, but my solution for the EEPROM station ID was to just use an older version of the library before the author made that change. That way, I could read EEPROM before all the ICSC stuff in the setup routine.

Maybe if you downgrade the library to the previous version (they are on github) you can software serial with an ID from EEPROM also. (if that's what you are in fact trying to do)