Jump to content

Code dose not want to loop in a loop

Symbolls
Go to solution Solved by shadow_ray,

This is how your code looks like:

void loop(){
  //no code here
  if (IrReceiver.decode()) {
    //bunch of code here
  }
  //no code here
}

Let's see what bool IRrecv::decode() does:

The main decode function, attempts to decode the recently receive IR signal.

@return false if no IR receiver data available, true if data available. Results of decoding are stored in IrReceiver.decodedIRData.

 

You should brake it up into 2 parts:

data_type data;

void loop(){
  if (IrReceiver.decode()) {
    //process the recieved data here
    data = process_data(IrReceiver.decodedIRData);
  }  
  //do usual stuff here
  do_stuff();
}

 

So i made an line folower and the code is all fine and good utill i aded a IR recever so i can turn it on or off at will and fore some resone the code that acualy follows the line dose not want to loop or react after the state is ON so i am not a good programer i am an engenier so it might be something dumb so sorry if thats the case. Here is the code

#include <Arduino.h>

#if FLASHEND <= 0x1FFF  // For 8k flash or less, like ATtiny85. Exclude exotic protocols.
#define EXCLUDE_UNIVERSAL_PROTOCOLS // Saves up to 1000 bytes program space.
#define EXCLUDE_EXOTIC_PROTOCOLS
#endif
/*
 * Define macros for input and output pin etc.
 */
//#include "PinDefinitionsAndMore.h"

#include <IRremote.hpp>

#if defined(APPLICATION_PIN)
#define RELAY_PIN   APPLICATION_PIN
#else
#define RELAY_PIN   5
#endif
// Arduino Line Follower Robot Code
#include <Servo.h>

Servo myservo1;
Servo myservo2;

#define R_S 2//ir sensor Right
#define L_S 4 //ir sensor Left
void setup(){ 

  pinMode(LED_BUILTIN, OUTPUT);
    pinMode(RELAY_PIN, OUTPUT);

    Serial.begin(115200);
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) || defined(SERIALUSB_PID) || defined(ARDUINO_attiny3217)
    delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor!
#endif
    // Just to know which program is running on my Arduino
    Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));

    // Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
    IrReceiver.begin(10, ENABLE_LED_FEEDBACK);

    Serial.print(F("Ready to receive IR signals at pin "));
    Serial.println(10);

int on = 0;
unsigned long last = millis();

myservo1.attach(9);
myservo2.attach(8);
  
pinMode(R_S, INPUT); 
pinMode(L_S, INPUT); 
delay(10);

myservo2.write(90);
delay(1000);
}
int on = 0;
unsigned long last = millis();
void loop(){

if (IrReceiver.decode()) {
        // If it's been at least 1/4 second since the last
        // IR received, toggle the relay
        if (millis() - last > 250) {
            on = !on;
            Serial.print(F("Switch relay "));
            if (on) {
              if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 1)){
              Serial.print("Forword");
              myservo1.write(94);
              myservo2.write(100);
                }   //if Right Sensor and Left Sensor are at White color then it will call forword function
              if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){
                Serial.print("Right");
              myservo1.write(113);
              myservo2.write(95);
                } //if Right Sensor is Black and Left Sensor is White then it will call turn Right function  
              if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){
                Serial.print("Left");
              myservo1.write(75);
              myservo2.write(95);
                }  //if Right Sensor is White and Left Sensor is Black then it will call turn Left function
              if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){
                Serial.print("Stop");
              myservo1.write(94);
              myservo2.write(90);
                } //if Right Sensor and Left Sensor are at Black color then it will call Stop function
              }
                digitalWrite(RELAY_PIN, HIGH);
                Serial.println(F("on"));
            }
            else {
                myservo1.write(94);
                myservo2.write(90);
                digitalWrite(RELAY_PIN, LOW);
                Serial.println(F("off"));
            }

#if FLASHEND >= 0x3FFF      // For 16k flash or more, like ATtiny1604
            IrReceiver.printIRResultShort(&Serial);
            Serial.println();
            if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
                // We have an unknown protocol, print more info
                IrReceiver.printIRResultRawFormatted(&Serial, true);
            }
#else
            // Print a minimal summary of received data
            IrReceiver.printIRResultMinimal(&Serial);
            Serial.println();
#endif // FLASHEND
        last = millis();
        IrReceiver.resume(); // Enable receiving of the next value
        }
    }

 

Link to comment
Share on other sites

Link to post
Share on other sites

Where exactly is your loop. in your snippet you have

void loop() { }

 

Which when called will run once and return.

 

If your wanting a indefinite loop for continuous scanning you probably want to add something like

while (true) 
{
	//do something
}

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Nayr438 said:

Where exactly is your loop. in your snippet you have

void loop() { }

 

Which when called will run once and return.

 

If your wanting a indefinite loop for continuous scanning you probably want to add something like

 

That's not how arduino works, it hides a lot of platform code from you including the real main loop, and calls your loop() function repeatedly. So that's correct, and if you do your own while(true) you'll prevent all the background stuff from working.

 

 

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Kilrah said:

That's not how arduino works, it hides a lot of platform code from you including the real main loop, and calls your loop() function repeatedly. So that's correct, and if you do your own while(true) you'll prevent all the background stuff from working.

 

 

then what can i do so the sensor reading part works

Link to comment
Share on other sites

Link to post
Share on other sites

This is how your code looks like:

void loop(){
  //no code here
  if (IrReceiver.decode()) {
    //bunch of code here
  }
  //no code here
}

Let's see what bool IRrecv::decode() does:

The main decode function, attempts to decode the recently receive IR signal.

@return false if no IR receiver data available, true if data available. Results of decoding are stored in IrReceiver.decodedIRData.

 

You should brake it up into 2 parts:

data_type data;

void loop(){
  if (IrReceiver.decode()) {
    //process the recieved data here
    data = process_data(IrReceiver.decodedIRData);
  }  
  //do usual stuff here
  do_stuff();
}

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×