Jump to content
 

Arduino Applications and Programs


Simond
 Share

Recommended Posts

4 hours ago, melmerby said:

Off on a different tangent.

Anybody tried the Arduino Nano RP 2040 connect?

It's an Arduino Nano that is also a R Pi Pico

image.png.137d4a3e7a548b3278958690dd3053da.png

Hi,

 

Its an Arduino that happens to contain the RP2040 microcontroller that is used in the Raspberry Pi Pico.

 

I'm interested in its introduction as it might result in the Arduino IDE supporting the Pico in addition to the Nano RP2040 Connect.

 

Regards

 

Nick

 

 

Link to post
Share on other sites

  • RMweb Premium
4 hours ago, NIK said:

Hi,

 

Its an Arduino that happens to contain the RP2040 microcontroller that is used in the Raspberry Pi Pico.

 

I'm interested in its introduction as it might result in the Arduino IDE supporting the Pico in addition to the Nano RP2040 Connect.

 

Regards

 

Nick

 

 

Hi Nick

Strangely it isn't listed amongst the official Arduino boards on their website but is shown as part of the RPi Pico family by Pi Hut.

 

On the Arduino Forum there is a question regarding V2.0 of the IDE (unanswered) about: "When will be available the support of RP2040? Is there a beta version of it already?"

Link to post
Share on other sites

Hi,

 

If you google 'Arduino Nano RP 2040 connect' then you should see a link to Tom's hardware which says it is a MCU board being made for the Arduino organisation.

 

Looks like it has not been released yet - waiting for Arduino IDE to be extended?.

 

Regards

 

Nick

Link to post
Share on other sites

  • RMweb Premium
1 hour ago, NIK said:

Hi,

 

If you google 'Arduino Nano RP 2040 connect' then you should see a link to Tom's hardware which says it is a MCU board being made for the Arduino organisation.

 

Looks like it has not been released yet - waiting for Arduino IDE to be extended?.

 

Regards

 

Nick

In stock at the Pi Hut:

https://thepihut.com/collections/pico/products/arduino-nano-rp2040-connect

Link to post
Share on other sites

1 hour ago, melmerby said:

Hi,

 

So hopefully the Arduino IDE has been extended to support that board.

 

Looks like it might fit inside a OO coach if needs be.

 

I'm not sure yet what using an RP2040 as the MCU does to help the user.

 

I suppose its a non Chinese MCU (die by TSMC of Taiwan, packaged in Japan) so is not likely to be embargoed by the rest of the world.

Also the documentation seems to be very good.

It may be the smallest low cost Dual Core MCU on the market - they've fitted it in a Nano form factor.

 

Micro Python on a MCU board is a nice feature as is the free Open MV license for machine vision

 

The PIO state machines are an interesting feature for the price - I had a Pico using 4 state machines coordinated by a simple C++ program on one core running for two months without problems.

 

Hopefully the RP2040 will be on the market for many years although whether there will be clones of the RP2040 and the boards it features on is less sure.

 

Regards

 

Nick

Link to post
Share on other sites

  • RMweb Premium

Some info has appeared on the Arduino site (it was only released 4 days ago):

https://blog.arduino.cc/2021/05/17/the-arduino-nano-rp2040-connect-is-here/

 

Cheap Chinese copies are less likely than normal Arduinos as unlike the Atmel based Arduinos the RPi stuff isn't open source and the Arm Processors aren't "off the shelf" devices

Edited by melmerby
Link to post
Share on other sites

On 22/05/2021 at 00:07, melmerby said:

As a test I uploaded the standard Arduino "blink.ino" into the Pico using the IDE and it works!

Hi,

 

That's good to hear.

The Pi Hut are stocking a plug in type breadboard with the Pico pin descriptions printed on it.

That will help all of us without X-ray vision who can't see the printing on the underside of the Pico.

 

Regards

 

Nick

 

Link to post
Share on other sites

  • RMweb Premium
Just now, NIK said:

Hi,

 

That's good to hear.

The Pi Hut are stocking a plug in type breadboard with the Pico pin descriptions printed on it.

That will help all of us without X-ray vision who can't see the printing on the underside of the Pico.

 

Regards

 

Nick

 

I've got an on screen picture which is OK.

 

 

I've recently noticed Arduino IDE sketches for Picos starting to appear on the 'net for popular peripherals such as the TMC2208 silent stepper.

Now the two organisations have started to co-operate it's all good!

Link to post
Share on other sites

  • 3 weeks later...
  • RMweb Premium

I'm just starting on the coding of a sketch that will see some 20 DCC Concepts' levers connected to an Arduino Mega to control one or more of 27 servos controlling points /signals - the servos themselves will be connected to MERG Servo4s but that's not for discussion.

 

The sketch will poll each lever in turn to ascertain whether its state has changed since the previous poll.

 

