Learning Arduino programming
Discussion
I've bought myself a toy, an arduino kit off ebay.
I've got it working by downloading prewritten scripts from various sites.
Now while i have got the electrical wiring side reasonably sorted i'm at an absolute loss at where to start writing programs.
I think the programming is based on C++ but not the same.
So any guidance on a decent book to buy?
No idea what i will do with it eventually
But got to learn how to program it first.
So the short term ambition is switch an LED on and off using two buttons
I've got it working by downloading prewritten scripts from various sites.
Now while i have got the electrical wiring side reasonably sorted i'm at an absolute loss at where to start writing programs.
I think the programming is based on C++ but not the same.
So any guidance on a decent book to buy?
No idea what i will do with it eventually
But got to learn how to program it first.
So the short term ambition is switch an LED on and off using two buttons
The arduino website is probably the best resource for code specific to the hardware. If it is general C/C++ programming techniques you need then there are lots of introductory tutorials at http://www.cprogramming.com/
Brite spark said:
How's this different from pi? Different programming?
Different hardware Different software
its a much more basic level. No video. No storage other than on the processor (2k ram and 32K flash). Slower processor. Very modular though in that you can get expansion boards(shields) that plug in to give you various interfaces to all kinds of stuff.
The hardware is designed to give very good access to external devices attached to the base board. Looking a the base specs it looks very restricted but with a few sensors and a bit of code its amazing what can be done.
One of my favourites is http://www.ardupilot.co.uk/ although they don't use the standard board any more and have integrated the sensor these days to produce an off the shelf product it started out based on the basic board.
This is a list (list of lists) of shields available.
http://shieldlist.org/
McWigglebum4th said:
I've bought myself a toy, an arduino kit off ebay.
I've got it working by downloading prewritten scripts from various sites.
Now while i have got the electrical wiring side reasonably sorted i'm at an absolute loss at where to start writing programs.
I think the programming is based on C++ but not the same.
So any guidance on a decent book to buy?
No idea what i will do with it eventually
But got to learn how to program it first.
So the short term ambition is switch an LED on and off using two buttons
the arduino website if full of useful stuffI've got it working by downloading prewritten scripts from various sites.
Now while i have got the electrical wiring side reasonably sorted i'm at an absolute loss at where to start writing programs.
I think the programming is based on C++ but not the same.
So any guidance on a decent book to buy?
No idea what i will do with it eventually
But got to learn how to program it first.
So the short term ambition is switch an LED on and off using two buttons
http://arduino.cc/en/tutorial/button
http://www.arduino.cc/en/Tutorial/BlinkingLED
Combination of those 2 should have up and running in under 30minutes

Don't do what I did and dive straight in at the deep end. Start here http://arduino.cc/en/Tutorial/HomePage
My first project is an 8x8x8 array of leds multiplexed through 4 x TLC5490 PWM drivers to make a 3D cube running at 50Hz frame rate via interrupts driven off the CPU clock crystal. CPU time left over is used to provide data to this frame buffer for animation. I'll let you know if/when I get it fully working. The TLC code was quite tricky but works now. Just got to get control of a shift register via SPI to get the multiplexing up and running.
Should end up all 512 LEDs individually addressable with up to 255 brightness levels 50 times per sec.
Just need some spare time now

