meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
arduino:code_collection [2025/02/21 19:34] – [Convert 2 registers to int32] vamsanarduino:code_collection [2025/03/22 22:54] (current) – [Float to char[]] vamsan
Line 1: Line 1:
 ====== LamaPLC: Arduino code collection ====== ====== LamaPLC: Arduino code collection ======
 +===== Frequently used code collection =====
 +==== TON ====
 +<code c>
 +  // var def block
 +  unsigned long startMillis, currentMillis;     // current and start time
 +  const unsigned long period = 2000;            // the value is a number of milliseconds
  
 +  // code block
 +  currentMillis = millis();                     // get the current "time" (actually the number of milliseconds since the program started)
 +  if (currentMillis - startMillis >= period)    // test whether the period has elapsed
 +  {
 +    startMillis = currentMillis;                // time update
 +    // after time has elapsed the call
 +    // ...
 +  }  
 +</code>
 +
 +==== check serial monitor ====
 +<code c>
 +
 +  // code in setup()
 +  Serial.begin(9600);
 +  
 +  while (!Serial) {
 +    ; // wait for serial port to connect. Needed for native USB port only
 +  }
 +  Serial.println("Serial monitor OK");
 +</code>
 +===== Converting code collection =====
 +==== Float to char[] ====
 +<code c>
 +// lib init (optional)
 +#include "avr/dtostrf.h"
 +// converting 
 +float a;
 +char sendValue[10];
 +dtostrf (a, 10, 8, sendValue); // float_value, min_width, num_digits_after_decimal, where_to_store_string
 +
 +</code>
 +
 +==== Int to char[] ====
 +<code c>
 +int a;
 +char sendValue[10];
 +itoa (i,sendValue,10); // int value, char * str, int base
 +// base:  Numerical base used to represent the value as a string, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary
 +
 +</code>
 +==== String to char[] ====
 +<code c>
 +
 +char place[20];
 +strcpy(place,"Home");
 +
 +</code>
 ===== Modbus code collection ===== ===== Modbus code collection =====
 ==== Convert 2 registers to float ==== ==== Convert 2 registers to float ====
Line 69: Line 123:
 } }
 </code> </code>
 +
 +call: [float] = //make_float_32bit_word//([first reg]) \\
  
 <code c> <code c>
Line 163: Line 219:
   Serial.println(tmpstr2);   Serial.println(tmpstr2);
   return false;   return false;
 +}
 +</code>
 +
 +===== General code collection =====
 +==== MAC random generator ====
 +Generate random MAC using pseudo random generator, bytes 0, 1 and 2 are static (MAC_START), bytes 3, 4 and 5 are generated randomly
 +<code c>
 +void generateMac() {
 +  // Marsaglia algorithm from https://github.com/RobTillaart/randomHelpers
 +  seed1 = 36969L * (seed1 & 65535L) + (seed1 >> 16);
 +  seed2 = 18000L * (seed2 & 65535L) + (seed2 >> 16);
 +  uint32_t randomBuffer = (seed1 << 16) + seed2; /* 32-bit random */
 +  memcpy(data.mac, MAC_START, 3);                // set first 3 bytes
 +  for (byte i = 0; i < 3; i++) {
 +    data.mac[i + 3] = randomBuffer & 0xFF;  // random last 3 bytes
 +    randomBuffer >>= 8;
 +  }
 } }
 </code> </code>