JDoggyDrums (Customer) asked a question.

Most simple approach to convert UNO code to the P1am-100?

So, after conceptualizing a machine and writing code for the machine in Arduino IDE for the UNO, what would be the best way to convert this over to use on the P1AM-100? I'm sure there will be many changes. Is there a good resource for the C++ commands and code specifics to the p1AM-100? As of now, all of the inputs/outputs and an HMI are running off of one UNO successfully, but of course I will have a few I/O cards and a GPIO card for the P1AM. It was a challenge to learn to code in C++ for the first time from scratch but there are a lot of resources specific to the arduino platform, so any help or info for converting to the P1am are much appreciated. Thanks!!


  • FACTS_AdamC (AutomationDirect)

    Without seeing your code, I can't give the most thorough advice. In general, since they are both Arduino based boards, the code should work identically between the two unless you're doing something AVR specific. Aside from that, the only changes you'd likely need are changing your IO read/write commands from the Arduino instructions to P1AM. Using the channelLabels feature in the P1AM library will make this easier. A couple snippets below.

     

    Arduino code(from Button example)

    1. const int buttonPin = 2; // the number of the pushbutton pin
    2. const int ledPin = 13; // the number of the LED pin
    3.  
    4. // variables will change:
    5. int buttonState = 0; // variable for reading the pushbutton status
    6.  
    7. void setup() {
    8. // initialize the LED pin as an output:
    9. pinMode(ledPin, OUTPUT);
    10. // initialize the pushbutton pin as an input:
    11. pinMode(buttonPin, INPUT);
    12. }
    13.  
    14. void loop() {
    15. // read the state of the pushbutton value:
    16. buttonState = digitalRead(buttonPin);
    17.  
    18. // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    19. if (buttonState == HIGH) {
    20. // turn LED on:
    21. digitalWrite(ledPin, HIGH);
    22. } else {
    23. // turn LED off:
    24. digitalWrite(ledPin, LOW);
    25. }
    26. }

    Same code but using P1 IO functions and channelLabels

    1. #include "P1AM.h"
    2.  
    3. channelLabel buttonPin = {1, 3}; // Discrete Input on slot 1 channel 3
    4. channelLabel ledPin = {2, 1}; // Discrete Output on slot 2 channel 1
    5. // variables will change:
    6. int buttonState = 0; // variable for reading the pushbutton status
    7.  
    8. void setup() {
    9. while(!P1.init());
    10. }
    11.  
    12. void loop() {
    13. // read the state of the pushbutton value:
    14. buttonState = P1.readDiscrete(buttonPin);
    15.  
    16. // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    17. if (buttonState == HIGH) {
    18. // turn LED on:
    19. P1.writeDiscrete(HIGH, ledPin); // Argument order is opposite of digitalWrite
    20. } else {
    21. // turn LED off:
    22. P1.writeDiscrete(LOW, ledPin); // Argument order is opposite of digitalWrite
    23. }
    24. }

     

     

    Expand Post
    Selected as Best
  • FACTS_AdamC (AutomationDirect)

    Without seeing your code, I can't give the most thorough advice. In general, since they are both Arduino based boards, the code should work identically between the two unless you're doing something AVR specific. Aside from that, the only changes you'd likely need are changing your IO read/write commands from the Arduino instructions to P1AM. Using the channelLabels feature in the P1AM library will make this easier. A couple snippets below.

     

    Arduino code(from Button example)

    1. const int buttonPin = 2; // the number of the pushbutton pin
    2. const int ledPin = 13; // the number of the LED pin
    3.  
    4. // variables will change:
    5. int buttonState = 0; // variable for reading the pushbutton status
    6.  
    7. void setup() {
    8. // initialize the LED pin as an output:
    9. pinMode(ledPin, OUTPUT);
    10. // initialize the pushbutton pin as an input:
    11. pinMode(buttonPin, INPUT);
    12. }
    13.  
    14. void loop() {
    15. // read the state of the pushbutton value:
    16. buttonState = digitalRead(buttonPin);
    17.  
    18. // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    19. if (buttonState == HIGH) {
    20. // turn LED on:
    21. digitalWrite(ledPin, HIGH);
    22. } else {
    23. // turn LED off:
    24. digitalWrite(ledPin, LOW);
    25. }
    26. }

    Same code but using P1 IO functions and channelLabels

    1. #include "P1AM.h"
    2.  
    3. channelLabel buttonPin = {1, 3}; // Discrete Input on slot 1 channel 3
    4. channelLabel ledPin = {2, 1}; // Discrete Output on slot 2 channel 1
    5. // variables will change:
    6. int buttonState = 0; // variable for reading the pushbutton status
    7.  
    8. void setup() {
    9. while(!P1.init());
    10. }
    11.  
    12. void loop() {
    13. // read the state of the pushbutton value:
    14. buttonState = P1.readDiscrete(buttonPin);
    15.  
    16. // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    17. if (buttonState == HIGH) {
    18. // turn LED on:
    19. P1.writeDiscrete(HIGH, ledPin); // Argument order is opposite of digitalWrite
    20. } else {
    21. // turn LED off:
    22. P1.writeDiscrete(LOW, ledPin); // Argument order is opposite of digitalWrite
    23. }
    24. }

     

     

    Expand Post
    Selected as Best
    • JDoggyDrums (Customer)

      Thanks, that's exactly what I'm looking for. I'm going to start playing with it this week and see how it goes. Is there a reference online for other similar function differences like write discrete vs digital write? Or is it best to just search out some of the example code snippets?

      • FACTS_AdamC (AutomationDirect)

        We don't have anything really detailing Arduino vs P1AM calls since they are pretty straightforward i.e. writeDiscrete == digitalWrite, readAnalog == analogRead.

         

        If you having trouble finding a correlating function you are welcome to ask here or if it sensitive info PM me.

         

        You can find an API reference for the entire library here: https://facts-engineering.github.io/api_reference.html

        Expand Post