what am i doing wrong here some help please

I'm trying to make my Arduino elegoo uno r3 robot car v2.0 use the IR remote to line track basically I want to push for instance the 1 button and it start the line track function for some reason which I have no idea what I'm doin lmao. can get this simple task to perform right ive tried switch loops and I cant get it any help would be appreciated

#include <IRremote.h>

/*defines button*/
#define button_1 16738455

/*defines line tracking sensors*/
#define LT1 digitalRead(10)
#define LT2 digitalRead(4)
#define LT3 digitalRead(2)

/*defines logic control pins*/ 
#define ENA 5
#define ENB 11
#define in1 6
#define in2 7
#define in3 8
#define in4 9

/*absoulte value*/
#define ABS 120

unsigned long A;

/*IR Receiver pin*/
IRrecv MYIR(12);
decode_results results;

/*move forward*/
void forward(){
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go forward");
}
/*move back*/
void back(){
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go back");
}
/*move left*/
void left(){
analogWrite(ENA,HIGH);
analogWrite(ENB,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go left");
}
/*move right*/
void right(){
analogWrite(ENA,HIGH);
analogWrite(ENB,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go right");
}
/*STOP!!!!*/
void stop(){
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,LOW);
Serial.println("STOP!!!");
}

void setup(){
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
Serial.begin(9600);
stop();
MYIR.enableIRIn();
}

void loop(){
 if (MYIR.decode(&results)){
   A=results.value;
   Serial.println(A);
   MYIR.resume();
   delay(125);

if (A==button_1){
  (LT2),forward();
}
else if(LT1){
       left();
       while(LT1);
}
else if(LT3){
       right();
       while(LT3);
}
}    
 }
 }

Posting the same question in different parts of the forum will earn you no friends here, so I deleted the duplicate of this topic to save your embarrassment.

Please remember to use code tags when posting code

What do you expect while(forward); to do (besides being stuck there forever/till the watch dog bites you) ?

well that's the thing ive wrote it like 5 different ways that's just the last one I wrote when I kinda gave up after like 6hrs of writing and trying to make it work all I want is when I press a button don't really care what button I just want it to line track after the button is pressed. like I said I'm a total noob so hey I except all the criticism

I except all the criticism

Great! To better communicate, use simple sentences that begin with an uppercase letter and end with a period.

Separate different thoughts or topics with blank lines.

If you don't, some people might think you are a rambling, uneducated idiot.

Do you even understand the code you posted ?

Don't have an IDE on my laptop so can't test it to make sure... but does that code even compile?! I can't even figure out what it is supposed to be doing just by looking at it.

wvmarle:
Don't have an IDE on my laptop so can't test it to make sure... but does that code even compile?! I can't even figure out what it is supposed to be doing just by looking at it.

Besides the weird use of the macros

#define LT1 digitalRead(10)
#define LT2 digitalRead(4)
#define LT3 digitalRead(2)

instead of doing the digitalRead() clearly in the code (to make it easier to understand), that’s OK.

What is surprising is that the OP does not notice (even without understanding code) the syntactical singularity of one line versus the others constructs

if (A==button_1){
 if([color=green][b]LT2[/b][/color]){ 
    forward();
    while([color=red][b]forward[/b][/color]);
}
else if([color=green][b]LT1[/b][/color]){
       left();
       while([color=green][b]LT1[/b][/color]);
}
else if([color=green][b]LT3[/b][/color]){
       right();
       while([color=green][b]LT3[/b][/color]);
}

Something feels weird isn’t it?

Then of course there is the fact that this is with in the IR receiver test, so if there is no received code pending...

It was so weird I got so confused I had no idea any more what it is supposed to be doing :slight_smile: This forward is here used as variable while it's not declared as such, though there's a function called forward(). Calling a function without the () or an undeclared variable normally results in a compiler error.

wvmarle:
Calling a function without the () or an undeclared variable normally results in a compiler error.

You can't call a function without the (), but you can test for the existence (ie. function pointer non-null) of a function by missing out the parentheses.

Right. So for the workings of the code that becomes even worse - as it's an infinite loop.
That code feels more and more like it should have been submitted to the obfuscated C contest, rather than this forum :slight_smile:

wvmarle:
Right. So for the workings of the code that becomes even worse - as it's an infinite loop.

hence my very explicit comment in #2 about that...

Ok so now that we understand I'm a dumbass.
can someone actually help me. All I want it to do is run the loop after I press the button all the car will do is drive forward Go Figure

The fact that you are starting does not make you a dumbass - we all started there

The question is more do you want to understand what you do and Learn something or do you expect someone to write the code for you?

If you want to learn - then I would suggest you write code to perform an active wait for the IR signal in the setup() (this code is in the loop at the moment)

Once you have received te signal - proceed to the loop() and let it spin

Well, that's easy.

const byte buttonPin = 1;
const byte moveForwardPin = 2;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(moveForwardPin, OUTPUT);
  digitalWrite(moveForwardPin, LOW);
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    digitalWrite (moveForwardPin, HIGH);
  }
}

Assumptions made:
Button is wired between GND and pin 1.
Pin 2 set HIGH makes the robot move forward.
With those assumptions, what this code does: the moment you press the button the robot starts moving forward. That's it.

probably not that easy apparently...

how does this answer the question I asked ? you have code in the loop(), you don't have an active wait in the setup()...

So if I add the wait to the setup() it will cause the entire loop() to run not just the forward(); func .

Not sure I understand what you are trying to achieve then - thought it was the whole loop

Please describe in detail the behavior you want to see - really in detail (what needs to work, what IR does . Where is the button etc)

Ok so it is a robotic car that has IR,line tracking,ultrasonic, and bluetooth. I can make all of them run individually. But now I wanted to start combining them together.

So the code I want to write is to have the IR start the line tracking the LT1,LT2,&LT3 are the line sensors.
The loop is the line sensors task to perform I just cant get the remote to activate the loop it will either spaz out or just drive forward.

Sorry for the long post

So you really want the full loop() to be running when you press a button right?

your code is messed up because of the comment I made in #2 and re-stated in #7. fix that first...