Task 21: Output random numbers between 0 and 100 to the terminal:
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println(random(0,101));
delay(200);
}
Task 22: dice throwing- output a number between 1 and 6 every second:
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println(random(1,7));
delay(1000);
}
Task 23: Display number of 6is thrown:
int sixes = 0;
int num = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
num = random(1,7);
if (num == 6){
sixes++;
}
Serial.println(num);
Serial.println();
Serial.println("Sixes throwen");
Serial.println(sixes);
Serial.println();
delay(1000);
}
Task 24 same as 23 but faster and stops at 25 sixes.
int sixes = 0;
int num = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
while (sixes < 25){
num = random(1,7);
if (num == 6){
sixes++;
}
Serial.println(num);
Serial.println();
Serial.println("Sixes throwen");
Serial.println(sixes);
Serial.println();
delay(100);
}
}
Task 25: how many throws to get to 100 sixes:
int sixes = 0;
int num = 0;
int throws = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
while (sixes < 100){
num = random(1,7);
if (num == 6){
sixes++;
}
throws++;
}
Serial.println("Throws to 100 sixes");
Serial.println(throws);
Serial.println();
delay(10000);
throws = 0;
sixes = 0;
}
Task 26: push button program:
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Task 27 - reverse program operation:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED off:
digitalWrite(ledPin, LOW); //Program altered so led is off when button pressed
}
else {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
}
Task 28 - Output state of button and led to screen.
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600); //Serial output setup
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED off:
digitalWrite(ledPin, LOW); //Program altered so led is off when button pressed
Serial.Println("Button pressed, LED off") // write output
}
else {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
}
Task 29 - De bouncing:
As switch contacts are made out of a metal they have the tenancy to bounce or chatter a number of times when the switch is released. Whilst this happens to quickly to observe and the effects often go unnoticed when it comes to sensitive logic circuits it can be interpreted as the switch being pressed and released a number of times. The effects of this can be countered using a simple delay or a Latch circuit depending on they type of switch in use.
Task 30 - Buzzer and light circuit:
Task 31 -
Task 32 -Run String conversion program
Had to set serial monitor to "NewLine" in order to get it to work
Task 33 - What happens under error conditions?
No number:
Result:String:
Value:0
Huge Number:
12345678900987654321 Result:
Value:-597200719
String: 12345678900987654321
String:
abcdResult:
String:
Value:0
String and number:
abcd1234Result:
Value:1234
String: 1234
Task 34 - Red Green program:
//This program reads a number from the serial terminal and illuminates a red led if its //larger than 50 and a green led if its less than 50:
int red = 2;
int green = 3;
String inString = ""; // string to hold input
void setup() {
// Initialize serial communications:
Serial.begin(9600);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
}
void loop() {
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string,
// then the string's value:
if (inChar == '\n') {
if (inString.toInt() > 50)
{
digitalWrite(red,HIGH);
digitalWrite(green,LOW);
}
if (inString.toInt() < 50)
{
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
}
inString = "";
}
}
}
Task 35 - Random led flasher
Generated a number between 0 and 100 if the number is less than 50 the green led lights if the number is larger than 50 the red led lights:
int red = 2;
int green = 3;
long rndNum;
void setup() {
// Initialize serial communications:
Serial.begin(9600);
int green = 3;
long rndNum;
void setup() {
// Initialize serial communications:
Serial.begin(9600);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
}
void loop() {
rndNum = random(101);
if (rndNum > 50)
{
digitalWrite(red,HIGH);
digitalWrite(green,LOW);
}
if (rndNum < 50)
{
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
}
delay(1000);
Task 36 - Simple guessing game:
Generates a number between 0 and 100 and accepts a users guess, if the users guess is larger than the number the red led is illuminated otherwise the green led is illuminated, if the number is correctly guessed then both leds are illuminated:
int red = 2;
int green = 3;
String inString = ""; // string to hold input
long rndNum;
long guess;
void setup() {
// Initialize serial communications:
Serial.begin(9600);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
}
void loop() {
// Read serial input:
rndNum = random(101);
while (guess != rndNum)
{
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
inString += (char)inChar;
}
if (inChar == '\n') {
guess = inString.toInt();
if (guess > rndNum)
{
digitalWrite(red,HIGH);
digitalWrite(green,LOW);
}
if (guess < rndNum)
{
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
}
if (guess == rndNum)
{
digitalWrite(red,HIGH);
digitalWrite(green,HIGH);
}
inString = "";
}
}
}
}
int green = 3;
String inString = ""; // string to hold input
long rndNum;
long guess;
void setup() {
// Initialize serial communications:
Serial.begin(9600);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
}
void loop() {
// Read serial input:
rndNum = random(101);
while (guess != rndNum)
{
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
inString += (char)inChar;
}
if (inChar == '\n') {
guess = inString.toInt();
if (guess > rndNum)
{
digitalWrite(red,HIGH);
digitalWrite(green,LOW);
}
if (guess < rndNum)
{
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
}
if (guess == rndNum)
{
digitalWrite(red,HIGH);
digitalWrite(green,HIGH);
}
inString = "";
}
}
}
}
Task 37 More complex guessing game:
Allows the user to guess the number as before, gives feedback as to how many guesses the user has made, when the user gets the number correct they are congratulated and their total number of guesses is displayed along with the mystery number:
int red = 2;
int green = 3;
String inString = ""; // string to hold input
long rndNum;
long guess;
int noGuesses = 0;
void setup() {
// Initialize serial communications:
Serial.begin(9600);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
}
void loop() {
// Read serial input:
noGuesses = 0;
rndNum = random(101);
while (guess != rndNum)
{
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
inString += (char)inChar;
}
if (inChar == '\n') {
guess = inString.toInt();
noGuesses++;
if (guess > rndNum)
{
digitalWrite(red,HIGH);
digitalWrite(green,LOW);
}
if (guess < rndNum)
{
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
}
if (guess == rndNum) // If guess correct alert user and return
{
digitalWrite(red,HIGH);
digitalWrite(green,HIGH);
Serial.println();
Serial.print ("Well Done, The number was ");
Serial.print (rndNum);
Serial.println();
Serial.print ("You took ");
Serial.print (noGuesses);
Serial.print (" guesses to get the number");
Serial.println();
inString = "";
return;
}
int green = 3;
String inString = ""; // string to hold input
long rndNum;
long guess;
int noGuesses = 0;
void setup() {
// Initialize serial communications:
Serial.begin(9600);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
}
void loop() {
// Read serial input:
noGuesses = 0;
rndNum = random(101);
while (guess != rndNum)
{
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
inString += (char)inChar;
}
if (inChar == '\n') {
guess = inString.toInt();
noGuesses++;
if (guess > rndNum)
{
digitalWrite(red,HIGH);
digitalWrite(green,LOW);
}
if (guess < rndNum)
{
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
}
if (guess == rndNum) // If guess correct alert user and return
{
digitalWrite(red,HIGH);
digitalWrite(green,HIGH);
Serial.println();
Serial.print ("Well Done, The number was ");
Serial.print (rndNum);
Serial.println();
Serial.print ("You took ");
Serial.print (noGuesses);
Serial.print (" guesses to get the number");
Serial.println();
inString = "";
return;
}
Serial.print("you guessed: ");
Serial.print(guess);
Serial.println();
Serial.print("so far you have made ");
Serial.print(noGuesses);
Serial.print(" guesses");
Serial.println("");
inString = "";
}
}
}
}
Task 38:
Arduino memory types:
Flash memory :
where the arduino "sketch" is stored, there is 32kb available for the user to strore programs on the ATMega328.
SRAM:
Where variables are created and stored during run time, the ATMega328 had 2048 Bytes available for storage. This memory space is volatile and its contents are lost after power is removed.
EEPROM:
User available memory space where long term memory can be stored, this memory has a limited number or write cycles (approx 100,000) the ATMega328 has 1024 Bytes of EEPROM available.
Task 39:
| Processor | Flash Memory | RAM | EEPROM |
| AVR Tiny 4 | 500 Bytes | 3 Bytes | NONE |
| ATMega | 32K | 2048 Bytes | 1024 Bytes |
| ATmega2560 | 256K | 8Kb | 4Kb |
Task 40:
First 512 bytes of EEPROM Memory:Byte 1 = 1 - I have used this one before
Bytes 2 - 512 are equal to 255
Data Logger Task:
Using the Arduinos internal EEPROM as a data store I will log 48 hours worth of light readings, The MEGA328 has 1kb of space available which equates to a total of 1000 possible readings of a byte each. I will take a reading every 5 minutes as light levels don't change that dramatically. In 48 hours there are 2880 minutes which will give me a total of 576 samples after a period of 48 hours at a sampling rate of 5 minutes as discussed above.
The below code takes a light reading every 5 minutes and writes it to the Arduinos EEPROM. After 576 consecutive samples have been taken a red led is illuminated to alert me that 48 hours has elapsed:
Time in hours = (loggingPeriod * samplesTaken ) / 60
Time in hours = (5 * 576) / 60 = 48
#include <EEPROM.h>
int lightPin = 0; // Pin for light readings
int doneLed = 2; // Pin for Done led
int powerLed = 3; // Pin for power led
long interval = 300000; //every 5 mins
long previousMillis = 0;
int count = 0;
void setup(){
pinMode(powerLed,OUTPUT);
pinMode(lightPin,INPUT);
pinMode(doneLed,OUTPUT);
digitalWrite(powerLed,HIGH);
Serial.begin(9600);
}
void loop(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
int lightPin = 0; // Pin for light readings
int doneLed = 2; // Pin for Done led
int powerLed = 3; // Pin for power led
long interval = 300000; //every 5 mins
long previousMillis = 0;
int count = 0;
void setup(){
pinMode(powerLed,OUTPUT);
pinMode(lightPin,INPUT);
pinMode(doneLed,OUTPUT);
digitalWrite(powerLed,HIGH);
Serial.begin(9600);
}
void loop(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
writeValue(getLight());
Serial.println(getLight());
Serial.println(count);
}
}
int getLight(){
int light = analogRead(lightPin);
int lightScaled = map(light, 0, 1023, 0, 255);
return lightScaled;
}
void writeValue(int value){
if (count <=576){
EEPROM.write(count,value);
count++;
}else{
digitalWrite(doneLed,HIGH); //Complete
}
}
Serial.println(count);
}
}
int getLight(){
int light = analogRead(lightPin);
int lightScaled = map(light, 0, 1023, 0, 255);
return lightScaled;
}
void writeValue(int value){
if (count <=576){
EEPROM.write(count,value);
count++;
}else{
digitalWrite(doneLed,HIGH); //Complete
}
}
This code will return all the readings stored in the Arduinos EEPROM.
#include <EEPROM.h>
int count = 0;
void setup(){
Serial.begin(9600);
Serial.println("Light Reading");
}
void loop(){
while (count <= 576){
Serial.println(EEPROM.read(count));
count++ ;
}
Results:
After the logger was set up and left for 48 hours the data dump code was uploaded to recover the readings obtained over the 48 hours:
The logger was started at mid day on day one and left to run for 2 full days at which point the red "Done" light illuminated to alert me that 48 hours had elapsed.
The graph above shows graphically how the light levels varied over the period of 48 hours. From the above graph it appears that the light levels rise and fall very sharply however this graph is showing data from a period of 48 hours so it very condensed.
The second graph below shows a "Sunset" over a period of 2 hours:
In the first graph, the peaks observed during the dark periods are people turning lights on it the room with the logger, if this data is extrapolated out it can be determined exactly how long the light was on.
The graph above shows graphically how the light levels varied over the period of 48 hours. From the above graph it appears that the light levels rise and fall very sharply however this graph is showing data from a period of 48 hours so it very condensed.
The second graph below shows a "Sunset" over a period of 2 hours:
In the first graph, the peaks observed during the dark periods are people turning lights on it the room with the logger, if this data is extrapolated out it can be determined exactly how long the light was on.
No comments:
Post a Comment