Author Topic: Q for the audurino experts  (Read 2209 times)

0 Members and 1 Guest are viewing this topic.

Lemosteam

  • Crew
  • *
  • Posts: 5919
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +3667
    • Designer at Keystone Details
Q for the audurino experts
« on: March 21, 2021, 08:22:15 PM »
0
I am trying to control a servo and a motor at the same time. The kit I have has an infrared receiver, and a matching mini remote keypad. I can get the servo to act in the manner I want, and I can get the motor to run for a specified amount of time.

HOWEVER,

I cannot press any other buttons while the motor is running, and even thought the code is set to run the motor for 3 seconds, there is an awful delay until the IR receiver become available again for another.

I notice that the IR led flickers as the motor runs and stops after the delay. I cannot make any inputs from the remote until that stops.

Is the motor putting feedback or a signal into the circuit while it runs? Is there an electronic device I can introduce into the circuit to prevent the feedback from the motor?

Is there any way to bypass that signal or prevent the delay?

I want to be able to keep the motor running while I control the servo.

Is this possible and maybe som code secrets that someone can share.

My current code will post shortly:

“#include <IRremote.h> //Include remote library
#include <Servo.h> //Include servo library
 
//Define each of the necessary buttons as their respective codes
#define play_button 765
#define off_button 41565
#define rewind_button 8925
#define fastforward_button 49725
#define funcstop_button 57885
#define zero_button 26775
 
int receiver_pin = 2; //Initialize pin 2 for IR receiver
int motor_right = 8; //Initialize pin 8 for motor to spin forward
int motor_left = 12; //Initialize pin 12 for motor to spin backward
int servo_pin = 5; //Initialize pin 5 for the servo
 
int degree = 90; //define degree variable to be 90 at start
 
Servo Servo1; //Create a servo object called Servo1
 
