If something do something, if STILL same thing do another thing

So basically I want to make a code like the subject says;
if (result_A) {
do something
}
if STILL (result_A) {
do another thing
}

My question is how should I go about this. I've written the code below and I want it to work like if code 1 isn't read, then turn machine left, if code 1 still isn't read turn the machine right. Once the code is found, move the motor straight.

void motor_find() {
if(results.value != Tx_1_code_1) {
  digitalWrite (dir_1,LOW); // left wheel will spin counterclockwise to create a pivot point for turning
  digitalWrite (dir_2,HIGH); // right wheel spins in clockwise direction
  analogWrite (pwm_1, 100); // speed of left motor is 100 bits/second
  analogWrite (pwm_2, 100); // speed of right motor is 100 bits/second

  if (results.value != Tx_1_code_1) {  // if STILL not code 1, do next thing
     digitalWrite (dir_1,LOW); // left wheel will spin counterclockwise to create a pivot point for turning
     digitalWrite (dir_2,HIGH); // right wheel spins in clockwise direction
     analogWrite (pwm_1, 100); // speed of left motor is 100 bits/second
     analogWrite (pwm_2, 100); // speed of right motor is 100 bits/second
   }
}
}

void realign() {
  if (results.value != Tx_1_code_1) {
    motor_find();
  }
}
/* This code is apart of a different loop but it's where I want the motor to realign and once the */
/* signal is found, then go straight */
else {
   Serial.println("AGV NOT STRAIGHT");
    Serial.print("RECEIVER ONE signal received: ");
    Serial.println(results.value, HEX);
    Serial.println("REALIGN THE MOTOR");
    Serial.println("");
    irrecv_1.blink13(true);
    realign(); // should realign the machine to find Tx_1_code_1
    delay(500);
    if (results.value == Tx_1_code_1) { // once code 1 found, go straight
      motor_straight(); 
    }
  }

I'm not that experienced in Arduino, so if you could explain it fully I'd really appreciate it. Thank!

This pseudocode

So basically I want to make a code like the subject says; 
if (result_A) {
   do something
}
if STILL (result_A) {
   do another thing
}

is really just

So basically I want to make a code like the subject says; 
if (result_A) {
   do something
}
if (result_A) {
   do another thing
}

So you probably are facing some other issue...

want it to work like if code 1 isn't read, then turn machine left, if code 1 still isn't read turn the machine right. Once the code is found, move the motor straight.

turn left
if not code1
  {
  turn right
  if not code1
    {
    go ahead
    }
  }
}

Does do_something() have any effect on result_A? Because, the only other thing that can change result_A is time. And you basically gave it none of that.

-jim lee

Yeah, STILL is only several microseconds later in the OP example. Is a line following robot's sensor likely to change in that time? Not likely.

jimLee:
Does do_something() have any effect on result_A? Because, the only other thing that can change result_A is time. And you basically gave it none of that.

-jim lee

So the way it works is a transmitter is sending a signal (result_A) which is being read by the receiver. This signal will make the machine go straight.
If the machine strays off the path and doesn't read result_A, then I want it turn back so it can get the signal again and essentially keep the machine moving in a straight path.
The way it actually works is that it will read result_A and go straight, and continue to go straight even if code_1 isn't being read anymore, which I coded it like that on purpose.
The do something you're saying is supposed to make the machine "find" result_A and keep it on a straight path.

#include <IRremote.h>

/* Receiver initiallization */
int recv_1 = 6;
IRrecv irrecv_1(recv_1);
decode_results results;

/* Transmitter 1 code */
unsigned int Tx_1_code_1 = 0x3C760228; // code for transmitter 1

/* Transmitter 2 code */
unsigned int Tx_2_code_2 = 0xE6BE61DB; // code for transmitter 2

/* Transmitter 3 code */
unsigned int Tx_3_code_3 = 0xE6BE61DA; // code for transmitter 3

#define pwm_1 2 // speed of motor left BROWN WIRE
#define pwm_2 4 // speed of motor right BROWN WIRE
#define dir_1 3 // direction of left RED WIRE
#define dir_2 5 // direction of right RED WIRE
int pwm_value = 0;