Ambitious
I will say i have some uses already pencilled in for it
I want a wheel spin detector for my landrover so i know which back wheel is spinning so i can brake the correct wheel.
I might even see if i can fit a front axle and the back and see if i can get an arduino to give me 4 wheel steering via some hydraulic valves
And it would be great for replacing the timer on the central heating
I will say i have some uses already pencilled in for it
I want a wheel spin detector for my landrover so i know which back wheel is spinning so i can brake the correct wheel.
I might even see if i can fit a front axle and the back and see if i can get an arduino to give me 4 wheel steering via some hydraulic valves
And it would be great for replacing the timer on the central heating
Brite spark said:
How's this different from pi? Different programming?
The Pi is a micro-PC, the Arduino and variants are Embedded systems.As stated the Arduino has nothing like the HP of the Pi, but at the same time it's designed for controlling things, not running a GUI.
It has Real-time capability (which the Pi doesn't, you can do soft-pseudo, but not hard).
You have more language options on the Pi, with the Arduino you are pretty much limited to assembly, C/C++ and maybe ADA. I'm not aware of every compiler for the AVR 8-Bit core.
In my mission to get an LED to switch on and off using two button i have written the following but it no worky
Any ideas?
int switchon = 3;
int switchoff = 2;
int ledPin = 13;
boolean ledOn = false;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchon) == HIGH && ledOn == false)
{
digitalWrite(ledPin, HIGH);
}
else
{
;
}}
if (digitalRead(switchoff) == HIGH && ledOn == true)
{
digitalWrite(ledPin, LOW);
}
else
{
;
}
Any ideas?
int switchon = 3;
int switchoff = 2;
int ledPin = 13;
boolean ledOn = false;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchon) == HIGH && ledOn == false)
{
digitalWrite(ledPin, HIGH);
}
else
{
;
}}
if (digitalRead(switchoff) == HIGH && ledOn == true)
{
digitalWrite(ledPin, LOW);
}
else
{
;
}
McWigglebum4th said:
In my mission to get an LED to switch on and off using two button i have written the following but it no worky
Any ideas?
int switchon = 3;
int switchoff = 2;
int ledPin = 13;
boolean ledOn = false;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchon) == HIGH && ledOn == false)
{
digitalWrite(ledPin, HIGH);
}
else
{
;
}}
if (digitalRead(switchoff) == HIGH && ledOn == true)
{
digitalWrite(ledPin, LOW);
}
else
{
;
}
I'm not familiar with this environment, but I do know C and C++ very well.Any ideas?
int switchon = 3;
int switchoff = 2;
int ledPin = 13;
boolean ledOn = false;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchon) == HIGH && ledOn == false)
{
digitalWrite(ledPin, HIGH);
}
else
{
;
}}
if (digitalRead(switchoff) == HIGH && ledOn == true)
{
digitalWrite(ledPin, LOW);
}
else
{
;
}
I'm assuming loop() is somehow called time and time again from within another loop somewhere else?
Also, it looks to me like the if/else block for the second condition (checking for Off) is outside the loop() function.
I could be wrong though, as I said I'm unfamiliar...
McWigglebum4th said:
Imagine you are explaining this to someone who hasn't got a clue about programming beyond
10 Print "McWiggle bum rocks"
20 GOTO 10
Then you know what level we are dealing with
OK - a couple of things that might help. Long post warning!10 Print "McWiggle bum rocks"
20 GOTO 10
Then you know what level we are dealing with
If you look at the closing brace of the "loop" function it ends where I put the the //<--- comment. The "if" clause that follows it needs to be inside this closing brace or it will never get evaluated.
/////////////////////////////////////////////
int switchon = 3;
int switchoff = 2;
int ledPin = 13;
boolean ledOn = false;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchon) == HIGH && ledOn == false)
{
digitalWrite(ledPin, HIGH);
}
else
{
;
}
} //<---- loop() ends here
if (digitalRead(switchoff) == HIGH && ledOn == true) // this bit never gets evaluated
{
digitalWrite(ledPin, LOW);
}
else
{
;
}
//////////////////////////
so lets put that second "if" inside the loop function
version 2
/////////////////////////////////////////////
int switchon = 3;
int switchoff = 2;
int ledPin = 13;
boolean ledOn = false;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchon) == HIGH && ledOn == false)
{
digitalWrite(ledPin, HIGH);
}
else
{
;
}
if (digitalRead(switchoff) == HIGH && ledOn == true) // this bit now gets evaluated every time loop is called
{
digitalWrite(ledPin, LOW);
}
else
{
;
}
} //<---- loop() ends here
///////////////////////////////////
Next problem is you create a variable called "ledOn" at the start of the code and initialise it to false, but you never set it to true when the led gets switched on. As it is part of your state test in the "if" clauses, you need to make sure it has the right value.
version 3
/////////////////////////////////////////////
int switchon = 3;
int switchoff = 2;
int ledPin = 13;
boolean ledOn = false;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchon) == HIGH && ledOn == false)
{
digitalWrite(ledPin, HIGH);
ledOn = true; //<--- just turned the led on - make sure the code knows this
}
else
{
;
}
if (digitalRead(switchoff) == HIGH && ledOn == true)
{
digitalWrite(ledPin, LOW);
ledOn = false; //<--- just turned the led off - make sure the code knows this too
}
else
{
;
}
}
///////////////////////////////////
finally, both the "else" clauses are empty so you can clean up by removing them.
version 4
/////////////////////////////////////////////
int switchon = 3;
int switchoff = 2;
int ledPin = 13;
boolean ledOn = false;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchon) == HIGH && ledOn == false)
{
digitalWrite(ledPin, HIGH);
ledOn = true; //<--- just turned the led on - make sure the code knows this
}
if (digitalRead(switchoff) == HIGH && ledOn == true)
{
digitalWrite(ledPin, LOW);
ledOn = false; //<--- just turned the led off - make sure the code knows this too
}
}
///////////////////////////////////
So following the logic through,
every time "loop" is called
check the "switchon" pin
if it is high and the led is currently off
then turn on the led and record the fact in the ledOn variable
then check the "switchoff" pin
if that is high and the led is currently on
then switch the led off and record the fact in the ledOn variable
exit "loop" ready for next time round
Sorry if this is an egg sucking lecture...
just in case you don't already try indenting your code between the braces { } using tab key as this will help you keep track of which code will get executed in each block. This is version 4 from above. Note I had to swap the { for [ to get pistonheads to keep the formatting but you would keep using the { } in the arduino editor
int switchon = 3;
int switchoff = 2;
int ledPin = 13;
boolean ledOn = false;
void setup()
[
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
]
void loop()
[
if (digitalRead(switchon) == HIGH && ledOn == false)
[
digitalWrite(ledPin, HIGH);
ledOn = true; //<--- just turned the led on - make sure the code knows this
]
if (digitalRead(switchoff) == HIGH && ledOn == true)
[
digitalWrite(ledPin, LOW);
ledOn = false; //<--- just turned the led off - make sure the code knows this too
]
]
Edited by malman on Wednesday 17th April 11:56
malman said:
just in case you don't already try indenting your code between the braces { } using tab key as this will help you keep track of which code will get executed in each block. This is version 4 from above. Note I had to swap the { for [ to get pistonheads to keep the formatting but you would keep using the { } in the arduino editor
I was going to suggest indenting, but couldn't get the PH editor to cope with the braces! Nice one.For my next trick i shall and get any button to switch the LED on and the opposite button to switch it off again
So i wrote this but it don't work
//////////////////
int switchleft = 3;
int switchright = 2;
int ledPin = 13;
boolean ledon = false;
boolean lastbuttonleft = true; //////if false then it is right button set to left button
void setup()
{
pinMode(switchleft, INPUT);
pinMode(switchright, INPUT);
pinMode(ledPin,OUTPUT);
}
void loop()////start of the loop
{
if (ledon == false && digitalRead(switchleft) == HIGH)
{
digitalWrite(ledPin, HIGH);///turn the led on
ledon = true; ///code knows the led is on
}
if (ledon == false && digitalRead(switchright) == HIGH)
{
digitalWrite(ledPin, HIGH);///turn the led on
ledon = true; ///code knows the led is on
lastbuttonleft = false; //// Code knows the last button was the right button now left button is off
}
/// finished switching the led on
if (ledon == true && lastbuttonleft == true && switchright == HIGH)
{
digitalWrite(ledPin, LOW);///turn led off
ledon = false;///code knows led is off
lastbuttonleft = false;///code knows last button was right
}
if (ledon == true && lastbuttonleft == false && switchleft == HIGH)
{
digitalWrite(ledPin, LOW);///turn led off
ledon = false;///code knows led is off
lastbuttonleft = true;///code knows last button was right
}
}
So i wrote this but it don't work
//////////////////
int switchleft = 3;
int switchright = 2;
int ledPin = 13;
boolean ledon = false;
boolean lastbuttonleft = true; //////if false then it is right button set to left button
void setup()
{
pinMode(switchleft, INPUT);
pinMode(switchright, INPUT);
pinMode(ledPin,OUTPUT);
}
void loop()////start of the loop
{
if (ledon == false && digitalRead(switchleft) == HIGH)
{
digitalWrite(ledPin, HIGH);///turn the led on
ledon = true; ///code knows the led is on
}
if (ledon == false && digitalRead(switchright) == HIGH)
{
digitalWrite(ledPin, HIGH);///turn the led on
ledon = true; ///code knows the led is on
lastbuttonleft = false; //// Code knows the last button was the right button now left button is off
}
/// finished switching the led on
if (ledon == true && lastbuttonleft == true && switchright == HIGH)
{
digitalWrite(ledPin, LOW);///turn led off
ledon = false;///code knows led is off
lastbuttonleft = false;///code knows last button was right
}
if (ledon == true && lastbuttonleft == false && switchleft == HIGH)
{
digitalWrite(ledPin, LOW);///turn led off
ledon = false;///code knows led is off
lastbuttonleft = true;///code knows last button was right
}
}
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


