

- #Mcwhorter arduino while loop serial#
- #Mcwhorter arduino while loop software#
- #Mcwhorter arduino while loop code#
- #Mcwhorter arduino while loop download#
Looking at the code below, the Arduino code is on the left, whilst the python coding is on the right.
#Mcwhorter arduino while loop serial#
With this board, I wanted try and use the serial communication to read the Arduino, but I had also seen a good video of someone using a python programme to catch the serial command from the Uno which I found very interesting. So for awhile, I put aside my Fab Hello Board and utilised my Arduino Uno

After trying to use the Arduino serial.print("") command, I found that this was not recognised by the chip and after quite a bit of research I realised the chip didn't do serial communication like the Arduino Uno. I wanted to try and achieve something with the serial monitor. Now I had accomplished something with the LED and button. This will change next time the button is pressed. This is to make sure that there is no difference later (last != current) on when the code repeats. Finally, the most important part is to make sure the lastButton = currentButton.

it has been pressed), then change the led state

If it has, it will reread the button in 5 milliseconds and declare that to be the state of the button.A boolean debounce function (which calls the previous state of the button) checks to see whether the button has changed state.These don't need to be set at the start because they will be varied throughout the code excepet LedOn which makes sure the led stays off at the start. One for last button state, current button state and ledOn. 5 milliseconds is plenty of time for you to remove your finger from the button (you can't press it faster). This example is based on Jeremy Blum's 'debounce' LED function which compares, the button state again after 5 milliseconds to see if it was really pressed (as compared to noise on the button). This can only read after you have taken your finger off. This is because it activates a millis() timng which compares the press to a set time. The reason I chose this 'debounce' program was because the Arduino 'debounce' program does not change the state of the button until you have taken your finger off the button, so it is too slow. Getting the LED to remain on when pressed requires a little bit more work than you would think. Pressing a button and the LED lights up is fun, but most of the time you want it to remain in that state. This is useful when you have a long and complex programme, we can then simplify it by calling smaller parts to runĪrduino Example 3 & 4 - Button for LED On/Off If not (else) we can 'call' a subroutine which runs a seperate routine.We then use the if else rule, so if the button is pressed (when the button is pulled to ground), we can then instruct the program to turn the LED on In the loop, we ask the chip to read the state of the button and save that to the variable buttonState.Note the button is set to (digitalWrite - High) to activate the internal resistor in the chip. The button 'int' is added, along with the button State which is a boolean state (HIGH/LOW) indicating the state of the button.Here are the interesting points of the code: With this example, the LED will flash when the button is not pressed, but will stay on when pressed. This is very easy to use and we can change the interval in the global variables at the beginning of the code. In this example, we use the millis() to count to an interval, and if we go above that we can run the blink code. This programme is based on the Arduino example but I have made some slight changes to highlight certain parts of the code. In this example, I have brought the button into the equation.
#Mcwhorter arduino while loop software#
Once in the Arduino software I then had to select certain options (see below)Īrduino Example 2 - Blink LED with Millis()
#Mcwhorter arduino while loop download#
Using the Arduino software is very easy, once you have the right hardware on your computer (you need to download the attiny hardware file). Other useful information is the basic details of the chip including internal oscillation speed, memory storage and reset pin.įor a more advanced user, understanding how the chip can be programmed in C is very useful and I look forward to researching this further as it gives real life examples of implementing the code (in C). It also showed me which pins were capable of Pulse Width Modulation which proved to be very useful when designing the board (there is no point putting a button the PWM pin, instead the LED can be used). I must admit that 95% of the datasheet is hard to understand but it does give you some useful information such as understanding the pinOuts for the chip. Before we got started with programming our HelloWorld boards, James gave us a talk on the values of looking at the datasheet for valuable information.
