Arduino based task scheduler/threading functions

ardIf you’ve used the Arduino before then you’ll know that any code you write in the loop() function is executed on every cycle. But what happens when you have code that needs to run at certain intervals? A delay() will just cause that iteration of loop() to hang and pause.

Arduino released a tutorial BlinkWithoutDelay which introduced the concept of checking the “uptime since last boot” and comparing that to make a basic function scheduler.

The code below basically does the same job, except it’s cleaner and easier to manage. This is by no means original, there have been hundreds of productions of this code, this is just one way to go about it.

The naming structure is shown below, instances of the Timer are prefixed with t_FunctionName to indicate this is a timer.

Instead of checking a single variable for the current milliseconds since boot, this will create a new instance of a structure that can be used to store timings for functions. This way you can easily change when to run a certain portion of code without digging through the sketch or remembering which variables contain the correct values.

The loop() portion of the sketch should be kept clean and only be checking the cycles of the functions from the structure. There should be no need to place any program code inside of the loop() function except for these checks.

This should provide the following benefits:

  1. Enable you to run code every [x] milliseconds or [x] seconds.
  2. Structure the program so that you know when code is running and being able to easily change this.
  3. Have a faster running program. e.g. if you know a portion of code is slower and delaying the loop() you can have this run on a larger interval so it isn’t executed as often.

This code is compatible with all versions of the Arduino and most likely other boards where the language is also based on C.

Quick build – A transistor NOT gate

NOT-GATE

I had a few minutes to spare the other night so I decided to build myself a quick transistor NOT gate. I will be adding other gates to the breadboard and posting their functions on the site but this is something to keep you occupied for now.

First of all, let’s have a look at the schematic of the circuit itself.

not gateWith only 3 resistors and one transistor, it’s easy to build this small circuit into other circuits.

The NOT gate is often called an inverter. Basically, you give it either a 1 or a 0 (on or off) and the output will be the opposite of the input. e.g. if you connect and switch to the input and an LED to the output, when you turn the switch ON, the LED will be OFF. If you turn the switch OFF, the LED will be ON.

Here is a quick picture of the of the circuit I built on a breadboard. (Click the image to get a larger version)

breadnot

Excuse the untidy wiring, I never have time to trim cables and make things look neat, especially on a breadboard. At the top left corner of the board I’ve got a 9v battery connected to a 5v regulator. That then feeds 5v into a 555 timer. The timer is used purely to supply a pulsing on-off signal. I could have easily used a manual switch but I had a 555 chip nearby so I thought I might as well use it. (Same for the voltage regulator). The transistor and resistors lower down on the board are the NOT gate.

When the 555 timer sends an ON signal, the red LED lights up. In the image, you can see that the green LED is currently lit. This is because the red led is NOT on.

When the red LED is lit, the green LED is NOT on. Simple.

Here is a truth table to show a NOT gate.

not-truthBasically, as a recap, if you give the NOT gate a 1 (on) the output will be 0 (off) and vise versa.

Found – Xilinx CPLD chip

I’ve been looking through a few old boards in the parts bin and came across this….

cpldchip

 

 

 

 

 

 

 

It was sat in this board…

1

It’s some sort of ISA CD-ROM card.

I’m now seriously considering building my own board with this chip and building a parallel cable to program it with.

You can never have too many development boards….right?

Fixing erratic LDR values

ldr

After working with a few different sensors hooked up the Arduino, I was experiencing some strange issues where as the value of the LDR would constantly jump up and down within a specific range. I am not sure if this is the quality of the sensor, but it was doing it for all of the LDRs that I have.

I decided that it needed some artificial smoothing in the code to get a useful value. See sample code below for smoothing out LDR values. We need to change the value from an erratic analog value to a digital percentage as an integer.

As you can see, the function takes two parameters samples and sampleInterval The samples is how many times it should query the LDR for the value and the second parameter is how long it should wait before each query.

Depending on the size of the LDR and quality, the min and max smooth value may need to be adjusted (550 and 1023) depending on the range you get back from your LDR.

The function will return an Integer between 0 and 100 depending on the amount of light.

Logic Gate Simulator

One piece of software that I’ve found invaluable is Logic Gate Simulator.

It can be found here. It was written by a guy named Steve Kollmansberger.

There are probably plenty of other gate simulator programs but this is the first one I found and I love it.

It lets you plonk your gates onto the drawing board and then tie the inputs and outputs together. I highly recommend the program and I suggest you give it a try.

gatesim_ss

 

My journey into logic

I haven’t looked at logic gates since I went to college 15 years ago. I recently decided that it’s time to revisit the theory and get a better understanding of electronics using logic circuits.

After viewing a few youtube videos and reading some old college textbooks, I’m in a much better position to start some physical experiments. I have looked in my parts collection and while there are plenty of TTL chips, none of them are the simple AND, OR and XOR type.

Onto the internet I went, looking for bargain chips to get playing with……. unfortunately, I just don’t have the spare cash right now to go throwing at random chips.

Enter this ………..

max2board

 

This is a cheap CPLD board based around the Altera Max II chip.

I know I could have spent a bit more and gone for a larger chip or even an FPGA but I’ve decided that this  will suffice for now.

My aim is to design the logic circuits using the schematic view of the Quartus 2 software. I know everyone these days uses VHDL or Verilog but I’m not all that bright so trying to learn a new language is just going to send me over the top.

I’ve now got to wait another 20 or so days for it to arrive from China but I can’t wait to get started with my adventures into logic circuits.

Bring it on!!!

Rant – Dropbox icon change

I haven’t used dropbox for ages but decided today that I need to start using it again. I’ve installed it onto my windows PC but there’s one thing that’s really bothering me…. the new icon.

dropboxicon

I hate it :(

It looks all IOS-ey and I don’t want my desktop icons to look like they belong on IOS.

Anyway, rant over, it’s not like it’s going to stop me using it, I just hope other software developers don’t start changing their icons to fit the IOS way

Page 7 of 8« First...45678