Jump to content
 

Arduino Applications and Programs


Simond
 Share

Recommended Posts

  • RMweb Gold

While you can write code to be doing all sorts of things at once responding to interrupts etc. it does make writing the sketches much harder. So Simonds suggestion of another arduino can save you a lot of time and frustration for another fiver or so. Also you dont have to worry about running out of memory or pinouts so much. 

Aslo you can come up with a standard servo driver maybe driver 3 or more servos. These can be connected to differrent control arduinos to do differrent jobs. There is something to be said for a modular approach.

 

Don

  • Like 1
  • Thanks 1
Link to post
Share on other sites

17 hours ago, 47137 said:

 

I particular, I am adding a few simple animations to my layout, barriers, gates, this sort of thing. I have a bit of a stumbling block: writing non-blocking software. Supposing I have a function to sweep a servo, and this takes four seconds to do its action, I have no idea whether it is possible to use the same function concurrently (if this is the right word), to move another servo.

 

It's entirely possible to do that - typically referred to as real-time programming. There are a few essential rules you'll have to learn. I'll probably get all sorts of criticism for this statement but IMO it's much better to do it in assembly language than anything else.

 

However it is tricky and as others have said, if you just want to get things going quickly you'd be better to use multiple micros. On the other hand you might find the challenge of writing real-time code quite addictive and more entertaining that building your layout :)

  • Thanks 1
Link to post
Share on other sites

  • RMweb Premium

Thank you Simon, Dave, Don and Andy.

 

I am going to provide a dedicated Arduino for one of my animations. This one will be a self-contained operation, a lifting barrier acting as a points repeater. I will carry on with my blocking code on the "main processor" for my other animations and lighting, and see what the finished effect looks like. After all, the human brain can only track up to four actions at the same time; and if the layout has a moving train, a Magnorail car (I'm really pleased with this so far), a barrier and one other thing, this may well be plenty. I'd like to create a bit of interest for a viewer, but not overwhelm them :-)

 

I could build my self-contained Arduino as a stand-alone project on a bit of Veroboard, with a potentiometer to adjust servo speed and a link to choose the direction of motion. Then if I need more, I can make duplicates of this, all with the same sketch. So, I have a fresh project to try too.

 

I think this will work out nicely for me.

 

(There are some notes on the car project on my layout topic). 

 

- Richard.

  • Like 1
Link to post
Share on other sites

Richard,

 

I’m greatly in favour of the modular approach, if you can reuse the hardware design, and clone the software, it makes the investment of time & effort in the design all the more worthwhile.

 

there’s a balance to be struck of course, you can make your module so multifunctional & complex that it can do “anything” but then it’s no longer a simple case of plugging in the clone and adjusting one pot, but then you’re back to the original question!

 

good luck with it!

Simon

Link to post
Share on other sites

  • RMweb Premium

I have realised, every servo will need different end stops. So for me, I will make one dedicated "controller module" to drive one servo (my barrier).

 

Conceivably, I could use this Arduino to also run a few features not needing critical timing. I'll try to remember this if I find myself running out of pins on my original "scenics processor" Arduino.

 

Thank you for your encouragement.

 

- Richard.

Link to post
Share on other sites

Nano clone, Bit of vero, three preset type pots, two for the stop positions, one for the speed.  

(Or two if you want different speeds in each direction)

 

3 pins output to servo.

 

3 pins input, +6V, 0V and “operate” (which could be momentary, as in high -> change state, or latched, as in high = position a & low = position b)

 

simple sketch either way 

 

job’s a good ‘un.

 

you could add a fourth pin to the input so it could transmit its state back to the master.

or go the whole hog, and make it CAN or DCC compatible.

wherever you go, there are rabbit holes!

 

 

 

 

  • Craftsmanship/clever 1
Link to post
Share on other sites

  • RMweb Premium

My own rabbit hole has been to add a strobe light. So after the control input changes, a yellow LED flashes four times before the barrier starts to move. The LED continues to flash until the barrier completes its movement.

 

