First program, help

Hello my name is Gerard, I'm new here.

Updated see the #12 reply.

I am making a small program that communicates with a microcontroller, working in hexadecimal and expects a 4-byte frame so as to: 0x23 0xC6 0x12 0x7A

The problem is not how to declare the hexadecimal code.

byte data[8];
STX=0x23;
llenar=0xC6;
CD=0x12;
fin=0x7A;

void setup() {                
 pinMode(13, OUTPUT); 
 //pinMode(8, OUTPUT);  
}

void loop() {
  void omplir(byte )
  { 
  unsigned int data;
  data = STX+llenar+CD+fin;  
  digitalWrite(data, HEX);
  delay(1000);
  }
}

Thanks and sorry if the solution is very easy but I'm new at this.

Sorry, it isn't clear what you want to do or what the problem is.

English is clearly not your first language, and your IP address indicates Spain - would it be easier to ask your question in the Spanish section of the forum?

Ok, well, I want to control with arduino another microcontroller this controls water pumps, I want to send commands with arduino. This microcontroller works with hexadecimal, for example the waft above load one of water pumps.

I want to send by port 13, hex code 0x23 0xC6 0x12 0x7A; for this I want to save this code in a variable, and after send using "digitalwrite(variable)" for output.

I hope I explained better.
Thanks

HEX numbers do not exist,

numbers are integers and you can represent them in base16 => hexadecimal

so your vars should be integers (byte = 8 bits integer) e.g.

byte data[8];
byte STX = 0x23;
byte llenar = 0xC6;
byte CD = 0x12;
byte fin = 0x7A;

Ok thank’s. I’m little by little learning. Monday will update the code to see if it works.

byte data[8];
byte STX = 0x23;
byte llenar = 0xC6;
byte CD = 0x12;
byte fin = 0x7A;

void setup() {                
pinMode(13, OUTPUT); 
}

void loop() {

byte funcllenar ( byte data){
data = STX+llenar+CD+fin;
digitalWrite(13,data);
}
}

so would be good?

data = STX+llenar+CD+fin;
digitalWrite(data);

A digitalWrite requires a pin number and a single bit value.

AWOL:

data = STX+llenar+CD+fin;

digitalWrite(data);



A digitalWrite requires a pin number and a single bit value.

Yes, sorry, I’ve seen and you have answered before I can correct it.
Otherwise, have to go as the plot, with 0x, or without?

byte data[8];
byte STX = 0x23;
byte llenar = 0xC6;
byte CD = 0x12;
byte fin = 0x7A;

void setup() {                
pinMode(13, OUTPUT); 
}

void loop() {

byte funcllenar ( byte data){
data = STX+llenar+CD+fin;
digitalWrite(13,data);
}
}
try to learn to keep the indentation of the right, and add spaces around operators to improve the structure of the code and therefore the readability

[code]void loop() {

byte funcllenar ( byte data){
data = STX+llenar+CD+fin;
digitalWrite(13,data);
}
}

versus

void loop() 
{
}

byte funcllenar ( byte data)
{
  data = STX + llenar + CD + fin;
  digitalWrite(13, data);
}

note that the parameter data is directly overwritten in funcllenar()

maybe you mean
byte funcllenar ()
{
byte data = STX + llenar + CD + fin;
digitalWrite(13, data);
}
[/code]

I still don’t see the point of writing the sum of three bytes like this - if the sum is zero, the result will be a LOW, otherwise it will be HIGH.

Makes sense to me,

if the bytes add up to zero the checksum is OK => led OFF
if the bytes add up to some non zero value => error => led ON

It seems that your expectation is that you can send your four byte frame with a single call to digitalWrite - you can't. If you are communicating with the pump controller using serial communications, including start & stop bits, you could collect your four bytes in a buffer and send it using a single call to serial.write.

However, if you're really intending to send four bytes of data only, you will need to send it bit by bit down the wire. Multiple calls to digitalWrite will serve you here - see "bit banging".

It would be easier to help if you have documentation that describes the protocol you're trying to use.

wildbill:
It seems that your expectation is that you can send your four byte frame with a single call to digitalWrite - you can’t. If you are communicating with the pump controller using serial communications, including start & stop bits, you could collect your four bytes in a buffer and send it using a single call to serial.write.

However, if you’re really intending to send four bytes of data only, you will need to send it bit by bit down the wire. Multiple calls to digitalWrite will serve you here - see “bit banging”.