IRrecv receiver(receiver_pin); //Arduino will take output of IR receiver from pin 2
decode_results output; //Decode the results from the IR receiver
 
 
void setup()
{
  Serial.begin(9600);
  receiver.enableIRIn(); // Start to take the output from IR receiver
  //Setup the two motor pins as outputs and setup the servo pin
  pinMode(motor_right, OUTPUT);
  pinMode(motor_left, OUTPUT);
  Servo1.attach(servo_pin);
}
 
 
void loop()
{
  if (receiver.decode(&output)) //if receiver output is one of the previously defined values
  {
    unsigned int value = output.value;
    switch(value) //switch the function of the circuit
      //based on the output value from the IR receiver
    {
      case play_button: //spin motor forward when play button pressed for 3 sec
      digitalWrite(motor_right,HIGH);
      delay(1000);
      digitalWrite(motor_right,LOW);
      digitalWrite(motor_left,LOW);
      break;
     
      case funcstop_button: //Spin motor backward when func/stop button pressed for 3 sec
      digitalWrite(motor_left,HIGH);
      delay(3000);
      digitalWrite(motor_right,LOW);
      digitalWrite(motor_left,LOW);
      break;
     
      case off_button: //Stop motor when off button pressed
      digitalWrite(motor_right,LOW);
      digitalWrite(motor_left,LOW);
      break;
     
      case rewind_button: //Increase degree by 10 when rewind button pressed
      degree+=10;
      if(degree>180) degree=180;
      Servo1.write(degree);
      break;
     
      case fastforward_button: //Decrease degree by 10 when fast forward button pressed
      degree-=10;
      if(degree<0) degree=0;
      Servo1.write(degree);
      break;
     
      case zero_button: //Set servo back to 90 degrees when zero button pressed
      degree=90;
      Servo1.write(degree);
      break;
     
    }
    receiver.resume(); //Resume communication with IR receiver”
« Last Edit: March 21, 2021, 08:38:57 PM by Lemosteam »

Rasputen

  • Crew
  • *
  • Posts: 524
  • Respect: +309
Re: Q for the audurino experts
« Reply #1 on: March 21, 2021, 08:57:58 PM »
+1
You've got delays of 1 and 3 seconds in there while the motor is running.  The Arduino will not do anything else while the delay function is running.

Lemosteam

  • Crew
  • *
  • Posts: 5919
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +3667
    • Designer at Keystone Details
Re: Q for the audurino experts
« Reply #2 on: March 21, 2021, 09:06:37 PM »
0
Without them the motor never stops.

Rasputen

  • Crew
  • *
  • Posts: 524
  • Respect: +309
Re: Q for the audurino experts
« Reply #3 on: March 21, 2021, 09:12:06 PM »
0
I'm not sure what you are trying to accomplish, but don't use the delay() function to control the length of time it runs.  Instead, look at the master time when you started the motor, and every loop you can compare the current time with the time you started running the motor.  If the running time has exceeded your desired run time, then stop the motor.  Otherwise do noting for that loop.


wcfn100

  • Crew
  • *
  • Posts: 8841
  • Respect: +1221
    • Chicago Great Western Modeler
Re: Q for the audurino experts
« Reply #4 on: March 21, 2021, 09:19:52 PM »
0
I'm not proficient in arduino, but I see your loop function has 3 open brackets but only 1 closed bracket.  Is that okay?


Jason

ednadolski

  • Crew
  • *
  • Posts: 4813
  • Respect: +1757
Re: Q for the audurino experts
« Reply #5 on: March 21, 2021, 09:45:57 PM »
0
I see your loop function has 3 open brackets but only 1 closed bracket.  Is that okay?

It appears to be truncated.

Ed

Lemosteam

  • Crew
  • *
  • Posts: 5919
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +3667
    • Designer at Keystone Details
Re: Q for the audurino experts
« Reply #6 on: March 21, 2021, 09:50:50 PM »
0
I should have explained that I am a total noob with a kit so the lingo is flying over my head.

I want to be able to start the motor, and then be able to use the servo while the motor is running and be able to top the motor as well. Will what you suggest allow that?

It’s for a bridge/ turnout control, but think of a remote control car, throttle and steer at the same time.

Any brackets missing from the code are intentional. The code runs fine.

Th post was interpreting the code as well, code, lol.

Rasputen

  • Crew
  • *
  • Posts: 524
  • Respect: +309
Re: Q for the audurino experts
« Reply #7 on: March 21, 2021, 09:59:59 PM »
0
How do you want to stop the motor?  A user input?  A maximum run time??  Some other condition???

Lemosteam

  • Crew
  • *
  • Posts: 5919
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +3667
    • Designer at Keystone Details
Re: Q for the audurino experts
« Reply #8 on: March 21, 2021, 10:29:20 PM »
0
User input from the remote. Maybe a max run time, might work too.

wcfn100

  • Crew
  • *
  • Posts: 8841
  • Respect: +1221
    • Chicago Great Western Modeler
Re: Q for the audurino experts
« Reply #9 on: March 21, 2021, 11:10:35 PM »
0

Any brackets missing from the code are intentional. The code runs fine.

First of all, close your braces.  Just do it.

From what I read, an 'If' statement without braces only handles the next line of code.  Everything after the first line will run no matter what the "If" statement evaluates too.

In this case your switch statement might be running regardless of the "If" value.


Jason

ednadolski

  • Crew
  • *
  • Posts: 4813
  • Respect: +1757
Re: Q for the audurino experts
« Reply #10 on: March 21, 2021, 11:22:35 PM »
0
I want to be able to start the motor, and then be able to use the servo while the motor is running and be able to top the motor as well.

To do that from a single loop you might want something that tracks the current status of each device.  On each loop, read the state of each IR button, compute the next/desired state for each device based upon the current state, toggle the output control signals as needed, and update the tracked states.   Repeat the loop forever (until some quit or reset condition is reached).

Is that Servo1.write(degree) a synchronous call, that is, it does not return from the call until the servo has moved the requested amount?   If so then the control loop won't be able to do anything else while the servo is moving.

(If it is synchronous and that is too long a time for the control loop to be doing nothing else, then you can make the amount of movement smaller, and use a sequence of consecutive calls across multiple iterations of the loop.)


User input from the remote. Maybe a max run time, might work too.

I'd suggest both, unless you want to allow the motor to run on indefinitely.


From what I read, an 'If' statement without braces only handles the next line of code.

That's correct if there are no braces with the 'if'.   An opening brace with no corresponding closing brace is a syntax error (code won't compile).   The compiler treats all newlines, tabs and space characters as whitespace, meaning that it does not care about the alignment of the braces -- so if you are missing a closing brace in one place (where you intended) but have another closing brace somewhere else, then the code block will not have the scope that you wanted, and will probably not do what you intended (even if it compiles).


Ed


« Last Edit: March 21, 2021, 11:37:56 PM by ednadolski »

ednadolski

  • Crew
  • *
  • Posts: 4813
  • Respect: +1757
Re: Q for the audurino experts
« Reply #11 on: March 21, 2021, 11:31:08 PM »
0
      case play_button: //spin motor forward when play button pressed for 3 sec

This seems ambiguous: does this mean the play button has to be pressed (and held) for 3 seconds before the motor starts to move, or that the motor should move for a duration of 3 seconds whenever a press of the play button is detected?

Ed

Lemosteam

  • Crew
  • *
  • Posts: 5919
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +3667
    • Designer at Keystone Details
Re: Q for the audurino experts
« Reply #12 on: March 22, 2021, 06:17:51 PM »
0
So the Audruino is not a multi-tasking processor.

Any ideas on some coding strings to a hive the desired effect?

ednadolski

  • Crew
  • *
  • Posts: 4813
  • Respect: +1757
Re: Q for the audurino experts
« Reply #13 on: March 22, 2021, 08:50:29 PM »
0
So the Audruino is not a multi-tasking processor.

Be careful what you ask for...  :ashat:   ;)