I only want one of these so I built it without preset pots:

DSCF1366.jpg.79a1bff07e40ac789165aeccf3207c7c.jpg

 

DSCF1367.jpg.666e0e3e116b1611a370c2e5c075ff35.jpg

 

The wire links look a bit industrial! There is only one break in the copper strips, this is under the resistor.

 

Connectors from the right: power in, servo, strobe LED, control input. Happy to share the sketch if anyone would like to see it. A peer review might do me good, this is my first completed Arduino project.

 

- Richard.

 

Edited by 47137
  • Like 4
Link to post
Share on other sites

  • RMweb Premium
8 hours ago, Simond said:

INO files are allowable attachments on here.  Happy to have a look, not sure I’ll be able to add much wisdom!

 

I suppose, its main feature is two timers running at the same time.

 

If the part where I move the servo looks a bit laborious, I'm afraid I wrote this myself, a week or so before I discovered a "sweep servo" demo installed with the IDE :-)

 

- Richard.

 

 

BarrierController.ino

Link to post
Share on other sites

  • RMweb Premium

The analogue ports can be used as digital ports. If I recall correctly you can simply refer to them by symbolic names i.e. digitaRead(A1) to access analogue 1. I have come across some peculiarities (sorry can't remember the details) in using them but generally they work fine.

 

If you run into problems using them as digital you can wire in a pullup to 5volts and then have the switch connect to 0volts and use a simple comparison to see if the analogue value is less than some magic number like 300. In fact thinking about it you probably have to use an external pullup if you use them as digital ports.

 

Cheers

Dave

 

 

Edited by eldavo
  • Informative/Useful 1
Link to post
Share on other sites

  • RMweb Gold
8 hours ago, Simond said:

Without looking it up, I am not sure of the details, but I’m sure Eldavo is right.  You can use analog ports as digital inputs. 
 

Merry Christmas!

 

Happy New Year!

Simon

 

I forget the details of which board now but there is a t least one of the arduino boards where one of the Analogue pins cannot be used as digital. The others are ok. Will try to find the info.

 

NANO and Pro Mini  analogue pins A6 A7 cannot be used as digital . I dont think that applies to the pro micro

 

Don

  • Like 1
  • Friendly/supportive 1
Link to post
Share on other sites

  • RMweb Premium
19 hours ago, Simond said:

Without looking it up, I am not sure of the details, but I’m sure Eldavo is right.  You can use analog ports as digital inputs.

 

Yes. Just:

 

pinMode(A0, INPUT_PULLUP);

 

Then put a switch between the analogue pin and the 0V line. This seems to work fine for A0 to A5, but I don't know about the extra two analogue pins on the Nano.

 

I found myself having to do this because I bought one of the old-fashioned motor driver shields, and this consumed most of the digital pins.

 

- Richard.

  • Friendly/supportive 1
Link to post
Share on other sites

Thanks Gents.

I’ll give this a try as I don’t seem to be getting any joy with dialling out backlash on my turntable (see Simond’s turntable thread mentioned previously) so I thought I’d add two more push buttons to nudge the deck

a step or two either way.

 

andy 

Edited by andysollis1
Auto corruption corrections
Link to post
Share on other sites

  • RMweb Premium
14 hours ago, andysollis1 said:

Thanks Gents.

I’ll give this a try as I don’t seem to be getting any joy with dialling out backlash on my turntable (see Simond’s turntable thread mentioned previously) so I thought I’d add two more push buttons to nudge the deck

a step or two either way.

 

andy 

I am so sorry I missed the auto corruption. Some of them are very good.

 

A while ago I built the Peco turntable for manual operation and added a neodymium magnet at each end of the deck. One of these meets up with a mating magnet on the bottom of the well to act as a sort of a latch. It's still working nearly five years on. If you could get the magnets the right size, so they hold the deck in position but pull away when the motor moves, this might give you the final alignment you need.

 

https://www.rmweb.co.uk/community/index.php?/topic/94323-compact-fiddle-yard-for-165-mm-gauge/&do=findComment&comment=1751052

 

(Sorry I can't find the link to the turntable thread you mention)

 

- Richard.

  • Funny 1
Link to post
Share on other sites

“Simond’s turntable threat” ILO “Simond’s turntable thread”. :)

 

I assume we’re talking about this thread.  Way back in the sands of time I was working on my turntable.  It’s so long ago I’ve forgotten more than I remember!

 

My stepper has an integral gearhead, which does suffer from backlash.  As is alway the case, there is more than one way of skinning the cat, and I initially used a constant, which I added to, or subtracted from the step count on reversal.  I’m sure it could be made to work, but I reviewed it many times and got very fed up.  I solved the backlash problem, and incremental errors (probably but not certainly missed steps) by a) allowing the table to only rotate anti-clockwise and b) resetting the step count every time it passes the photo sensor.

 

