Fun with Arduino - a Series of Introductory Videos

Post all your model railway electronic problems, solutions and discoverys here.
User avatar
dubdee1000
Posts: 901
Joined: Mon Mar 31, 2008 9:59 pm
Location: cwm brynbuga

Re: Fun with Arduino - a Series of Introductory Videos

Postby dubdee1000 » Sun Jan 20, 2019 3:59 pm

Thanks Rudy. Another enjoyable video tutorial.

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 08 User Interface Serial.print()

Postby RudyB » Thu Jan 24, 2019 3:38 pm

Our way of modifying the day / night cycle time seemed quite handy, but we do need to modify the code and upload it again every time we like to change the cycle time. Is there maybe an easier way?

Yes there is ... we can change the cycle time 'on the fly' via a User Interface. There are different solutions, with hardware otr with software. We're going to try them both. In this video we'll do the first preparations, writing text and numbers to the PC screen via Serial.print().

Link to Fun with Arduino 08 User Interface Serial.print()


Image
Always try to have fun - website/blog - youtube channel

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 09 Variables byte int long unsigned

Postby RudyB » Sun Jan 27, 2019 10:06 am

Before we continue to work on our User Interface, let's first take a moment to have a closer look at variables and data types. We are going to use ever more variables in the coming videos ... and ... we'll have a look at a pitfall concerning data types that prevented our code from previous video 8 to always work as intended.

Fun with Arduino 09 Variables byte int long unsigned


Image
Always try to have fun - website/blog - youtube channel

User avatar
PinkNosedPenguin
Posts: 1327
Joined: Tue Oct 01, 2013 10:23 pm
Location: Wiltshire

Re: Fun with Arduino - a Series of Introductory Videos

Postby PinkNosedPenguin » Sun Jan 27, 2019 10:28 am

Rudy, many thanks indeed for posting these videos :D
I intend to do something with an Arduino soon-ish, so will definitely go through them all properly when I get started 8)

User avatar
Roger (RJ)
Posts: 1377
Joined: Tue May 29, 2007 2:59 pm
Location: Nottingham, UK.

Re: Fun with Arduino - a Series of Introductory Videos

Postby Roger (RJ) » Sun Jan 27, 2019 3:33 pm

That table of variables may not make sense to a lot of people in the UK as it is using full stops, aka periods (.) where we would use commas (,) so -32.768 for instance, would be written as -32,768 or -32768 with no comma or full stop.

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 10 Show Cycle Status and Time with Serial.print

Postby RudyB » Thu Jan 31, 2019 2:14 pm

Now that we have our code working, by using the correct data type or by typecasting, we can continue the work on our User interface. The goal in this video is to display the status of our day / night cycle on screen ... is it switched on or off, is it day or night, and what is the currently used day / night time. All this will be dynamically updated.

Fun with Arduino 10 Show Cycle Status and Time with Serial.print


Image
Always try to have fun - website/blog - youtube channel

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 11 Keyboard Input via Serial Read and ParseInt

Postby RudyB » Sun Feb 03, 2019 8:46 am

The User Interface works, it shows the status if the day / night cycle on screen. We're now going to add the option to change the cycle time via the keyboard. The functions we are going to use are Serial.available(), which tells us that there is new input, and Serial.read() or Serial.parseInt() to read the characters that are typed.

Fun with Arduino 11 Keyboard Input via Serial Read and ParseInt


Image
Always try to have fun - website/blog - youtube channel

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 12 Analog Input, analogRead(), Change Range, map()

Postby RudyB » Thu Feb 07, 2019 10:40 am

Now that we can change the cycle time via the PC keyboard, let's have a look at a hardware oriented solution ... a rotating knob. We connect a potentiometer to an analog input and read the voltage with the analogRead() instruction. With the map() instruction we can convert the range from 0-1023 to the range that we like to use for our cycle time, like say 1-9 minutes with a 1 minute step size, or maybe 10-300 seconds, with a 10 second step size.

Fun with Arduino 12 Analog Input, analogRead(), Change Range, map()


Image
Always try to have fun - website/blog - youtube channel

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 13 Timer with millis(), no delay(), Multitasking

Postby RudyB » Sun Feb 10, 2019 9:36 am

The delay() statement that we used so far for our timing stalls the Arduino. This leads to a complete lack of feedback when we change the cycle time while the cycle is running. Luckily there is a solution: we can use the Arduino internal clock, which counts milliseconds from the moment the Arduino is started. We can read the clock using the millis() statement and we can decide if it is time for action.

Fun with Arduino 13 Timer with millis(), no delay(), Multitasking


