IAN LANG ELECTRONICS

And this was done to keep the input pins reliably low, as the pins of the ATmega are prone to floating voltages.

 

 

This time the switched is configured as below:

CIRC-07

Go Back

Hmmm. Oomlout seem to think this circuit somewhat underwhelming and it appears that they haven't lavished their usual care and attention on it. So we'll give it some love here.  Here's the circuit as directed by the book:

And here's how I realised it on the breadboard. The leftmost switch is the one used for the first code.

Before we go delving into any code here let's have a look at how that switch has been done. Previously we have always configured our switches in this manner:

 

5V

0V

10k

to input pin

0V

to input pin

5V

And this is done to get the Arduino pin reliably high when the button is not pressed. In both cases they are working on a principle of a voltage divider.

It doesn't actually matter what configuration we use but, as we shall see, we need to write the code to match. Right then, here's the first batch of code to use on circuit:

 

 

 

// constants won’t change. They’re used here to

// set pin numbers:

const int buttonPin = 2;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin

 

// variables will change:

int buttonState = 0;         // variable for reading the pushbutton status

 

void setup() {

  // initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);      

  // initialize the pushbutton pin as an input:

  pinMode(buttonPin, INPUT);    

}

 

void loop(){

  // read the state of the pushbutton value:

  buttonState = digitalRead(buttonPin);

 

  // check if the pushbutton is pressed.

  // if it is, the buttonState is HIGH:

  if (buttonState == HIGH) {    

    // turn LED on:    

    digitalWrite(ledPin, HIGH);  

  }

  else {

    // turn LED off:

    digitalWrite(ledPin, LOW);

  }

}

 

What'll happen when you upload this code is that the LED will come on. If you press the button, it'll go off. This is the obverse of what's supposed to happen and it's because we've configured the button to keep the pin high rather than low. We could change the configuration of the button, but it's easier to change the code. In the first instance it says :

 if (buttonState == HIGH) {    

    // turn LED on:    

    digitalWrite(ledPin, HIGH);  

 

Change that HIGH to LOW  (and don't forget to put it in capitals like I did)

 

Later on it says:

 else {

    // turn LED off:

    digitalWrite(ledPin, LOW);

 

Change that LOW to HIGH and re-upload the code to your board. This time the LED should be off, and when you press the button it should go on. When you release, off it goes again.

 

The second batch of code is also obverse but it doesn't actually matter. As it stands, the leftmost switch will switch the LED on and the rightmost switches it off again.

 

 

 

int ledPin = 13; // choose the pin for the LED

int inputPin1 = 3; // button 1

int inputPin2 = 2; // button 2

 

void setup() {

  pinMode(ledPin, OUTPUT); // declare LED as output

  pinMode(inputPin1, INPUT); // make button 1 an input

  pinMode(inputPin2, INPUT); // make button 2 an input

}

 

void loop(){

  if (digitalRead(inputPin1) == LOW) {

    digitalWrite(ledPin, LOW); // turn LED OFF

  } else if (digitalRead(inputPin2) == LOW) {

    digitalWrite(ledPin, HIGH); // turn LED ON

  }

}

 

The decision as to whether a button has been pressed or not and the consequent action to take is expressed as an  if.....else if condition. Wherever there's only one condition in an else, you don't need it. Try deleting the word " else " and re-uploading the code.The circuit will work just the same.This is a case of an agile programmer being too agile, I suspect.

 

When you come to the third part of the code you might get the compiler moaning, probably twice. the first time it might come up with this:

 

stray "\" in program

 

which means it's found a character it doesn't like. i spent 20 minutes looking for this character and couldn't find it, so just type the whole line in again manually and it's satisfied. The next thing it  may moan about in this line is:

 

expected primary-expression before ";" token

 

In plain English, it's saying that the bit at the end that says {value - ;} is missing a minus symbol, so chuck one in and get {value -- ;}

Whilst you're at it, at the very top of the code, change the code from ledPin =13 to ledPin = 9 and move your jump wire that's in pin 13 to pin 9 on your board; if you try to do this on pin 13 it'll not work correctly because 13 has a built in LED that affects the current.

Upload the lot to your board and watch as your LED fails to shine. Never fear! Press down, and keep pressed down, the leftmost button. The LED gets gradually brighter until it reaches a maximum. Now do the same with the rightmost button. The LED gets dimmer and goes off. The rate at which it goes up and down is a bit fast, so change the last line of the code to say delay (100); instead of delay (10); and re-upload to your board. You should find when you press the buttons that the rate of change in both directions is a lot slower. If you put a bigger number in that delay, it'll be slower still.

Here's the modified code then:

 

int ledPin = 9; // choose the pin for the LED

int inputPin1 = 3; // button 1

int inputPin2 = 2; // button 2

void setup() {

  pinMode(ledPin, OUTPUT); // declare LED as output

  pinMode(inputPin1, INPUT); // make button 1 an input

  pinMode(inputPin2, INPUT); // make button 2 an input

}

 

int value = 0;

void loop(){

  if (digitalRead(inputPin1)==LOW) {value= value --;}

  else if (digitalRead(inputPin2) == LOW) { value=value++; }

  value = constrain(value, 0, 255);

  analogWrite(ledPin, value);

  delay(100);

}

 

Once again we have an if.....else if conditional with only one else condition, so scrub that word "else" out in the code because it's not needed.

 

 

 

 

 

 

 

 

 

Voltage Divider More> Copy & Paste Code from Here:

If you look closely at the code on the right, you'll see a command that we haven't used in these pages before:

 

constrain (value, 0,255)

 

This is a mathematical operator and it makes sure that no matter how big or small a number gets, the maximum it will be as far as your Arduino is concerned is 255, and the minimum is 0. You can use other numerical values too.