void setup() {
  /* Motor */
  pinMode(pwm_1, OUTPUT);
  pinMode(dir_1, OUTPUT);
  pinMode(pwm_2, OUTPUT);
  pinMode(dir_2, OUTPUT);
  /* receiver */
  Serial.begin(9600);
  irrecv_1.enableIRIn();
}

void loop() {
  /* receiver */
  if (irrecv_1.decode(&results)) {
    path_1();
    irrecv_1.resume();
  }
}

/* This function will drive the AGV straight */
void motor_straight() {
  digitalWrite(dir_1,HIGH); // high is clockwise
  digitalWrite(dir_2,HIGH); // low will be counterclockwise
  analogWrite(pwm_1,100);
  analogWrite(pwm_2,100);
  }

/* This function will reverse the AGV */
void motor_reverse() {
  digitalWrite(dir_1,LOW); // high is clockwise
  digitalWrite(dir_2,LOW); // low will be counterclockwise
}

/* This function will turn the AGV left */
void motor_left() {
  digitalWrite (dir_1,LOW); // left wheel will spin counterclockwise to create a pivot point for turning
  digitalWrite (dir_2,HIGH); // right wheel spins in clockwise direction
  analogWrite (pwm_1, 100); // speed of left motor is 50 bits/second
  analogWrite (pwm_2, 100); // speed of right motor is 50 bits/second
  delay(1500); // motor will turn for 4 seconds
}

/* This function will turn the motor right */
void motor_right() {
  digitalWrite (dir_1,HIGH); // left wheel will spin counterclockwise to create a pivot point for turning
  digitalWrite (dir_2,LOW); // right wheel spins in clockwise direction
  analogWrite (pwm_1, 100); // speed of left motor is 50 bits/second
  analogWrite (pwm_2, 100); // speed of right motor is 50 bits/second
  delay(1600); // motor will turn for 4 seconds
}

/* This function will stop the AGV */
void motor_stop() {
    analogWrite(pwm_1,0); // speed of left motor is 0 bits/second
    analogWrite(pwm_2,0); // speed of right motor is 0 bits/second
    // The speed of the left and right motor is set to zero
}

void motor_find() {
  if(results.value != Tx_1_code_1) {
  digitalWrite (dir_1,LOW); // left wheel will spin counterclockwise to create a pivot point for turning
  digitalWrite (dir_2,HIGH); // right wheel spins in clockwise direction
  analogWrite (pwm_1, 100); // speed of left motor is 100 bits/second
  analogWrite (pwm_2, 100); // speed of right motor is 100 bits/second
  }
}

void realign() {
  if (results.value != Tx_1_code_1) {
    motor_find();
  }
}

void path_1() { // U-Turn
  if (results.value == Tx_1_code_1) {
    Serial.println("BEGIN U-TURN");
    Serial.print("RECEIVER ONE signal received: ");
    Serial.println(results.value, HEX);
    Serial.println("AGV WILL MOVE STRAIGHT");
    Serial.println("");
    irrecv_1.blink13(true);
    motor_straight();
  }
    
  else if (results.value == Tx_2_code_2) {
    Serial.println("INITIATE RIGHT TURN");
    Serial.print("RECEIVER ONE signal received: ");
    Serial.println(results.value, HEX);
    Serial.println("AGV WILL TURN RIGHT THEN CONTINUE");
    Serial.println("");
    irrecv_1.blink13(true);
    motor_right();
    motor_straight();
    }
  else if (results.value == Tx_3_code_3) {
    Serial.println("STOP THE AGV");
    Serial.print("RECEIVER ONE signal received: ");
    Serial.println(results.value, HEX);
    Serial.println("MOTOR WILL STOP");
    Serial.println("");
    irrecv_1.blink13(true);
    motor_stop();
    delay(500);
  }
  else {
   Serial.println("AGV NOT STRAIGHT");
    Serial.print("RECEIVER ONE signal received: ");
    Serial.println(results.value, HEX);
    Serial.println("REALIGN THE MOTOR");
    Serial.println("");
    irrecv_1.blink13(true);
    realign();
    delay(500);
    if (results.value == Tx_1_code_1) {
      motor_straight(); 
    }
  }
}

