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

0 Members and 1 Guest are viewing this topic.

Lemosteam

  • Crew
  • *
  • Posts: 4735
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +1715
    • Designer at Keystone Details
Re: Q for the audurino experts
« Reply #15 on: March 23, 2021, 07:42:23 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
   

@kiwi_al Thank you so much.  I will try this right away and report back!

BTW that was the entire code posted. I pm;y removed the intial and clos code cmmands, and maybe some brackets and braces...

ednadolski

  • Crew
  • *
  • Posts: 3825
  • Respect: +720
Re: Q for the audurino experts
« Reply #16 on: March 23, 2021, 09:23:26 AM »
0
    // where is your main() ????
   //e.g void main ( void) or int main (void)

You don't need to write a main() function for arduino.  IIRC there already is a canned one that calls setup() and loop().  (If you wrote one, the linker/loader would probably complain about a duplicate symbol.)

Ed
« Last Edit: March 23, 2021, 09:25:05 AM by ednadolski »

Mark W

  • Crew
  • *
  • Posts: 1976
  • Respect: +2080
    • Free-moNebraska
Re: Q for the audurino experts
« Reply #17 on: March 23, 2021, 10:19:17 AM »
0
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.

As Rasputen mentioned, you need to delay without 'delay()', which will allow you to read button press while the motor is spinning.

Check out https://www.arduino.cc/en/pmwiki.php?n=Tutorial/BlinkWithoutDelay

Quote
Sometimes you need to do two things at once. For example you might want to blink an LED while reading a button press. In this case, you can't use delay(), because Arduino pauses your program during the delay(). If the button is pressed while Arduino is paused waiting for the delay() to pass, your program will miss the button press.

Contact me about custom model building.
Learn more about Free-moNebraska.
Learn more about HOn3-mo.

kiwi_al

  • Crew
  • *
  • Posts: 1005
  • Gender: Male
  • Respect: +63
Re: Q for the audurino experts
« Reply #18 on: March 23, 2021, 12:46:02 PM »
0
You don't need to write a main() function for arduino.  IIRC there already is a canned one that calls setup() and loop().  (If you wrote one, the linker/loader would probably complain about a duplicate symbol.)

Ed
Oops ok so I guess you guys are using the arduino IDE thingy? I guess I'd better get modern and play with one of these. The last chipset I programmed in C was 20 years ago. I'll let you young ones sort it   :D :D

svedblen

  • Crew
  • *
  • Posts: 575
  • Gender: Male
  • Respect: +169
    • Three Yards Yard - beware - it is H0 - No, now it's O
Re: Q for the audurino experts
« Reply #19 on: March 23, 2021, 12:51:47 PM »
0
As Rasputen mentioned, ...
I second that. Just do as Rasputen and Mark W advice.
Lennart

Lemosteam

  • Crew
  • *
  • Posts: 4735
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +1715
    • Designer at Keystone Details
Re: Q for the audurino experts
« Reply #20 on: March 23, 2021, 02:09:37 PM »
0
Thanks everyone!  Also I forgot to ask, can the Audruino control the motor voltage, either at a steady voltage or if you pressed the button again the motor would increase speed or decrease speed?

Rasputen

  • Crew
  • *
  • Posts: 391
  • Respect: +206
Re: Q for the audurino experts
« Reply #21 on: March 23, 2021, 02:40:25 PM »
0
Normally you would use a separate driver and supply a PWM signal to it.

Lemosteam

  • Crew
  • *
  • Posts: 4735
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +1715
    • Designer at Keystone Details
Re: Q for the audurino experts
« Reply #22 on: March 23, 2021, 02:51:58 PM »
0
Normally you would use a separate driver and supply a PWM signal to it.

@Rasputen , laymen's terms- I really am a total noob here.  What is a driver and what is a PWM signal and can i assume you are referring to the motor?

Rasputen

  • Crew
  • *
  • Posts: 391
  • Respect: +206
Re: Q for the audurino experts
« Reply #23 on: March 23, 2021, 02:58:24 PM »
0
In this case, the driver is a separate chip to control the motor, driven by a pulse width modulated signal from the Arduino.

ednadolski

  • Crew
  • *
  • Posts: 3825
  • Respect: +720
Re: Q for the audurino experts
« Reply #24 on: March 23, 2021, 03:12:45 PM »
0
In this case, the driver is a separate chip to control the motor, driven by a pulse width modulated signal from the Arduino.