on my layout, there is almost never a need to turn clockwise, so the loss of this minor element of fidelity is not an issue, and is far outweighed by the table hitting its presets every time.

 

hope this helps

Happy New Year!

Simon

  • Friendly/supportive 1
Link to post
Share on other sites

7 hours ago, Simond said:

“Simond’s turntable threat” ILO “Simond’s turntable thread”. :)

 

I assume we’re talking about this thread.  Way back in the sands of time I was working on my turntable.  It’s so long ago I’ve forgotten more than I remember!

 

My stepper has an integral gearhead, which does suffer from backlash.  As is alway the case, there is more than one way of skinning the cat, and I initially used a constant, which I added to, or subtracted from the step count on reversal.  I’m sure it could be made to work, but I reviewed it many times and got very fed up.  I solved the backlash problem, and incremental errors (probably but not certainly missed steps) by a) allowing the table to only rotate anti-clockwise and b) resetting the step count every time it passes the photo sensor.

 

on my layout, there is almost never a need to turn clockwise, so the loss of this minor element of fidelity is not an issue, and is far outweighed by the table hitting its presets every time.

 

hope this helps

Happy New Year!

Simon

I think it will probably be more fun in the end to just add the extra nudge buttons. 
 

what I’ve tried and failed in is actually doing the nudge so far. I can move to a preset position, but it seems to throw an error when I add in to the sketch a +or- figure ? I’m guess I’m not doing it right as still learning. It’s not quite like the BBC basic and Comodore basic I did 30 Years ago as a kid ! 

  • Friendly/supportive 1
Link to post
Share on other sites

/* Example sketch to control a Peco Turntable in OO using a 28BYJ-48 stepper motor with ULN2003 driver board, AccelStepper and Arduino UNO: acceleration and deceleration. By A.Sollis Dec 2020 */

// Include the AccelStepper library:
#include <Stepper.h> 
#include <AccelStepper.h>
#include <Wire.h>

// Motor pin definitions:
#define motorPin1  8      // IN1 on the ULN2003 driver
#define motorPin2  9      // IN2 on the ULN2003 driver
#define motorPin3  10     // IN3 on the ULN2003 driver
#define motorPin4  11     // IN4 on the ULN2003 driver

// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);
int Pin07Read ;  // step Road 1 (head)
int Pin06Read ;  // step Road 2 (head)
int Pin05Read ;  // step Road 3 (head)
int Pin04Read ;  // step Road 1 (tail)
int Pin03Read ;  // step Road 2 (tail)
int Pin02Read ;  // step Road 3 (tail)
int PinA1Read ;  // Nudge left
int PinA2Read ;  // Nudge right