There's the entire code if that helps any of you. Also, what do you guys mean by the time changing the sensor value?

OK so its a line following robot - or similar.
All you need is to detect and respond to the "NOT OK" condition.

If its OK its following the line. If its NOT OK you need to know which way it has deviated, so you can correct it.

Your approach is to do a "seek right". And if not successful a "seek left".

So actually the pseudocode boils down to

if (result_A) {
seek right
}
if seek right unsuccessful {
seek left
}

How many degrees is a seek right or left?

You CAN control a line following robot like that, but it has a LOT of limitations. However you will have fun and learn a lot from the experience.

Once its working you can try some challenges. What happens when the line ends? or turns sharply?

Hi,

first of all this functionality is programmable.

I'm unsure if I understand your description right. That I can understand it you should describe it with more details
you wrote

If the machine strays off the path and doesn't read result_A, then I want it turn back so it can get the signal again and essentially keep the machine moving in a straight path.The way it actually works is that it will read result_A and go straight, and continue to go straight even if code_1 isn't being read anymore, which I coded it like that on purpose.
The do something you're saying is supposed to make the machine "find" result_A and keep it on a straight path.

So my questions are:
What do you mean by "If the machine strays off the path and doesn't read result_A, then I want it turn back"
Does "turn back" mean your vehicle is turning around 180 degrees to put the receiver looking at your IR-remote to be able to receive the IRR-signal ?"

or does it mean "turn back" in the code?

"turn back" in code is an unusual term.

So please give a description in this way:

On the IR-remote-control I press a key to send the start signal. The vehicle starts to move forward

if the vehicle strays off you do what on the IR-remote-control? nothing or a correcting command?

if the vehicles strays off what shall the program do? nothing or correct its direction automatically

What do you mean by "find" result_A ??
should the program stay in mode "A" which means go "straight forward" until you send another command "B" with the IR-remote-control?

If you have both motors given the same parameters like here

analogWrite(pwm_1,100);
  analogWrite(pwm_2,100);

And the vehicle strays off

there has to be some kind of feedback to the program about the fact that is is straying off.
This feedback could be a new command on the IR-remote-control

or this feedback could be something "onboard" of your vehicle that creates this feedback. Your arduino has no consciousness about straying off. It needs some kind of sensor to become "aware" of "Oh I'm straying off"

So what kind of sensor do you want to use?

best regards Stefan

Sorry I probably could've worded that better lol. The turn back meant that it should turn towards the transmitters, so either turn left or right until the receiver and transmitter are in line with each other.

StefanL38:
"turn back" in code is an unusual term.

On the IR-remote-control I press a key to send the start signal. The vehicle starts to move forward

if the vehicle strays off you do what on the IR-remote-control? nothing or a correcting command?
it should have a correcting command

if the vehicles strays off what shall the program do? nothing or correct its direction automatically
it should correct automatically

What do you mean by "find" result_A ??
should the program stay in mode "A" which means go "straight forward" until you send another command "B" with the IR-remote-control?
Yeah that's how I want it to work

johnerrington:
How many degrees is a seek right or left?
I gotta figure that out, probably 10 degrees left or right

You CAN control a line following robot like that, but it has a LOT of limitations. However you will have fun and learn a lot from the experience.

Once its working you can try some challenges. What happens when the line ends? or turns sharply?
It will follow a path, so it'll keep going straight until it reads the next transmitter. Then the machine will do the next command (turn left or right or whatever). I'll put up different transmitters around a room and I'll call that PATH_1. The machine will have PATH_1 coded into it so it'll do specific things each time a transmitter is read.
**I'll probably make like 2 or 3 different paths and have a touchscreen that'll give you the option of which path to take, but the transmitters will have to be setup accordingly. **

Would you like to try posting that code again

static const int SensorPin = 2; // switch pin 

int SensorStatePrevious = HIGH; // previousstate of the switch 
int Led1 = 11; 
int Led2 = 10; int buttonState; 

