Need Help programming the Arduino Microcontroller

Ok so for our assignment we need to program the arduino. But I have never done programming before. From examples I got this program

/*
Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.

The circuit:

  • potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
  • LED connected from digital pin 9 to ground

created 29 Dec. 2008
by Tom Igoe

*/

// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = 0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(10);
}

This is a simple program which turns on an LED on pin 9 when the button is pressed. My question is how would I turn on a light on when 13 but when the button is pressed turn off the light on pin 13 and turn on the one on pin 9??

Your comments say that you are reading a potentiometer value (connected to analog pin 0), but your question is about reading a switch and doing something with the reading. Which is it?

how would I turn on a light on when 13 but when the button is pressed turn off the light on pin 13 and turn on the one on pin 9??

When what is 13?

Hi

Is this what you are looking for?

const int buttonPin = 5; // Attach button to this pin
const int ledPin9 = 9;
const int ledPin13 = 13;

void setup(){
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); //internal pull up recistor
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin13, OUTPUT);
}

void loop(){
  if (digitalRead(buttonPin) == LOW){ //button is pressed
    digitalWrite(ledPin9, LOW);
    digitalWrite(ledPin13, HIGH);
  }
  else{
    digitalWrite(ledPin9, HIGH);
    digitalWrite(ledPin13, LOW);
  }
}

Code is compiled but untested.

-Fletcher

Paul- I don't understand. Its one of the sample programs which turns on and off an LED

Fletcher- For this can you explain a step by step of what to connect where, sorry i'm a bit new

Hi Killa

Connect a button between pin no. 5 and ground (GND). You could do this with a wire connected between no. 5 and gnd - and simulate press/unpress by disconnecting it. Please be carefull with your won programs; if you forget this line:

digitalWrite(buttonPin, HIGH);

you will destroy the pin!

Connect a 220 ohm recistor to pin 9. Connect a led to the recistor. Connect that other leg of the led to GND.

You are now set to go since pin no. 13 has it's own onboard led.

-Fletcher

if you forget this line:

Code:digitalWrite(buttonPin, HIGH);

you will destroy the pin!

How?

It will float around, but it won't do any harm.

Hi

buttonPin is declared as input
~~ ~~void setup(){  pinMode(buttonPin, INPUT);  digitalWrite(buttonPin, HIGH); //internal pull up recistor  ....~~ ~~
If you forget the
~~ ~~digitalWrite(buttonPin, HIGH); //internal pull up recistor~~ ~~
and connece the pin directly GND (by pressing the button) I reckon it gonna draw way more than 20 mA.

You are right and I was wrong:

This also means however, that input pins with nothing connected to them, or with wires connected to them that are not connected to other circuits, will report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin.

-Fletcher