Help for Newbe


I am new to Arduino because I wanted to replicate an electronic circuit I build using transistors and IC’s.

I have in the past programed in BASIC but not for many many years.

Having viewed some tutorials online I wrote what I thought was a good bit of code (or sketch) only to find when I tried it on UnoArduSim (Simulator) it failed to run as I expected. I cannot see why and would appreciate any help in seeing what is incorect in my code which simply cycles through 4 LED’s on a button press.

DDI_V1.ino (1.13 KB)

The pinMode(pin, OUTPUT) sets the pin to output and the default state is LOW. None of the LED pins are HIGH or ever set to HIGH so nothing can happen.

How is your switch wired? Does it have a pulldown resistor?

Please read the "how to use the forum" stickies to see how to properly post code. Here is your code posted properly:

// Constants

const int ledPin9 = 9;
const int ledPin10 = 10;
const int ledPin11=11;
const int ledPin12=12;
const int buttonPin=2;

// Variables
int buttonState=0;
int ledPin9State=1;
int ledPin10State=0;
int ledPin11State=0;
int ledPin12State=0;

void setup() {  
 pinMode(ledPin9, OUTPUT);
 pinMode(ledPin10, OUTPUT);
 pinMode(ledPin11, OUTPUT);
 pinMode(ledPin12, OUTPUT);
 pinMode(buttonPin, INPUT);

void loop() {
 buttonState = digitalRead(buttonPin);
 ledPin9State = digitalRead(ledPin9);
 ledPin10State = digitalRead(ledPin10);
 ledPin10State = digitalRead(ledPin11);
 ledPin10State = digitalRead(ledPin12);
 if (buttonState == HIGH && ledPin9State == HIGH) {
 digitalWrite(ledPin9, LOW);
 digitalWrite(ledPin10, HIGH);
 if (buttonState == HIGH && ledPin10State == HIGH) {
 digitalWrite(ledPin10, LOW);
 digitalWrite(ledPin11, HIGH);
 if (buttonState == HIGH && ledPin11State == HIGH) {
 digitalWrite(ledPin11, LOW);
 digitalWrite(ledPin12, HIGH);
 if (buttonState == HIGH && ledPin12State == HIGH) {
 digitalWrite(ledPin12, LOW);
 digitalWrite(ledPin9, HIGH);
  buttonState = digitalRead(buttonPin);

You do this each time through loop() so when the button is pressed buttonState will remain in its pressed state all the time that the button is pressed. The loop() function repeats thousands of times a second. Can you see how that might be a problem ?

You need to detect when the button becomes pressed rather than when it is pressed. Look at the StateChangeDetection example in the IDE to see how to do it.