PWM is an electrical signal that is delivered to a motor as a sequence of on-off 'pulses', which is more easily and accurately controlled from a computer chip.  Contrast to something like an analog voltage level that is applied at a certain level to control the rotation, i.e. the signal from a traditional DC power supply.

Software control of PWM usually makes use of a dedicated hardware controller chip.  The job of the software is to program the controller chip to deliver the desired PWM signal.  The specific programming depends upon the design of the chip; such programming is commonly referred to as a 'driver'.  (Note the hardware controller chip is often called a 'driver' as well -- the term is insanely overloaded.)

A processor such as an Arduino typically lacks the specific hardware circuitry needed to generate a PWM signal, hence the need for a controller chip. Other kinds of peripheral hardware devices such as say a Bluetooth radio have their own specific requirements, but also have a controller that interfaces to software running on a general purpose processor.  Thus one processor can control many kinds of hardware peripherals. Controllers such as arduino often have libraries of pre-written drivers for various kinds of peripherals (otherwise every programmer would have to know the details of how to program each hardware controller in order to use them.)

More on PWM:https://en.wikipedia.org/wiki/Pulse-width_modulation

Ed
« Last Edit: March 23, 2021, 03:20:04 PM by ednadolski »

Lemosteam

  • Crew
  • *
  • Posts: 4735
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +1715
    • Designer at Keystone Details
Re: Q for the audurino experts
« Reply #25 on: March 23, 2021, 03:26:26 PM »
0
You guys are having fun with me this aren't you??   :D :D :trollface: :D :D

ednadolski

  • Crew
  • *
  • Posts: 3825
  • Respect: +720
Re: Q for the audurino experts
« Reply #26 on: March 23, 2021, 03:34:47 PM »
0
You guys are having fun with me this aren't you??   :D :D :trollface: :D :D

Well, there are always the data sheets for the PWM controller chips....  :scared:

Ed

peteski

  • Crew
  • *
  • Posts: 25629
  • Gender: Male
  • Honorary Resident Curmudgeon
  • Respect: +2679
    • Coming (not so) soon...
Re: Q for the audurino experts
« Reply #27 on: March 23, 2021, 05:45:58 PM »
0
PWM is also the method DCC decoders use to control motor speed, and function output voltages (for lighting effects).

Wow, that brings back some memories of when I made a "car computer" for my Camaro.  It was Z80-based (an 8-pit processor often used in the 1980s).  I wrote its programming on paper using the Z80 mnemonics, then I typed in all the machine code instructions by hand into an EPROM chip, using a Dato IO universal prom burner.  The program was "multitasking".  Like someone mentioned earlier, it was actually a loop calling bunch of routines that serviced various tasks (and relied on timing generated by an external pulse generator triggering a non-maskable interrupt (NMI).

I have not dabbled in today's microcontrollers, but they look easier to program, and of course much more powerful.

Even if the operating system is multitasking, if getting down to the hardware level, on a single processor (CPU) hardware, the multitasking is done by time slicing. Basically, since the CPU can only run a single instruction at a time, the various tasks are all executed on the processor a little chunk at a time.  But it all happens behind the scenes, and so fast that to us humans it appears that multiple things are running at the same time.

With today's multi-processor hardware, and multi-threaded programs, things get way more complicated.  There are semaphores (no, no the RR type), IPC,  m-o-u-s-e.  :-)  Way over my pay grade.

John,  if you want to include programming code in your post, try using the "insert code" tags The forum software should then ignore what is between the code tags.

I should look like this:
Code: [Select]
#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
« Last Edit: March 23, 2021, 05:49:01 PM by peteski »
. . . 42 . . .

Lemosteam

  • Crew
  • *
  • Posts: 4735
  • Gender: Male
  • PRR, The Standard Railroad of my World
  • Respect: +1715
    • Designer at Keystone Details
Re: Q for the audurino experts
« Reply #28 on: March 23, 2021, 09:10:08 PM »
0
So I tried the code using the clock and still no go. BUT, I replaced the motor with an LED and wha-la! Remote works fine, and the ir receiver picks up all inputs.

Also, I must mention that the Tinkercad Auruino simulator works perfectly, with or without the delay command.

I am convinced that one of two things is preventing the IR from receiving a new signal from the remote:

It is either electronic interference frequencies from the motor running

OR

The overall circuit simply does not have enough voltage to run all of the devices simultaneously.

Any more bright ideas?

Q. Would there be an electronic solution to the interference?