It would be easier to help if you have documentation that describes the protocol you’re trying to use.

Very good appreciation, is absolutely true, the microcontroller currently pump is controlled by a PC serial port.
When I started with arduino, the idea was to use this serial TX / RX, but I could synch problems, and if used the digital port 13, this did not happen, so why did the program; but you are absolutely right. I’ll try.
Thank you very much.

That would be correct?

byte data[8];
byte STX = 0x23;
byte llenar = 0xC6;
byte CD = 0x12;
byte fin = 0x7A;
int v = 0;

void setup() {                
Serial.begin(9600);
}

void loop() {

if (v < 1){  //to send only 1 time the instruction
      byte funcllenar (){
        byte data = STX + llenar + CD + fin;
        serial.Write(13, data);
        v=1;
        }
}  
}

This code returns a error, on line: “byte funcllenar (){”

sketch_aug25a.ino: In function ‘void loop()’:
sketch_aug25a:15: error: a function-definition is not allowed here before ‘{’ token

This code returns a error, on line: "byte funcllenar (){"

sketch_aug25a.ino: In function 'void loop()':
sketch_aug25a:15: error: a function-definition is not allowed here before '{' token

Is there some part of "You can't define a function inside a function" that you don't understand?

PaulS:

This code returns a error, on line: “byte funcllenar (){”

sketch_aug25a.ino: In function ‘void loop()’:
sketch_aug25a:15: error: a function-definition is not allowed here before ‘{’ token

Is there some part of “You can’t define a function inside a function” that you don’t understand?

Ok, I had not understood well.
Finally:

byte data[8];
byte STX = 0x23;
byte llenar = 0xC6;
byte CD = 0x12;
byte fin = 0x7A;
int v = 0;

void setup() {                
Serial.begin(9600);
}

void loop() {

if (v < 1){  //to send only 1 time the instruction
     
        byte data = STX + llenar + CD + fin;
        Serial.write(data);
        v=1;
        
}  
}
byte STX = 0x23;
byte llenar = 0xC6;
byte CD = 0x12;
byte fin = 0x7A;
        byte data = STX + llenar + CD + fin;
        Serial.write(data);

0x23 + 0xC6 + 0x12 + 0x7A = 0x175 = 373 which will not fit in a byte.

Why are you ADDING the values? It seems to me like you want to store the 4 values in an array and send the array.

Why are you ADDING the values?

could be a first order checksum.
The DHT series temperature/humidity sensors use the same algorithm for checksum. Very fast but low robustness (1 in 255)

PaulS:

byte STX = 0x23;

byte llenar = 0xC6;
byte CD = 0x12;
byte fin = 0x7A;






byte data = STX + llenar + CD + fin;
        Serial.write(data);



0x23 + 0xC6 + 0x12 + 0x7A = 0x175 = 373 which will not fit in a byte.

Why are you ADDING the values? It seems to me like you want to store the 4 values in an array and send the array.

Okay, sorry for the delay; but is that the former design engineer for microcontroller water pumps do not let a good documentation and that complicates it even more.
Do not + frames, I have to send the frames individually, should be installed array.
Here is the version of the code, but it returns an error.

byte toSend[4] = {0xC6, 0x00, 0x12, 0x00}; //Array declaration
//byte toSend[0] = 0xC6; 
//byte toSend[1] = 0x00; 
//byte toSend[2] = 0x12; 
//byte toSend[3] = 0x00; 
int v = 0;
int i;

void setup() {                
    Serial.begin(9600);
    pinMode(8, OUTPUT);
   
}

void loop() {

  if (v < 1){  //to send only 1 time the instruction
    for(i=0; i<5; i=i+1){
      digitalWrite(8, HIGH);   // set the LED on      
      Serial.write(toSend, i);
    }
    v=1;    
    digitalWrite(8, LOW);    // set the LED off    
  }  
}

Still not working, but the code compiles fine, I will continue working.
Also I added a led to know when the execution program starts and end,
Thank’s!!

    for(i=0; i<5; i=i+1){
      digitalWrite(8, HIGH);   // set the LED on      
      Serial.write(toSend, i);

Send 0 items from the array of 4 elements.
Then, send 1 item from the array of 4 elements.
Then, send 2 items from the array of 4 elements.
Then, send 3 items from the array of 4 elements.
Then, send 4 items from the array of 4 elements.

Perhaps you could explain why you are doing this. Perhaps you have some impression that this is what the device expects.