1 (edited by siva.kanth.sharma 2023-04-12 17:03:00)

Topic: "if" function not working

before i start- i am a complete newb when it comes to programming, microcontrollers, electronics etc.

I am trying to build a remote controlled car using an arduino nano. HC-05, sg90 servo, l298n motor driver and a simple BO motor. I will attach the code at the end. Here's my issue:

for the life of me, i cannot get any of the "if" statements to work. i tried to check if the pins are outputting anything using LEDs, and they're not.

i tried to test it by simplifying the code(again, with LEDs) . i first only kept the digital writes, it works. then i tried only the analogwrite, that works too. but when i put it all together, it doesnt work. i initially had the digitalwrite and analogwrite within the "if" conditions, then i moved it out as part of my trial to fix it. the "lead1" and "lead2" chars were also part of that modification. initally it was a straight high/low in the digital write.

the servo works flawlessly through all this.

PS: I'm unable to work the text sprintf too, but its secondary. I'd appreciate help with that too, but i can do without it. i did simila debug attempts with this too.

#define REMOTEXY_MODE__HARDSERIAL

#include <RemoteXY.h>

// RemoteXY connection settings 
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 9600


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =   // 61 bytes
  { 255,2,0,51,0,54,0,16,31,0,4,176,42,29,49,5,2,26,4,48,
  19,10,5,45,2,26,129,0,3,30,14,5,17,83,112,101,101,100,0,129,
  0,59,37,15,6,17,83,116,101,101,114,0,67,4,31,17,61,5,2,26,
  51 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  int8_t steer; // =-100..100 slider position 
  int8_t speed; // =-100..100 slider position 

    // output variables
  char monitor[51];  // string UTF8 end zero 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////
#include <Servo.h>

const int pedal = 6; //motor PWM
const int motora = 7; // motor direction
const int motorb = 8; // motor direction
const int servopin = 5;
Servo servo1;
int move;
char lead1;
char lead2;

void setup() 
{
  RemoteXY_Init (); 
  pinMode(motora, OUTPUT);
  pinMode(motorb, OUTPUT);
  pinMode(pedal, OUTPUT);
  servo1.attach(servopin);
  // TODO you setup code
  
}

void loop() 
{ 
  RemoteXY_Handler ();

  int steer = RemoteXY.steer;
  int speed = RemoteXY.speed;
  int servoPosition = map(steer, -100, 100, 0, 180);
  servo1.write(servoPosition);

  if (speed = 0){
   lead1 = LOW;
   lead2 = LOW;
  sprintf (RemoteXY.monitor, "Brake..");
  }

  else if (speed > 0){
   lead1 = HIGH;
   lead2 = LOW;
    move = map(speed, 0, 100, 0, 255);
  }

  else if (speed < 0){
   lead2 = HIGH;
   lead1 = LOW;
    move = map(speed, 0, 100, 0, 255);
  }
  digitalWrite(motora, lead1);
  digitalWrite(motorb, lead2);
  analogWrite(pedal, move);
  sprintf (RemoteXY.monitor, move);
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay() 


}

2

Re: "if" function not working

Change

 if (speed = 0){

into

 if (speed == 0){

.

= is an assignment. == a compairison.