Any ideas on some coding strings to a hive the desired effect?

My advice would be to start with something very simple and add to it incrementally after getting something basic to work.   Trying to do too much at once is a recipe for frustration (even if you've been writing code for a long time).

Ed

kiwi_al

  • Crew
  • *
  • Posts: 1409
  • Gender: Male
  • Respect: +417
Re: Q for the audurino experts
« Reply #14 on: March 23, 2021, 06:31:33 AM »
0
#include <IRremote.h> //Include remote library
#include <Servo.h> //Include servo library
 
//Define each of the necessary buttons as their respective codes
#define play_button 765
#define off_button 41565
#define rewind_button 8925
#define fastforward_button 49725
#define funcstop_button 57885
#define zero_button 26775
 
int receiver_pin = 2; //Initialize pin 2 for IR receiver
int motor_right = 8; //Initialize pin 8 for motor to spin forward
int motor_left = 12; //Initialize pin 12 for motor to spin backward
int servo_pin = 5; //Initialize pin 5 for the servo
 
int degree = 90; //define degree variable to be 90 at start
 
Servo Servo1; //Create a servo object called Servo1
 
IRrecv receiver(receiver_pin); //Arduino will take output of IR receiver from pin 2
decode_results output; //Decode the results from the IR receiver
 
 
void setup()
{
  Serial.begin(9600);
  receiver.enableIRIn(); // Start to take the output from IR receiver
  //Setup the two motor pins as outputs and setup the servo pin
  pinMode(motor_right, OUTPUT);
  pinMode(motor_left, OUTPUT);
  Servo1.attach(servo_pin);
}
 
 
void loop()
{
  if (receiver.decode(&output)) //if receiver output is one of the previously defined values
  {
    unsigned int value = output.value;
    switch(value) //switch the function of the circuit
      //based on the output value from the IR receiver
    {
      case play_button: //spin motor forward when play button pressed for 3 sec
      digitalWrite(motor_right,HIGH);
      delay(1000);
      digitalWrite(motor_right,LOW);
      digitalWrite(motor_left,LOW);
      break;
     
      case funcstop_button: //Spin motor backward when func/stop button pressed for 3 sec
      digitalWrite(motor_left,HIGH);
      delay(3000);
      digitalWrite(motor_right,LOW);
      digitalWrite(motor_left,LOW);
      break;
     
      case off_button: //Stop motor when off button pressed
      digitalWrite(motor_right,LOW);
      digitalWrite(motor_left,LOW);
      break;
     
      case rewind_button: //Increase degree by 10 when rewind button pressed
      degree+=10;
      if(degree>180) degree=180;
      Servo1.write(degree);
      break;
     
      case fastforward_button: //Decrease degree by 10 when fast forward button pressed
      degree-=10;
      if(degree<0) degree=0;
      Servo1.write(degree);
      break;
     
      case zero_button: //Set servo back to 90 degrees when zero button pressed
      degree=90;
      Servo1.write(degree);
      break;
  //where's your default?   
    } //switch
    } //if
    } //end of loop routine
    receiver.resume(); //Resume communication with IR receiver”
    // where is your main() ????
   //e.g void main ( void) or int main (void)
   //      {
   //         setup();
  //          loop();
  //          do something else;
 //         }

 If you only posted a code snippet please post the rest of your code, I haven't programmed an arduino but do have some experience with the C programming language   :D :D