To progress, Need advice, opinions, remarks on my 1st program

Hello,

I can be considered a beginner in the Arduino world, I “master” the development (Python, Php, Bash, etc …) but unfortunately not the C / C ++.

I started a small project to train myself in techniques related to Arduino, based on PIR, RF 433MHz etc …

So I made my first program (Yearr!) On Arduino starting with the links RF433, and the first tests are rather conclusive (despite a lot of problems related to the C language and format conversion).

I allow myself to submit my (test) code to “specialists” in order to have your advice, remarks, possible optimization, memories, speeds etc etc
Basically tell me if it is possible to “do better” so that I can progress in the right way.

Thank you very much in advance for your selflessness !!

#include <Streaming.h>
#include <VirtualWire.h>
#include <VirtualWire_Config.h>

char PIR_id[] = "PIR01:";
const byte TX_Pin  = 7;
const byte LED_Pin = 5;

int liveInterval = 60;

void setup() {
  Serial.begin(115200);
  pinMode(LED_Pin,OUTPUT);
  vw_setup(2000); 
  vw_set_tx_pin(TX_Pin);
  Serial << "START Transmeter" << endl;
}

void loop() {
  static int liveLast;
  
  if ( (millis()/1000) - liveLast >= liveInterval) { 
    liveLast += liveInterval;
    SendMsgCapteur(PIR_id, (char *)":LIVE:");
  }

  // Pour tester, sera remplacé par detection PIR
  digitalWrite(LED_Pin, true);
  SendMsgCapteur(PIR_id, (char *)":TEST:");
  digitalWrite(LED_Pin, false);
  
  delay(1000);
}

void SendMsgCapteur(char* id, char* etat) {
  static int i;
  
  char Msg2Send[40] = "";
  strcat(Msg2Send, id );
  
  // Index de Message
  char index[5];
  itoa(++i, index, 10);
  strcat(Msg2Send, index);

  // Type de message
  strcat(Msg2Send, etat);
  
  // Etat de la Batterie
  char batterie[10];
  itoa(millis(), batterie, 10);
  strcat(Msg2Send, batterie);
  
  vw_send((uint8_t *)Msg2Send, strlen(Msg2Send)+1);
  vw_wait_tx();
  
  Serial << Msg2Send <<endl; // affiche le tableau de char
  digitalWrite(LED_Pin, false);
}

FYI, I made my current tests on Nano, but intends to finalize the set (when finished) on an ATTyny85 or other (I specify it in case there are inconsistencies)

(Sorry for my bad english… :frowning: )

RF433-Transmeteur.ino (1.19 KB)

First off you are not posting your code properly.
Please read #7
http://forum.arduino.cc/index.php/topic,148850.0.html

if ( (millis()/1000) - liveLast >= liveInterval) {
There is an example BWD (BLINK WITHOUT DELAY) in the IDE that you should master.
liveLast should be ‘unsigned long’

Suggest you use HIGH/LOW rather than true/false.

larryd:
First off you are not posting your code properly.
Please read #7
http://forum.arduino.cc/index.php/topic,148850.0.html

Thank you for your reply,

Sorry for the code, Its Ok Now :wink: ...

larryd:
if ( (millis()/1000) - liveLast >= liveInterval) {
There is an example BWD (BLINK WITHOUT DELAY) in the IDE that you should master.

This portion of code is made to trigger an action every minute, I will look at what you advise me, Thanks

larryd:
liveLast should be ‘unsigned long’

You're right, for the unsigned, it's a copy error, it's ok in my current code

larryd:
Suggest you use HIGH/LOW rather than true/false.

it is a habit of "ordinary" languages :wink: , does it have a concrete influence? HIGH = 1 and LOW = 0 in all cases no?
But you're right, it may be more clear with respect to the electrical purpose

Thanks

true/false are boolean constants that are best used in if() while() also bool myFlag = true; if(myFlag) { //do stuff }
Note: a variable that is equal to 13 (-13, 4, 255 etc.) is true

HIGH/LOW are defined as 1/0, an output pin can be either 1 or 0 i.e. 5v/0v or 3.3v/0v

See:
https://www.arduino.cc/reference/en/language/variables/constants/constants/

if ( millis()- liveLast >= 60 * 1000ul) // has one minute gone by? (good habit to add ul as compiler defaults to integer math. ul tells compiler result to be unsigned long)

As always comments in your code is a good thing :wink:

Streaming.h is a bit heavy on memory so using hardware Serial might be better used for ATTyny85.

Not a programmer here but I try :slight_smile:

larryd:
Streaming.h is a bit heavy on memory so using hardware Serial might be better used for ATTyny85.

You are right.
I use "Streaming.h" in a debug approach, and will not use it in the final version
But it is faster and easier to use than having to play in the conversion format (int, string, etc ...) or to add a multitude of line "Serial.print".

Format conversion is the thing that poses me the most problem currently, it is related to the C / C ++, i have the habit of other language more ... permissive in this field;)

VirtualWire was a pain to work with in my also “still learning” experience. RCSwitch library made doing things with 433Mhz projects a breeze. Highly recommend giving that a look if you run into any issues with the sending or receiving or if you want to decode signals from existing devices.