void setup() {
  digitalWrite(16,HIGH);
  digitalWrite(15,HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(2, HIGH);

  Serial.begin(9600); // Start screen print

  // Set the maximum steps per second:
  stepper.setMaxSpeed(60);
  // Set the maximum acceleration in steps per second^2:
  stepper.setAcceleration(5);

  //configure pin13 as an input and enable the internal pull-up resistor
  pinMode(13, INPUT_PULLUP);
  //configure analogue pin 1 and 2 to be used for "nudge" sequence
  pinMode(A1, INPUT_PULLUP); 
  pinMode(A2, INPUT_PULLUP);

  //read the sensor (open collector type) value into a variable

  int sensorVal = digitalRead(13);

  // if near reference point move away

  sensorVal = digitalRead(13);

  Serial.println("Finding Sensor Clockwise"); // Turn clockwise to find sensor
  while (sensorVal == HIGH) {
    sensorVal = digitalRead(13);
    stepper.moveTo(stepper.currentPosition() + 1000);
    stepper.runSpeedToPosition();

    delay(5);
  }

  // step forward to sensor index point
  Serial.println("Finding Sensor anti clockwise"); // turn to find sensor
  while (sensorVal == LOW) {
    sensorVal = digitalRead(13);
    stepper.moveTo(stepper.currentPosition() - 100);
    stepper.runSpeedToPosition();

    delay(50);
  }
  stepper.setCurrentPosition(0);
 }

void loop() {

  // run to position with set acceleration
  stepper.runToPosition();

  Pin07Read = digitalRead(7) ;
  Pin06Read = digitalRead(6) ;
  Pin05Read = digitalRead(5) ;
  Pin04Read = digitalRead(4) ;
  Pin03Read = digitalRead(3) ;
  Pin02Read = digitalRead(2) ;
  PinA1Read = digitalRead(15) ;
  PinA2Read = digitalRead(16) ;
  
 
  //  check which button is being pressed - chooses which road is set

 if (PinA1Read == LOW)
  {
    stepper.move(+20);
    Serial.println("Nudge Clockwise: ");
    delay(500);
  }
  else

    if (PinA2Read == LOW)
    {
        stepper.move(-20);      
      Serial.print("Nudge anti-clockwise: ");
      delay(500);
    }
    else

  
  if (Pin07Read == LOW)
  {
    stepper.moveTo(350);
    Serial.println("Current Road 3  +350: ");
  }
  else

    if (Pin06Read == LOW)
    {
        stepper.moveTo(0);      
      Serial.print("Current Road 2 zero: ");
    }
    else

      if (Pin05Read == LOW)
      {
        stepper.moveTo(-350);
        Serial.print("Current Road 1 -350: ");
      }
      else

        if (Pin04Read == LOW)
        {
          stepper.moveTo(2400);
          Serial.print("Current Road 6 2400: ");

        }
        else

          if (Pin03Read == LOW)
          {
            stepper.moveTo(2090);
            Serial.print("Current Road 5 2090: ");
          }
          else

            if (Pin02Read == LOW)
            {
              stepper.moveTo(1725);
              Serial.print("Current Road 4 1725: ");
            }
            else
            {

              digitalWrite(8, LOW);
              digitalWrite(9, LOW);
              digitalWrite(10, LOW);
              digitalWrite(11, LOW);;
              Serial.print("Current steppermotor position: ");
              Serial.println(stepper.currentPosition());
            }

}

 

Here is my code should anyone be interested. 6 pre-set positions, hall sensor to set zero from the deck.

 

Andy

  • Like 1
Link to post
Share on other sites

Hello.

 

This is a bit of a plea for help! I have been building a machine to cut my own gears. It uses 2 stepper motors to drive the 2 axis I need.

 

image.png.0cc50d114cddb6ea063ab1a0d5a546b4.png

 

The idea was to use an arduino to drive the 2 motors. They need to drive one motor at a set ratio to the other which is defined by the number of required teeth of the gear.

 

image.png.95321c789d8fff36f5b477938f5f881d.png

 

The thing is, I have been working on trying to work out the arduino drive of the machine for a few months now and to be honest, I have got nowhere after trying out several different types of drive etc. I have limited knowledge of coding and this is stressing me out!

 

Is there anyone able and willing to help me out or point me in the right direction?

 

Thanks

 

Missy.

  • Interesting/Thought-provoking 1
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
 Share

×
×
  • Create New...