There will then be code that will detect whether the lever is now normal or reversed if the lever's state has changed and (generally) operate the appropriate servo(s) accordingly. The new state of the lever will be stored for future reference.

 

Some (signal operating) servos will only be operated if the relevant points have been set and this will be based on the state of the point's lever.

 

My first question concerns the declaration of the switches and servos. Do they have to be declared/defined individually or can they be declared/defined into something like an array? Using an array or something similar will cut the size of the sketch down.

 

The initial version of the sketch will simply use a specific lever to always operate a default servo so, for example, lever 15 will operate (signal) servo 15a without testing the setting of the relevant points. The other niceties will be incorporated into the sketch in due course. 

 

There'll probably be a few more questions as the sketch develops.

 

Thank you in advance for your responses.

Link to post
Share on other sites

  • RMweb Premium

I may be missing something but if you are simply connecting the switches and servos to digital ports on the Arduino all you need to know is the port number. That assumes you are using the high level access methods such as digitalRead() etc. You can keep the port numbers in an array of "int"s or some other numerical type or, if they are wired sequentially, you could just define the lowest port number and the max number of switches/servos.

 

There are obviously wrinkles if you are using the Arduino native PWM capabilities as the ports are not sequential so you might use the array approach.

 

It really depends on what is convenient for what you are trying to do.

 

Cheers

Dave

 

 

Link to post
Share on other sites

  • RMweb Premium

Thanks for the response Dave.

 

What  I was trying to avoid is having 20 lines of code like

int lever1 = 8;

int lever2 = 9;

and so on

 

and again

 

pinMode(lever1, INPUT_PULLUP);
pinMode(lever2, INPUT_PULLUP);
and so on

 

And then there's the similar (output) code for the 27 servos

Link to post
Share on other sites

  • RMweb Gold

Hi Ray

 

yes you can do what you want 

 

Asuming all the pins to be used are digital

 

Int   Leverpins[20]={1,3,4,5etc.};       //asssigning the pins to the levers note array starts from 0

 

 

For (int lever = 0;  lever>20; lever++) {              //   for 20 levers

     pinMode(Leverpin[lever], INPUT_PULLUP);

     }                                                       // will set up the assigned pin    

 

then to scan the pins

 

