What's on your workbench?

JR59 Jan 29, 2006

  1. Svein-Martin Holt

    Svein-Martin Holt TrainBoard Member

    345
    375
    25
    You could check out millis() if you need to do many things at the same time. Using millis will give you the option to start and stop different actions along the timeline.
    I used it in my sketch for the KFC model, where I programmed the flickering of the tube, running at the same time as other actions, like starting and stopping the motor. You need to program a STATE Machine, where the code is in different states. Check out the code on my homepage: KFC store 3D print - PlateLayer by Svein-Martin Holt
    From the reference guide: millis() - Arduino Reference
    A very good example here on the State Machine: Overview | Multi-tasking the Arduino - Part 1 | Adafruit Learning System
     
  2. rray

    rray Staff Member

    8,277
    9,277
    133


    I tried learning millis myself and it's somewhat difficult to get it correct, and am having some success. My coding skills are abysmal at best, but with the help of the AI ChatGPT I got this:

    can you write arduino sketch to randomly turn 18 leds on and off between 5 and 25 seconds each with no more than 8 leds on at a time using non blocking millis

    Sure! Here's an Arduino sketch that randomly turns on and off 18 LEDs, with no more than 8 LEDs on at a time, using non-blocking millis() function:

    scss
    const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3, A4, A5}; // Define an array of LED pins

    int numActiveLeds = 0; // Initialize the number of active LEDs to 0
    unsigned long previousMillis = 0; // Initialize the previousMillis variable
    const long intervalMin = 5000; // Minimum interval time in milliseconds
    const long intervalMax = 25000; // Maximum interval time in milliseconds

    void setup() {
    for (int i = 0; i < 18; i++) {
    pinMode(ledPins, OUTPUT); // Set all the LED pins as output
    }
    randomSeed(analogRead(0)); // Initialize the random number generator
    }

    void loop() {
    unsigned long currentMillis = millis(); // Get the current time in milliseconds

    if (currentMillis - previousMillis >= intervalMin && numActiveLeds < 8) {
    int ledIndex = random(0, 18); // Choose a random LED index
    digitalWrite(ledPins[ledIndex], HIGH); // Turn on the chosen LED
    numActiveLeds++; // Increment the number of active LEDs
    previousMillis = currentMillis; // Update the previousMillis variable
    }

    if (numActiveLeds > 0 && currentMillis - previousMillis >= intervalMax) {
    int ledIndex = random(0, 18); // Choose a random LED index
    digitalWrite(ledPins[ledIndex], LOW); // Turn off the chosen LED
    numActiveLeds--; // Decrement the number of active LEDs
    previousMillis = currentMillis; // Update the previousMillis variable
    }
    }

    In this sketch, we first define an array ledPins that contains the pin numbers of the 18 LEDs. We also initialize two variables: numActiveLeds to keep track of the number of LEDs that are currently turned on, and previousMillis to keep track of the last time an LED was turned on or off.

    In the setup() function, we set all the LED pins as output and initialize the random number generator using the analogRead(0) function.

    In the loop() function, we use the millis() function to check the time and turn on or off LEDs accordingly. If the current time minus the previous time is greater than or equal to the minimum interval time, and the number of active LEDs is less than 8, we choose a random LED to turn on, turn it on, and increment the numActiveLeds variable. We also update the previousMillis variable to the current time.

    If the number of active LEDs is greater than 0 and the current time minus the previous time is greater than or equal to the maximum interval time, we choose a random LED to turn off, turn it off, and decrement the numActiveLeds variable. We also update the previousMillis variable to the current time.

    This way, the sketch randomly turns on and off 18 LEDs, with no more than 8 LEDs on at a time, using non-blocking millis() function.




    So I modified the code the AI gave me to this which includes the stepper code, and so far so good with this code, but I want to play with it some more:


    #include <Stepper.h>

    const int stepsPerRevolution = 20; // change this to fit the number of steps per revolution for your motor

    const int ledPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 16, 17, 18, 19}; // Define an array of LED pins

    // initialize the stepper library and connect steppers on digital pins 25, 24, 23, 22:
    Stepper myStepper(stepsPerRevolution, 25, 24, 23, 22);

    int numActiveLeds = 0; // Initialize the number of active LEDs to 0
    unsigned long previousMillis = 0; // Initialize the previousMillis variable
    const long intervalMin = 5000; // Minimum interval time in milliseconds
    const long intervalMax = 25000; // Maximum interval time in milliseconds

    void setup() {

    // set the speed at 80 rpm:
    myStepper.setSpeed(80);
    // initialize the serial port:
    Serial.begin(9600);

    for (int i = 0; i < 18; i++) {
    pinMode(ledPins, OUTPUT); // Set all the LED pins as output
    }
    randomSeed(analogRead(0)); // Initialize the random number generator
    }
    void loop() {


    // step one one direction:
    Serial.println("clockwise");
    myStepper.step(stepsPerRevolution);

    unsigned long currentMillis = millis(); // Get the current time in milliseconds
    if (currentMillis - previousMillis >= intervalMin && numActiveLeds < 8) {
    int ledIndex = random(0, 18); // Choose a random LED index
    digitalWrite(ledPins[ledIndex], HIGH); // Turn on the chosen LED
    numActiveLeds++; // Increment the number of active LEDs
    previousMillis = currentMillis; // Update the previousMillis variable
    }
    if (numActiveLeds > 0 && currentMillis - previousMillis >= intervalMax) {
    int ledIndex = random(0, 18); // Choose a random LED index
    digitalWrite(ledPins[ledIndex], LOW); // Turn off the chosen LED
    numActiveLeds--; // Decrement the number of active LEDs
    previousMillis = currentMillis; // Update the previousMillis variable
    }
    }
     
  3. Svein-Martin Holt

    Svein-Martin Holt TrainBoard Member

    345
    375
    25
    The AI ChatGPT is great. But the teachers don't like it. :LOL:
     
  4. rray

    rray Staff Member

    8,277
    9,277
    133
    Actually, I have learned quite a bit because of the ChatGPT. It fails at the code a lot, and when I enter the failed code in Arduino, it give me errors that I have sometimes been able to correct. It actually forces me to do a bit of debugging, and that's where a lot of the help comes from.

    I posed the same question 6 times, and received 6 different answers, and each time something different happened. So I started re-structuring my query and getting different answers, so I was exposed to several different ways to get similar results. I hope ChatGPT stays free.
     
    drken, MMR283 and Svein-Martin Holt like this.
  5. rray

    rray Staff Member

    8,277
    9,277
    133
    Continuing with the wiring on the UP Hotel. I have 2 motors with fan blades and 16 of 27 LED's wired in so far. There is lots of wiring involved in this project: 06 Continuing Wiring.jpg
     
  6. bostonjim

    bostonjim TrainBoard Member

    846
    1,105
    27
    I am really jealous of your wiring skills and knowledge. Amazing work. Jim
     
    CNE1899, rray, MMR283 and 1 other person like this.
  7. drken

    drken TrainBoard Member

    343
    193
    18
    rray,
    The arduino website has this example built into the IDE called "Blink Without Delay", which shows how to use the millis() function to make LEDs blink in patterns without stopping the code using delay(). Theoretically, you should be able to use this to make as many lights as you need blink in any pattern independently of each other, just so long as each of them gets their own variables for the interval time and the "previousMillis"variable they use to hold the last time the LED blinked (or whatever you have it do).
     
    rray likes this.
  8. rray

    rray Staff Member

    8,277
    9,277
    133
    I made a quick video showing the U.P. Hotel Lighting and Ceiling Fans, and attached the code used (I used the millis command so the stepper motors would run continuously without stopping due to a delay in the code.




    Sketch:
    ___________________________________________________________________________________________

    #include <Stepper.h>
    const int stepsPerRevolution = 20; // change this to fit the number of steps per revolution for your motor
    const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}; // Define an array of LED pins
    // initialize the stepper library and connect steppers on digital pins 25, 24, 23, 22:
    Stepper myStepper(stepsPerRevolution, 25, 24, 23, 22);
    int numActiveLeds = 0; // Initialize the number of active LEDs to 0
    unsigned long previousMillis = 0; // Initialize the previousMillis variable
    const long intervalMin = 5000; // Minimum interval time in milliseconds
    const long intervalMax = 25000; // Maximum interval time in milliseconds
    void setup() {
    // set the speed at 80 rpm:
    myStepper.setSpeed(80);
    // initialize the serial port:
    Serial.begin(9600);
    pinMode(0, OUTPUT);

    for (int i = 0; i < 18; i++) {
    pinMode(ledPins, OUTPUT); // Set all the LED pins as output
    }
    randomSeed(analogRead(0)); // Initialize the random number generator
    }
    void loop() {
    // step one one direction:
    Serial.println("clockwise");
    myStepper.step(stepsPerRevolution);
    digitalWrite(0, HIGH);

    unsigned long currentMillis = millis(); // Get the current time in milliseconds
    if (currentMillis - previousMillis >= intervalMin && numActiveLeds < 8) {
    int ledIndex = random(0, 18); // Choose a random LED index
    digitalWrite(ledPins[ledIndex], HIGH); // Turn on the chosen LED
    numActiveLeds++; // Increment the number of active LEDs
    previousMillis = currentMillis; // Update the previousMillis variable
    }
    if (numActiveLeds > 0 && currentMillis - previousMillis >= intervalMax) {
    int ledIndex = random(0, 18); // Choose a random LED index
    digitalWrite(ledPins[ledIndex], LOW); // Turn off the chosen LED
    numActiveLeds--; // Decrement the number of active LEDs
    previousMillis = currentMillis; // Update the previousMillis variable
    }
    }


    _______________________________________________________________________________________________
     
    HoboTim, Zscaleplanet, drken and 5 others like this.
  9. MMR283

    MMR283 TrainBoard Member

    145
    669
    11
    AA6FAED7-58A1-4E21-9564-235EAE8B76E7.jpeg CEB1D7F4-0624-4D17-B021-539813E73CE3.jpeg Well Robert passed the baton to me to mask and paint the U.P. Hotel. I first painted the different levels with white primer. Had to make three separate paint sessions, due to the primer comes out light in color against a grey resin. Once all dried, I started to mask the windows with precut masking thanks to Robert. Made this a lot faster to get the windows masked. I used Tamiya masking tape for all the bay windows! Next step is to spray a sealer on all the walls. The purpose of sealing is to make sure the paint won’t bleed through all the masking.
     
  10. CNE1899

    CNE1899 TrainBoard Member

    1,097
    1,872
    36
    Clyde,
    Looks tedious! But Will be worth it I'm sure.

    Scott
     
    MMR283 likes this.
  11. MMR283

    MMR283 TrainBoard Member

    145
    669
    11
    Forgot to mention when masking, not only do you want do mask the front walls, you need to mask the inside walls as well. Reason being is you don’t want to get any overspray unto the window Mullins!
     
    Zscaleplanet, Kurt Moose and CNE1899 like this.
  12. drken

    drken TrainBoard Member

    343
    193
    18
    More progress. After a few different iterations, the platform for the switches/signals is in its (hopefully) final version and I've connected signals to an SE74 for 2 out of the 3 switches here. Four down, 3 to go. They're currently controlled by the switch address they've been assigned, with two addresses for each 4 aspect signal head (two states per address). Eventually, I'll set up JMRI to assign which switch and detection section they're protecting. I've also learned I hate working with ribbon cable. Very uncooperative and the connectors need far more force to attach than I should have to apply to electronics.

    Front Signals.jpg
    There's no option to lower the brightness on the signals, they're a little brighter than I'd like.
    Back Signals.jpg
    I need to remove the foam under the mount so everything fits. As it turns out, I'm collecting a nice collection of foam bits due in part to my inability to cut out the right piece of foam on first attempt, leading to much bigger holes than I need. The foam bits should come in useful, I hope.

    Here's a close up of how I'm mounting the signals to the platform.
    Signal Mount.jpg I'm going to leave the picture sideways because it doesn't really matter. The signals and mounts are designed to be mounted under the table, but that's not an option here. So I designed a mount that fits under the platform and holds the mounting board and signal in place. The leg of the mount is press-in-place to the platform and mounted to the board with a M3 nut/bolt combo. I'm very glad I'm going to hide all this behind foam when it's done.
     
    rray, HoboTim, Kurt Moose and 4 others like this.
  13. CNE1899

    CNE1899 TrainBoard Member

    1,097
    1,872
    36
    drken,
    Nice work so far!
    Can you print out a colored acetate film to cover over the lights?
    A piece to cover just the light modules can be printed to look like the actual light panel.
    They can be printed in the different colors and may diffuse the light more.
    Just a thought.

    Scott
     
  14. mdvholland

    mdvholland TrainBoard Member

    521
    860
    30
    Or maybe a drop of transparant paint..?

    Just wondering, Ken (?), aren't those signs very vulnerable when you're working on the rest of the layout..? Or are they detachable?

    Matt
     
  15. Zscaleplanet

    Zscaleplanet TrainBoard Supporter

    649
    1,570
    36
    Just catching up with this posting and watched the video -- Rob, the hotel, specifically the lighting aspects are simply incredible....WOW.
     
    rray likes this.
  16. drken

    drken TrainBoard Member

    343
    193
    18
    A bit of paint might work, but I'd rather not start experimenting with non-reversible things until I know which ones are spares and which ones I actually need. As for the signals themselves, they are detachable. You might not be able to see it, but the holes in the platform that they stick up out of are designed so I can fit the whole signal through from the top and into the connector on the mounting board. It makes them much easier to mount (as well as unmount).
     
    mdvholland likes this.
  17. Kez

    Kez TrainBoard Member

    755
    665
    32
    If I'm going to build more Z houses, I'll need to be able to pick up the Z garbage, eh?

    ;)

    A bit of painting done yesterday and today...

    20230226_191321.jpg 20230226_192506.jpg
     
    MMR283, Zscaleplanet, CNE1899 and 7 others like this.
  18. MMR283

    MMR283 TrainBoard Member

    145
    669
    11
    F00B8469-A23F-48D1-8BB1-7A3524E88DC1.jpeg FC97EAF5-8D57-46A6-87A2-6C51C309FAA5.jpeg Finally have finished with painting the U.P. hotel. It was a big help having Robert make masking for windows. Still had to do a lot of touch up with the paint to get the windows as perfect as I can. I think the results came out great. Next is to get all the windows into place and blinds, then work on adding decal signs for the building. Have ordered furniture from Shapeways for the first level. The lobby, Cigarette/Beer store, and cafe will have interiors.
     
  19. MMR283

    MMR283 TrainBoard Member

    145
    669
    11
    EDCE9434-EEA1-4D04-BF92-AACD8717CB24.jpeg 9F2FE221-4543-4084-BEF1-4C8B79DC0E1A.jpeg Here’s a picture’s of the correct levels! Had the floors in the wrong order!
     
  20. CNE1899

    CNE1899 TrainBoard Member

    1,097
    1,872
    36
    Clyde,
    Nice work! I like the colors and the silver cladding.

    Scott
     
    MMR283 likes this.

Share This Page