How to get started with arduino

Hi all,

I'm completely new to arduino and have not mastered programming ;-). I'm trying to build the following, can anyone help me in the right direction?

  • Arduino Uno
  • 2 leds (red and green)
  • 1 microswitch

Default state: microswitch activated - green led on
Microswitch released - red led on for 5 seconds
after 5 seconds check if microswitch is activated, if yes --> default state, if no --> blink red led

Start working your way through the relevant example sketches under File > Examples. Start with File > Examples > 01. Basics > BareMinimum, then File > Examples > 01. Basics > Blink, etc. The associated tutorials are here:

Make sure you understand what every line of code does before moving on to the next example. When you find something you don't understand look it up in the reference:
http://www.arduino.cc/reference/en
Make some changes to the example code and verify it has the expected effect.

Is the following acceptable code in regards to jumping from one function to the other?

// constants won't change. They're used here to set pin numbers:
   const int buttonPin = 2;              // the number of the pushbutton pin
   const int greenled =  12;             // the number of the green LED pin
   const int redled =  13;               // the number of the red LED pin

// variables will change:
   int buttonState = 0;                  // variable for reading the pushbutton status

void setup() {
   pinMode(greenled, OUTPUT);     		 // initialize the RED LED pin as an output
   pinMode(redled, OUTPUT);          	 // initialize the GREEN LED pin as an output
   pinMode(buttonPin, INPUT);            // initialize the pushbutton pin as an input
}

void loop() {
   buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
   if (buttonState == HIGH) {			 // check if the pushbutton is pressed. If it is, the buttonState is HIGH
      digitalWrite(greenled, HIGH); 	 // turn green led on
   } else {
      digitalWrite(greenled, LOW);  	 // turn greenled off
      activated();		 				 // jump to function activated
   }
}

void activated() {
   digitalWrite(redled, HIGH);  		 // turn red led on
   delay(5000); 		 				 // wait for 5 seconds            
   errorcheck();		 				 // jump to function errorcheck
}

void errorcheck() {
   buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
   if (buttonState == HIGH) {
     loop();			                 // jump to function main loop
   } else {								 // blink red led
     digitalWrite(redled, HIGH);         // turn the LED on (HIGH is the voltage level)
     delay(500);                         // wait for a second
     digitalWrite(redled, LOW);          // turn the LED off by making the voltage LOW
     delay(500);                         // wait for a second
   }
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

wouterheer:
Is the following acceptable code

Calling loop() from a function called from loop() is definitely not acceptable.

Use of delays in a program that polls an input is only acceptable if you don't mind your program being very unresponsive.

Back to the examples with you. Make sure to spend some quality time with File > Examples > 02.Digital > BlinkWithoutDelay.

There are a couple of great free books to help get started and hold on to for reference.

Arduino Programming Notebook
by Brian W. Evans

and

Introduction to Arduino: A piece of cake!
by Alan G. Smith

http://www.introtoarduino.com/

Can’t agree more with the books and examples - crashing around just trying things will just get you frustrated and you’ll give up .
Google is also useful
For some great guides.

I now have the following code, but for some reason the variable is not writen or read correctly, the led never moves to blinking...

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; 
const int greenled =  12;  
const int redled =  13; 
const long interval = 250;   

// variables will change:
int buttonState = 0;            
int errorState = 0;
int ledState = LOW;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; 

void setup() {
  pinMode(greenled, OUTPUT);
  pinMode(redled, OUTPUT); 
  pinMode(buttonPin, INPUT);
}

void loop() {
  unsigned long currentMillis = millis();
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    digitalWrite(greenled, HIGH);
    digitalWrite(redled, LOW);
    errorState = 1;
  } else if (buttonState == HIGH)  {
    if (errorState = 1) {
      digitalWrite(greenled, LOW);
      digitalWrite(redled, HIGH);
      delay(1000);
      errorState = 2;
    } else if (errorState = 2) {
      digitalWrite(greenled, LOW); 
      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis; 
        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }
        digitalWrite(redled, ledState);
      }
    }
  }
}

Already got it :slight_smile:

if (errorState = 2) should be if (errorState == 2)