Communication

I basically started with a simple communication code, if I send the arduino a 1, it writes pin 13 high, and if I send it a 0, it writes it low.

Bust I have now incorporated, Virtual Wire, as well as a 433mhz tx and rx modules, I have written a code to replicate it, but am having trouble, I have finished a drafted, which is inconstant and has errors in practise. I also fear it is long winded and alot more conversions are occurring than needed?

#include <VirtualWire.h>
char* controller;
void setup() {
  pinMode(13,OUTPUT);
vw_set_ptt_inverted(true); //
vw_set_tx_pin(12);
vw_setup(4000);// speed of data transfer Kbps
Serial.begin(9600);
}

void loop(){

while(Serial.available() == 0) - 0;

char val = Serial.read() - '0';

if (val == 1) {
  
 controller = "1";
}
if(val == 0){
  controller="0";
}

else{
  controller = "3";
}

if (controller = "1") {
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,HIGH);
}
if (controller = "0") {
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,LOW);
}

else
{vw_wait_tx();
}

Serial.flush();
}

main issue is when, I send a 1, it may turn on, but when I send it another 1 it turns off, and is completely random and inconsistant.

while(Serial.available() == 0) - 0;

Why are you subtracting 0 from a while loop? How does that make sense?

if (val == 1) {
  
 controller = "1";
}
if(val == 0){
  controller="0";
}

So, controller should have the value read from the serial port? Why are you subtracting and adding '0', then?

controller is a pointer. What memory space does it point to?

if (controller = "1") {

= != ==.

Serial.flush();

Block until all pending serial data (none, that is) has been sent. How useless it that?

PaulS:

while(Serial.available() == 0) - 0;

Why are you subtracting 0 from a while loop? How does that make sense?

if (val == 1) {

controller = “1”;
}
if(val == 0){
  controller=“0”;
}



So, controller should have the value read from the serial port? Why are you subtracting and adding '0', then?

controller is a pointer. What memory space does it point to?



if (controller = “1”) {



= != ==.



Serial.flush();



Block until all pending serial data (none, that is) has been sent. How useless it that?

Well I took the -0, out and no difference, I dont understand what you mean by;

if (controller = "1") {

= != ==.

Well I took the -0, out and no difference, I dont understand what you mean by;
Code:
if (controller = “1”) {
= != ==.

Look in the reference section of this site!

Mark

Look in the reference section of this site!

Specifically, look at the assignment operator (=) and the equality operator (==). They ARE different symbols for a very good reason. Do the research. Drill this point into your head so you don't make the same mistake again.

done