How to prevent one code block disrupting another?

Hi,
I am facing one problem and don’t even know how to solve it. The code included is arduino bluetooth transmitter used in my esk8 build. It receives some values from the main bldc controller and transmits them straight to the receiver, where the values are displayed. Data transfer via bluetooth works properly. However, recently I decided to implement another feature for my eskate - wirelessly powered lights. On one side is 2.4Ghz transmitter and one of its channels is switch. On the other side is receiver. I hooked up pwm input to arduino and checking weather push button is pressed. So the problem is: my flashlight occasionally blinks (I guess when more data over bluetooth is flowing). Maybe there is a way to get rid of this problem? Thanks

//BT
#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);

//UART
#include "VescUart.h"
#include "datatypes.h"
#ifdef ARDUINO_AVR_NANO
#define SERIALIO Serial
#define DEBUGSERIAL Serial
#endif

//values
float current = 0.0;
float voltage = 0.0;
float sspeed = 0.0;
float distance = 0.0;

unsigned long sec;
void setup() 
{
  
  SERIALIO.begin(115200);
  BTserial.begin(38400);
  pinMode(5, INPUT);
  pinMode(9, OUTPUT);
  analogWrite(9, 127);

}

struct bldcMeasure values;
void loop() {
  //-----------------------lights----------
  sec = pulseIn(5, HIGH);
if (sec > 1500){
   digitalWrite(9, HIGH);
  }
 else {
  digitalWrite(9, LOW);
 }
 //---------------------------------------
 if(VescUartGetValue(values))
  {
     SerialPrint(values);
     voltage=(values.inpVoltage);
     current=(values.avgInputCurrent);
     sspeed=((values.rpm/7)/2.4*0.00025136*60);
     distance=((values.tachometer/(14/3))/2.4*0.00025136);
  
  }
 else
 {}
 
    BTserial.print('<');
    BTserial.print(voltage);
    BTserial.print(',');
    BTserial.print(current);
    BTserial.print(',');
    BTserial.print(sspeed);
    BTserial.print(',');
    BTserial.print(distance);
    BTserial.print('>');
    delay(100);

 
}

Exactely what is connected to pin 5? In "setup()" you have an analogWrite on pin 9. In "loop()" you have digitalWrite's on the same pin. If your arduino (for some reason) resets, pin 9 will be turned half on in setup(), and then be shut off again in loop(). This may be the cause of the blinks.

Sorry, i missed that line. It was used for testing purposes only and it helped me to realize that flashlight is not blinking when turned on in the setup.

2.4Ghz receiver pwm pin is hooked up to pin 5

laikiux:
2.4Ghz receiver pwm pin is hooked up to pin 5

Why would a 2.4GHz receiver have a PWM pin?

Post a link to the datasheet for the receiver and post the program in which it is being used.

...R

I am talking about this one:

laikiux:
I am talking about this one:

That's a picture not a datasheet. It doesn't even identify the model number or manufacturer, so nobody can even search for a datasheet.

https://hobbyking.com/media/file/737141785X1139128X5.pdf to be exact. Actually, all of the rc controllers work in the same way...

I didn’t see any reference to PWM in the brochure, but perhaps you’re thinking about PPM used to control RC servos?

You’ll have to identify more about the transmitter if you want to control it with an Arduino - or get rid of it altgether and drive the servos directly from Arduino ‘servo’ pins....

Let us know what you’re trying to achieve.

laikiux:
Actually, all of the rc controllers work in the same way...

OK. I did not know you were using a receiver that is intended to control a servo. Unfortunately the control signal for a servo is often called PWM even though it is completely different from the PWM that and Arduino produces.

Please explain again the overall purpose of the program and the problem you are having. I can't make enough sense of your Original Post.

What is an esk8?

What does this mean "It receives some values from the main bldc controller and transmits them straight to the receiver"? What is "it"? What is "the main bldc controller"? What is the "receiver"?

A simple diagram showing all the parts and how they relate to each other would be a good idea. See this Simple Image Guide

...R

I will try to make things clear
Some additional pictures:


my goal is to get rig of a occasional blinking of my flashlights. Because of the intensive data sending over bluetooth, sometimes this part of code delays.

//-----------------------lights----------
  sec = pulseIn(5, HIGH);
if (sec > 1500){
   digitalWrite(9, HIGH);
  }
 else {
  digitalWrite(9, LOW);
 }
 //---------------------------------------

Maybe I should do it in this way?

//-----------------------lights----------
  sec = pulseIn(5, HIGH);
if (sec < 1500){
   digitalWrite(9, LOW);
  }
 else {
  digitalWrite(9, HIGH);
 }
 //---------------------------------------

I do not know anything about the transmitter / receiver you are using, so it is hard to give good advice on that. But your sketch is constantly transmitting data over bluetooth, and if you think that is an issue, then try to limit the BT activity by modifying your sketch to something like:

//BT
#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);