For (int lever =0; lever>20; lever++) {

   Newleverstate =  (digitalRead(lever) 

    if Newleverstate == LOW {

        do something

 

 

Actually I would probably setup  an array of sturctures  for each lever  containing the assigned pin and current pin state and changed    then I could scan through the pins updating the current pin state and marking any that have changed and then process them because more than one lever could have been changed  you need the new states to check locking etc.

 

I can give you more help if you need 

 

Don

  • Like 1
Link to post
Share on other sites

  • RMweb Premium

My preferred style would be something like the example below.  Notice the termination clause in the "for" statement should use a less than (<) if you are running forward through the array.

 


#define NUMBER_OF_SWITCHES  4
int switches[NUMBER_OF_SWITCHES] = {3, 7,10, 25};

void setup() {

  for (int i = 0; i < NUMBER_OF_SWITCHES; i++) {
    pinMode(switches[i], INPUT_PULLUP);
  }
  
}

void loop() {

}

 

If you have a lot of switches to check you may run into performance problems if you use the simple digitalRead function as each one uses around 1000 processor instructions but I would recommend you go with the simple approach at least to start with.

 

Cheers

Dave

 

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

  • RMweb Gold

I left my last post sort of hanging here is what I was talking abou.

 

Firstly if you scan the levers from say 1 to 20 you cannot act on them until all have been scanned. For example lever 3 may be locked by lever 7 if you scan and reach 3 and find it operated the immediate response may be  invalid as lever 7 is pulled (rversed in signallling terms). However if you continue the scan and then find that lever 7 has been restored are you going to go back and check whether 3 was pulled or not because if it was it could now be valid.

 

So here is my approach

 

fisrt define a struct for the leverstates and an array for the pin 

struct Leverstate {

    char leverstate;

    bool  leverchanged;

    }   Myleverstates[20];

int Leverpin[20] = {1.2.4.5, etc  for all 20}

char releasetable [20][20] {

 initialise table here

}

 

void setup()  {

For (int lever = 0, lever<20, lever++) {

     pinMode(Leverpin[lever], INPUT_PULLUP;

}

  ....more code....

}

 

void loop () {

......

void Leverscan();

void Leveractions();

....

}

 

 

void Leverscan() {

char newstate;

    for ( int lever = 0, lever<20, lever++) {

       if  (digitalRead(Leverpin[lever] == LOW) {

           newstate='R';

           }

       else {

           newstate='N';

          }

       if (newstate != Leverstate[lever].leverstate) {

           Leverstate[lever].leverchanged=true;

           Leverstate[lever].leverstate=newstate;

           }

        else {

            Leverstate[lever].leverchanged=false;

            }

        }

 

void Leveractions();

       for  (int lever=0, lever<20, lever++)  {

           if (Leverstate[lever].leverchanged)         // i.e changed is true 

            for (otherlever=0, otherlever<20, otherlever++) {

               switch releasetable[lever][otherlever]

               case '/'                                 //   no interlock

                 break;

               case 'N'                               //  locks if normal

                 if (Leverstate[otherlever].leverstate=='N') {

                     digitalWrite(warningpin, LOW);                 // turns on interlock warning led

                     If (Leverstate[lever].leverstate=='N') {

                           Leverstate[lever].leverstate='R';

                          }

                      else {

                          Leverstate[lever].leverstate='N';

                         }

                      leverstate[lever].leverchanged=false

                     }

 

Well that gives some idea not fully worked out yet but its time to sleep

 

do you like this idea

 

Don

    

 

 

 

 

 

 

 

 

 

 

 

Link to post
Share on other sites

  • RMweb Premium

Many thanks for the responses.

 

I'd managed to find something akin to Dave's (eldavo) suggestion via Google and have gone along that route.

 

I'd quite happily include some interlocking but my colleague is disinclined to go down that path so it is fairly basic/standard code.

 

The greater number of lines are in the void loop() where each lever movement is treated separately in a lengthy Switch/Case bit of coding partly because some signals are route selected to save levers, It is a pity that you seem to have to have a Case statement for each single value and can't group similar processing values together. That said, it may be many lines of code , but the single value coding should make it easier to understand in due course.

 

Thanks once again for the assistance.

Link to post
Share on other sites

  • RMweb Premium

A quickie question for those more au fait with Arduinos than myself

 

I connect a load (Say about 10Ma) between two digital pins

If one pin is high and one pin is low is it OK that one pin is sourcing current and the other is sinking the same current?

From the Arduino spec and my gut feeling it should be OK but I need to be certain.

 

It is for colour light signals using 2pin bi-color LEDs (Red - Green & Yellow -Green respectively) with a suitable series resistor, where the colour change is by reversal of voltage.

The two pins used for each LED would reverse High/Low to effect the colour change.

Edited by melmerby
Link to post
Share on other sites

  • RMweb Premium
47 minutes ago, Simond said:

Keith,

 

like you, I feel it should work.  I suspect it’s a case of suck it and see…

 

do let us know!

Simon

 

 

9 minutes ago, Donw said:

I would think it would work but if you are plannning to control a number of lights  it may count as 10mA for each pin .

 

Don

The Arduino is supposedly capable of sourcing & sinking 40mA per pin, although 20mA is recommended.

I should be OK with my bipolar LED at about 10mA

 

Off to try it now.

 

  • Like 1
Link to post
Share on other sites

  • RMweb Premium
17 hours ago, Ray H said:

...It is a pity that you seem to have to have a Case statement for each single value and can't group similar processing values together. That said, it may be many lines of code , but the single value coding should make it easier to understand in due course.

 

I'm not quite sure why you would need a switch statement to deal with each lever separately but I think you can do what you want without too much difficulty.  Something like...

 

  switch (lever) {
    case 1:
    case 3:
    case 6:
      do stuff for cases 1,3 and 6;
      break;
    case 2:
      do stuff for case 2;
      break;
    case 4:
    case 5:
      do stuff for cases 4 and 5;
      break;
  }

 

If you omit the "break" statement the execution drops through into the next case by default.

 

I would probably be looking at either a structure as suggested earlier or multiple arrays to define what is done for each lever switch so that way you can just use your "index" value from the "for" statement you use to walk through checking the switches to directly access the servo ports or whatever.

 

Cheers

Dave

  • Like 1
  • Agree 1
  • Informative/Useful 2
Link to post
Share on other sites

  • RMweb Gold
3 hours ago, eldavo said:

 

I'm not quite sure why you would need a switch statement to deal with each lever separately but I think you can do what you want without too much difficulty.  Something like...

 


  switch (lever) {
    case 1:
    case 3:
    case 6:
      do stuff for cases 1,3 and 6;
      break;
    case 2:
      do stuff for case 2;
      break;
    case 4:
    case 5:
      do stuff for cases 4 and 5;
      break;
  }

 

If you omit the "break" statement the execution drops through into the next case by default.

 

I would probably be looking at either a structure as suggested earlier or multiple arrays to define what is done for each lever switch so that way you can just use your "index" value from the "for" statement you use to walk through checking the switches to directly access the servo ports or whatever.

 

Cheers

Dave

 

Yes that would work .   You can also do this

 

Case 1:

do something for case 1

Case 2

 do something else  for both case 1 and for case 2

Break

 

so both things get done for case1 only the second for case 2

 

Don

Case 

 

 

  • Thanks 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...