Image
Always try to have fun - website/blog - youtube channel

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 14 Day Night Cycle with millis(), no Delay, Direct Feedback

Postby RudyB » Thu Feb 14, 2019 9:49 am

Now that we know how to get rid of the delay(0 and use millis() in stead (video 13) we can finalize our Automatic Day Night Light Cycle unit to have direct on screen feedback of cycle time adjustment by the user and to have the cycle stop, and the lights turn off, immediately when the switch is set to ‘off’.

Our unit has quite nice specifications:
- Configurable timing, via keyboard or via analog input with on screen display
- An option to randomize the times to give it some ‘livelyness’
- On screen display of the on/off, day/night state and the cycles times


Fun with Arduino 14 Day Night Cycle with millis(), no Delay, Direct Feedback


Image
Always try to have fun - website/blog - youtube channel

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 15 LED Dimmer, analogWrite(), Pulse Width Modulation

Postby RudyB » Sun Feb 17, 2019 9:56 am

We used analogRead() to read the voltage on our potentiometer. The Arduino also has the opposite instruction: analogWrite(). This name is somewhat misleading. Unlike with an analog input, where a 10 bit A/D converter is used, the Arduino does not have a D/A converter on board.

The analogWrite() function uses a technique called Pulse Width Modulation. A digital output switches between HIGH and LOW in a fast pace, whereby the HIGH percentage is proportional to the analog value we wish to send out. If a device that receives the signal is too slow to follow the switching frequency, the result is it 'sees' the average of the on/off times. This also holds for light ... even though LEDs are fast enough to follow the switch frequency, our human eyes + brain are not and we see an average brightness.


Fun with Arduino 15 LED Dimmer, analogWrite(), Pulse Width Modulation


Image
Always try to have fun - website/blog - youtube channel

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 16 LED Dimming with Fade, analogWrite(), millis()

Postby RudyB » Thu Feb 21, 2019 11:09 am

Now that we know how to dim LEDs with analoWrite(), we can go a step further and change the dimming over time to create a gradual fade in or out. This is a nice effect for instance for LED strips mounted under kitchen cabinets, or for LED strip overhead lighting on a model railway layout to simulate a gradual change from night to day. And also for the red/green transition of railway signals along the track a fade gives just that little extra eye candy.

Fun with Arduino 16 LED Dimming with Fade, analogWrite(), millis()


Image
Always try to have fun - website/blog - youtube channel

User avatar
RudyB
Posts: 179
Joined: Sat Sep 20, 2014 9:29 am

Fun with Arduino 17 Railway Crossing, State Transition Diagram, switch()

Postby RudyB » Mon Feb 25, 2019 9:42 am

We're at the start of a new Arduino project: an automatic railway crossing.

The system comprises several parts: train detection (optical), blinking lights ('blink' with a twist), a moving beam (servo motor).

We'll look into a way of specifying these kinds of systems as well as a way to translate the specifications into code, with a stepwise approach that does not put too much strain on our grey cells.


Fun with Arduino 17 Railway Crossing, State Transition Diagram, switch()


Image
Always try to have fun - website/blog - youtube channel

User avatar
TimberSurf
Posts: 2450
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: Fun with Arduino - a Series of Introductory Videos

Postby TimberSurf » Tue Feb 26, 2019 7:54 am

Hi Rudy, a very nice series of tutorials! :D
You are now up to the level I am currently at. I have already written my level crossing program.
It is always interesting to see others code :?
It should be noted by all, that there are always at least half a dozen different ways to write the same program, much discussion would ensue if we debated who's is the best code! :lol: Some ways use a smaller amount of code, some use more sophisticated functions, others are simpler but more understandable to the beginners. :D None are wrong, if it works, then it is right! :P
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

User avatar
Flashbang
Posts: 3313
Joined: Thu Oct 18, 2007 1:07 pm
Location: SE United Kingdom
Contact:

Re: Fun with Arduino - a Series of Introductory Videos

Postby Flashbang » Tue Feb 26, 2019 5:05 pm

TS... The lowering sequence isn't quite correct, both AHB barrier booms lower together not one after the other.
Its only on full barriers that the approach two barriers lower then once proved down the trailing (exit) two lower.
Also, though often not modelled, when the yellow light extinguishes the red lights all come on for around 0.5 second followed by them flashing. Flashing is all left hand red lights on together then all right hand red lights on and so on. Called a Wig Wag sequence.
Now you need the warbling (Yodel alarm) pedestrian warning tone added :D
[Image << Click the Icon to go to my website
Broken? It was working correctly when I left it.


Return to “Electronics”

Who is online

Users browsing this forum: No registered users and 2 guests