//UART
#include "VescUart.h"
#include "datatypes.h"
#ifdef ARDUINO_AVR_NANO
#define SERIALIO Serial
#define DEBUGSERIAL Serial
#endif

//values
float current = 0.0;
float voltage = 0.0;
float sspeed = 0.0;
float distance = 0.0;

unsigned long sec;
void setup()
{
 
  SERIALIO.begin(115200);
  BTserial.begin(38400);
  pinMode(5, INPUT);
  pinMode(9, OUTPUT);
  analogWrite(9, 127);

}

struct bldcMeasure values;
void loop() {
  //-----------------------lights----------
  sec = pulseIn(5, HIGH);
if (sec > 1500){
   digitalWrite(9, HIGH);
  }
 else {
  digitalWrite(9, LOW);
 }
 //---------------------------------------
 if(VescUartGetValue(values))
  {
     SerialPrint(values);
     voltage=(values.inpVoltage);
     current=(values.avgInputCurrent);
     sspeed=((values.rpm/7)/2.4*0.00025136*60);
     distance=((values.tachometer/(14/3))/2.4*0.00025136);

    BTserial.print('<');
    BTserial.print(voltage);
    BTserial.print(',');
    BTserial.print(current);
    BTserial.print(',');
    BTserial.print(sspeed);
    BTserial.print(',');
    BTserial.print(distance);
    BTserial.print('>');
    delay(100);
     
 
  }
 /*else // else block is unused, remove it
 {}
 
    BTserial.print('<');
    BTserial.print(voltage);
    BTserial.print(',');
    BTserial.print(current);
    BTserial.print(',');
    BTserial.print(sspeed);
    BTserial.print(',');
    BTserial.print(distance);
    BTserial.print('>');
    delay(100);
*/
 
}

You could also modify it even further and only read “VescUartGetValue” once every second - this would also remove the “need” for a delay.

Thank you for reply! Yes, the problem is that the data is constantly flowing over bluetooth and variable sec is not constantly checked. When it is interrupted this code line kiks in and turns flashlights off:

else {
//  digitalWrite(9, LOW);}

Thanks for the diagram - it makes things much clearer.

My guess is that this code is not robust

sec = pulseIn(5, HIGH);
 
 if (sec > 1500){
 digitalWrite(9, HIGH);
 }
 else {
 digitalWrite(9, LOW);
 }

because it makes no allowance for the possibility that it starts measuring the time in the middle of a pulse. If it does that it would report a short pulse and (presumably) turn the light off briefly.

It may be made more reliable by checking the length of the short pulse to make sure it really is a short pulse - for example

sec = pulseIn(5, HIGH);
 
 if (sec > 1500){
 digitalWrite(9, HIGH);
 }
 else if (sec < 1200) {  // this is probably the wrong number
 digitalWrite(9, LOW);
 }

If that is not sufficient then you may need to check for a few consecutive short pulses before turning off the light

…R

PS … if this was my project I would use a pair of nRF24L01+ transceivers for the wireless link. Simple nRF24L01+ Tutorial - much simpler than trying to measure a pulse width.

Thank you Robin2! It is definitely worth to give it a go!

Nope, that doesn’t work. What I noticed is that Flashlights blinks less frequently.

I would try to create a minimal sketch which only looks for pulses which turns on / off the flashlights. This makes it easier to figure out exactely which interval / pulse length to to use.

Here you go:


laikiux:
Nope, that doesn't work. What I noticed is that Flashlights blinks less frequently.

You have not posted the latest version of your program so I have no idea what you tried.

I don't even know which of my suggestions you tried.

...R

Sorry.

//BT
#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);

//UART
#include "VescUart.h"
#include "datatypes.h"
#ifdef ARDUINO_AVR_NANO
#define SERIALIO Serial
#define DEBUGSERIAL Serial
#endif

//values
float current = 0.0;
float voltage = 0.0;
float sspeed = 0.0;
float distance = 0.0;

unsigned long sec;
void setup() 
{
  
  SERIALIO.begin(115200);
  BTserial.begin(38400);
  pinMode(5, INPUT);
  pinMode(9, OUTPUT);

}

struct bldcMeasure values;
void loop() {
sec = pulseIn(5, HIGH);
 
 if (sec > 1500){
 analogWrite(9, 130);
 }
 else if (sec < 1200){  
 digitalWrite(9, LOW);
 }
 if(VescUartGetValue(values))
  {
     SerialPrint(values);
     voltage=(values.inpVoltage);
     current=(values.avgInputCurrent);
     sspeed=((values.rpm/7)/2.4*0.00025136*60);
     distance=((values.tachometer/(14/3))/2.4*0.00025136);

    BTserial.print('<');
    BTserial.print(voltage);
    BTserial.print(',');
    BTserial.print(current);
    BTserial.print(',');
    BTserial.print(sspeed);
    BTserial.print(',');
    BTserial.print(distance);
    BTserial.print('>');
    delay(100);
 
  }

 
  
}