unsigned long AlarmTimeOut = 5000; // Time we wait before we see the press as a long press 
unsigned long AlarmTimeOut2 =10000; // Time we wait before we see the press as a time out press 
unsigned long SensorLongPressMillis; // Time in ms when we the button was pressed 
unsigned long TimeOutMillis; 
unsigned long prevTimeOutMillis; 

bool SensorStateLongPress = false; // True if it is a long press 
const int SensorCheckDuration = 50; // Time between two readings of the button state 

unsigned long prevSensorMillis; // Timestamp of the latest reading 
unsigned long SensorPressDuration; // Time the button is pressed in ms 

//// GENERAL //// 

unsigned long currentMillis; // Variabele to store the number of milleseconds since the Arduino has started 

void setup() { 

Serial.begin(9600); // Initialise the serial monitor
 
pinMode(SensorPin, INPUT); // set SensorPin as input 
pinMode(Led1, OUTPUT); 
pinMode(Led2, OUTPUT); 

Serial.println("Press button"); } // Function for reading the button state 

void readButtonState() { 

if(currentMillis - prevSensorMillis > SensorCheckDuration) { 

buttonState = digitalRead(SensorPin); 

if (buttonState == LOW && SensorStatePrevious == HIGH && !SensorStateLongPress) { 

SensorLongPressMillis = currentMillis; 
SensorStatePrevious = LOW; Serial.

I developed this code for a float switch which activates with a short press then if the press becomes long it does something else. The principle is what you are looking for. Adapt it to suit your needs

static const int SensorPin = 2;                    // switch pin
int SensorStatePrevious = HIGH;                      // previousstate of the switch
int Led1 = 11;
int Led2 = 10;
int buttonState;

unsigned long AlarmTimeOut = 5000;    // Time we wait before we see the press as a long press
unsigned long AlarmTimeOut2 =10000;    // Time we wait before we see the press as a time out press
unsigned long SensorLongPressMillis;                // Time in ms when we the button was pressed
unsigned long TimeOutMillis;
unsigned long prevTimeOutMillis;
bool SensorStateLongPress = false;                  // True if it is a long press

const int SensorCheckDuration = 50;                 // Time between two readings of the button state
unsigned long prevSensorMillis;                     // Timestamp of the latest reading

unsigned long SensorPressDuration;                  // Time the button is pressed in ms

//// GENERAL ////

unsigned long currentMillis;          // Variabele to store the number of milleseconds since the Arduino has started

void setup() 
{
  Serial.begin(9600);                 // Initialise the serial monitor

  pinMode(SensorPin, INPUT);          // set SensorPin as input
  pinMode(Led1, OUTPUT);
  pinMode(Led2, OUTPUT);
  Serial.println("Press button");
}

// Function for reading the button state
void readButtonState()
{

  if(currentMillis - prevSensorMillis > SensorCheckDuration)
  {
    buttonState = digitalRead(SensorPin);    
    if (buttonState == LOW && SensorStatePrevious == HIGH && !SensorStateLongPress)
    {
      SensorLongPressMillis = currentMillis;
      SensorStatePrevious = LOW;
      Serial.println("Button pressed");
      //digitalWrite(Led1, HIGH);
      Serial.println("1");
    }
    SensorPressDuration = currentMillis - SensorLongPressMillis;
    if (buttonState == LOW && !SensorStateLongPress && SensorPressDuration >= AlarmTimeOut && SensorPressDuration <= AlarmTimeOut2)
    {
      SensorStateLongPress = true;
      Serial.println("Button long pressed");
      Serial.println("2");
    }
    if (buttonState == LOW && SensorStateLongPress == true && SensorPressDuration >= AlarmTimeOut2)
    {
      SensorStateLongPress = false;
      Serial.println("Button time out");
      Serial.println("4");
    }
    if (buttonState == HIGH && SensorStatePrevious == LOW)
    {
      SensorStatePrevious = HIGH;
      SensorStateLongPress = false;
      Serial.println("Button released");
      Serial.println("3");
      //digitalWrite(Led1, LOW);
      //digitalWrite(Led2, LOW);
    }
    prevSensorMillis = currentMillis;
  }
}


void loop() 
{
  currentMillis = millis();    // store the current time
  readButtonState();           // read the button state 
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.