I am new to Arduino, and totally new to writing any type of computer coding. I have purchased the Arduino starter kit, and I am working on the first project that involves code writing. The project is from the instruction book you receive with the starter kit. The project is titled “SPACESHIP INTERFACE”. I wrote and copied the code EXACTLY as it is displayed in the instruction book, but I can not get it to compile. I get error messages, and I don’t have any idea what these error messages mean. And also, if I copied EXACTLY what was in the book why is the code not correct? I have attached the program I wrote. Any help would greatly be appreciated. Thank you. And remember, I am new to Arduino, and I have never written a line of computer code in my life.
void loop() {
switchState = digitalRead(2);
if switchState = = LOW) {
// the button is not pressed
digitalWrite(3, HIGH); // green LED
digitalWrite(4, LOW); // red LED
digitalWrite(5, LOW); // red LED
}
else { // the buttton is pressed
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(250); // wait for a quarter of a second
// toggle the LEDs
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250); // wait for a quarter of a second
Unfortunately for you computers are really pedantic. They cannot cope with missing or misplaced punctuation marks or spaces where no space should be or missing or extra capital letters. Also things much be in the correct positions e.g. I'm pretty sure "int switchState = 0" should be before "void setup() not after it.
You get used to it but at first copying things EXACTLY is harder than you might think.
I'm going to let you in on a secret:
The code for all the projects in the book are available in the Arduino IDE under File > Examples > 10.StarterKit_BasicKit. In this case, you would want the "p02SpaceshipInterface" example.
Instead of wasting your time copying code from paper to the computer and then fixing all the inevitable typos, you can now put that time towards studying the code line by line and doing research and experiments when you don't understand something.
Instead of wasting your time copying code from paper to the computer and then fixing all the inevitable typos, you can now put that time towards studying the code line by line and doing research and experiments when you don't understand something.
Didn't we suffer for our art back in the late 70s with that !
brianphoenix:
but I can not get it to compile. I get error messages, and I don't have any idea what these error messages
In this case the advice in Reply #7 is probably the most useful.
For the future, if you get an error message from the compiler you should post it as well as the code that produced the error.
The first error is usually the most important and the error message will include the line number where the compiler got stuck. However the actual error may be one or two lines earlier.
The only thing you learn from copying text from a piece of dead tree to a computer is how to copy text from a piece of dead tree to a computer, a skill that is completely worthless today.
Sure, if they are just going to blindly upload the example to their Arduino and never go any farther in the journey then they will have learned little (not that there's necessarily anything wrong with someone just wanting to reproduce a project and calling it good right there). But if they came into the project with no interest in continuing on the learning journey, then no amount of typos is going to change that. In fact, I believe that immediate frustration of the introduced typos could throw some people off, who might have gotten hooked if they had an immediate success with the canned example at the start, followed later by the inevitable learning experience of fixing bugs once they start modifying the examples or writing their own code.
The people who would happily stay up all night copying code out of a magazine are probably going to get into this stuff no matter how many speed bumps they hit along the way. But there are a lot of other people out there who have some interest in the subject, but perhaps aren't absolutely obsessed and may have other things competing for their attention. I think the real magic of Arduino is to make microcontrollers more accessible to that group so they can have the chance to get hooked.
Whether you learn anything about programming by copying code into the Arduino or loading and running an example surely depends on your personality.
When I got an Arduino I had never before programmed in C let alone C++, although I had experience of other programming languages. I loaded the Blink sketch and uploaded it and got an immediate sense of achievement although the Uno had already been running it anyway as it turned out.
Looking at the code I could see a call to the delay() function with a number in the brackets. What would happen if I changed the number I wondered, so I changed it to 5000 and uploaded the sketch and the blink rate changed. WOW, I can program in C++ ! I suspect that this experience is common to many others but had I simply been presented with an empty editor screen progress would have been slower by an order of magnitude.
I'd say copying code from a book can offer a useful learning experience, particularly when you inevitably make a few unimportant looking typos and the compiler spits loads of errors at you. If nothing else you learn that when programming computers you need to be precise in your thinking and typing.
That's pretty much how I learned programming though in those days it was punched cards, FORTRAN and you had to wait until the next day to discover you'd made a silly typo. It may not be much easier now but at least it's a lot faster.
AWOL:
Happily, there isn't really a modern-day analogy of the dropped card deck, or the hamster ate my fan-fold.
The cards used to have the instructions "Do not fold, spindle, or mutilate" printed on them. The mechanical card readers apparently did not get the memo.