LCD

Screen Shot 2014-05-30 at 16.54.15

Here is my code for the working LCD Crystal ball exercise. properly the most  fiddly project so far this used a lot of what was learnt in previous chapters, for example it has a poteniometer attached to change the contrast of the LCD, as shown below:

(video)

The LCD has 9 outputs in this project.

.Output 1 = Vss pin is grounded along with Output 16 (LED -)

Output 2 = Vcc connects directly to the 5v power along with Output

Output 3 = vO went to the centre pin of the pontentiometer to change the contrast of the screen.

Output 4 = RS goes to pin digital pin 12 of the board. this controls where the characters will appear on screen.

Output 5 = R/W to ground, puts the screen in read or write mode.

Output 6 = E Digital pin 11 of the board. (Enable) tells the lcd that it will be receiving a command.

Output 11 = D4 to digital pin 5

Output 12 = D5 to digital pin 4

Output 13= D6 to digital pin 3

Output 14 = D7 to digital pin 2

Output 15 = LED+  to 5v power through a 220ohm resistor.

Pins D0-7  are used to send character data to the screen. As you can see we used D4-D7

(More how they work soon).

 

Ohms Law

Another important mathematical equation to learn is the Ohms Law, this will allow you to understand how much current is travelling trough your circuit with the equation:

I = V/R

or

Current = voltage / resistance

So 3.3 volts with 220 ohm resistance = 0.015 amps or 15 amps

 

Also how much resistance is needed for your circuit to run properly:

R = V/I

Resistance = voltage/current

So 3.3 volts with 15 amps = 0.22 or 220 resistor

 

An finally how much voltage there is

V = IxR

Voltage = current x resistance

So 15 amps with 220 resistor or 0.22 = 3.3 volts

 

AMPS (amperes)

Amps are what the current is measured in and shows how much electrical current is flowing within your circuit.

To understand amps:

1 amp = 1000 milliamps

so 10 milliamps = 0.01 amp

These can be measured by a Multimeter.

 

Light Theremin

Quickly moving through the book and the next project is a creating a light sensitive tone generator.

Simple to create, the circuit has one photo resistor (as used for the RGB LED),  connected by a power wire, resistor to ground and an Analog (A0) cable from the Arduino.

img

Next the piezo is connected to the board, one leg grounded and the other connected to a digital output (pin 8 for this test). The code is as follows: Screen Shot 2014-05-30 at 14.11.36

the first 5 seconds the time to set the range  between 0 -1023 by placing you hand over the photo-resistor.

Now when you move your hand over it again your notice a tone which changes on light allowance. (No video for this one as it is an annoying noise).

C Code

Arduino is written in C language. But luckily the IDE has it’s own syntax that takes away the writing of a lot of the most used functions. For example:

digitalWrite();  actually compiles to:

#define ARDUINO_MAIN

#include “wiring_private.h”

#include “pins_arduino.h”

void pinMode(uint8_t pin, uint8_t mode)

{

uint8_t bit = digitalPinToBitMask(pin);

uint8_t port = digitalPinToPort(pin);

volatile uint8_t *reg, *out;

if (port == NOT_A_PIN) return;

// JWS: can I let the optimizer do this?

reg = portModeRegister(port);

out = portOutputRegister(port);

if (mode == INPUT) {

uint8_t oldSREG = SREG;

                cli();

*reg &= ~bit;

*out &= ~bit;

SREG = oldSREG;

} else if (mode == INPUT_PULLUP) {

uint8_t oldSREG = SREG;

                cli();

*reg &= ~bit;

*out |= bit;

SREG = oldSREG;

} else {

uint8_t oldSREG = SREG;

                cli();

*reg |= bit;

SREG = oldSREG;

}

}

// Forcing this inline keeps the callers from having to push their own stuff

// on the stack. It is a good performance win and only takes 1 more byte per

// user than calling. (It will take more bytes on the 168.)

//

// But shouldn’t this be moved into pinMode? Seems silly to check and do on

// each digitalread or write.

//

// Mark Sproul:

// – Removed inline. Save 170 bytes on atmega1280

// – changed to a switch statment; added 32 bytes but much easier to read and maintain.

// – Added more #ifdefs, now compiles for atmega645

//

//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));

//static inline void turnOffPWM(uint8_t timer)

static void turnOffPWM(uint8_t timer)

{

switch (timer)

{

#if defined(TCCR1A) && defined(COM1A1)

case TIMER1A:   cbi(TCCR1A, COM1A1);    break;

#endif

#if defined(TCCR1A) && defined(COM1B1)

case TIMER1B:   cbi(TCCR1A, COM1B1);    break;

#endif

#if defined(TCCR2) && defined(COM21)

case  TIMER2:   cbi(TCCR2, COM21);      break;

#endif

#if defined(TCCR0A) && defined(COM0A1)

case  TIMER0A:  cbi(TCCR0A, COM0A1);    break;

#endif

#if defined(TIMER0B) && defined(COM0B1)

case  TIMER0B:  cbi(TCCR0A, COM0B1);    break;

#endif

#if defined(TCCR2A) && defined(COM2A1)

case  TIMER2A:  cbi(TCCR2A, COM2A1);    break;

#endif

#if defined(TCCR2A) && defined(COM2B1)

case  TIMER2B:  cbi(TCCR2A, COM2B1);    break;

#endif

#if defined(TCCR3A) && defined(COM3A1)

case  TIMER3A:  cbi(TCCR3A, COM3A1);    break;

#endif

#if defined(TCCR3A) && defined(COM3B1)

case  TIMER3B:  cbi(TCCR3A, COM3B1);    break;

#endif

#if defined(TCCR3A) && defined(COM3C1)

case  TIMER3C:  cbi(TCCR3A, COM3C1);    break;

#endif

#if defined(TCCR4A) && defined(COM4A1)

case  TIMER4A:  cbi(TCCR4A, COM4A1);    break;

#endif

#if defined(TCCR4A) && defined(COM4B1)

case  TIMER4B:  cbi(TCCR4A, COM4B1);    break;

#endif

#if defined(TCCR4A) && defined(COM4C1)

case  TIMER4C:  cbi(TCCR4A, COM4C1);    break;

#endif

#if defined(TCCR4C) && defined(COM4D1)

case TIMER4D:cbi(TCCR4C, COM4D1);break;

#endif

#if defined(TCCR5A)

case  TIMER5A:  cbi(TCCR5A, COM5A1);    break;

case  TIMER5B:  cbi(TCCR5A, COM5B1);    break;

case  TIMER5C:  cbi(TCCR5A, COM5C1);    break;

#endif

}

}

void digitalWrite(uint8_t pin, uint8_t val)

{

uint8_t timer = digitalPinToTimer(pin);

uint8_t bit = digitalPinToBitMask(pin);

uint8_t port = digitalPinToPort(pin);

volatile uint8_t *out;

if (port == NOT_A_PIN) return;

// If the pin that support PWM output, we need to turn it off

// before doing a digital write.

if (timer != NOT_ON_TIMER) turnOffPWM(timer);

out = portOutputRegister(port);

uint8_t oldSREG = SREG;

cli();

if (val == LOW) {

*out &= ~bit;

} else {

*out |= bit;

}

SREG = oldSREG;

}

int digitalRead(uint8_t pin)

{

uint8_t timer = digitalPinToTimer(pin);

uint8_t bit = digitalPinToBitMask(pin);

uint8_t port = digitalPinToPort(pin);

if (port == NOT_A_PIN) return LOW;

// If the pin that support PWM output, we need to turn it off

// before getting a digital reading.

if (timer != NOT_ON_TIMER) turnOffPWM(timer);

if (*portInputRegister(port) & bit) return HIGH;

return LOW;

}

Time saved = massive, and when the code is compiled and sent to the Arduino processor it will open it up and read it in C which then coverts to computer language binary (1010101) which then run’s the code.

You can find all the functions and libraries in C in the location of: Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino and the files extensions are .h for header .c for C code and .cpp for C++ code.

Traffic Lights

….I made some. Agin this was using the digital pins to have the green light on at the begging, when somebody pushes the push button a delay of 2 seconds starts before the orange LED lights up, followed by a 3 second delay then he red LED.

The light stays on for 5-6 seconds before return to the orange light, another 3 second delay and eventually returning to green.

 

 

The breadboard

WP_20140529_028

This was just a experiment for me to understand how the breadboard worked and getting power to both sides.

As you can see it is extremely easy to power up the board and to utilise the other half of the board you just need to link up the (+) and (-) slots using two wires. Simple.

Image()

What I did was place 4 LED’s, two either side of the board, and light them up using one push button.

This turned them all on at once so, again for experimentation purposes, I set the 4 lights to be lit up in a pattern one after the other. (Look at LED lesson for simple setups). Once i had the 4 lights lighting up it reminded me of traffic lights. So….

 

Servo Motor

The code for the servo motor.

 

#include <Servo.h>
Servo servoRight; // Define right servo

int angle = 0; // servo position in degrees
void setup() {

servoRight.attach(9); // Set right servo to digital pin 9

servoRight.write(angle);
}

void loop() { // Loop through motion tests

forward();
delay (1500); // Example: move forward

reverse();
delay (1500); // Example: move forward

}
// Motion routines for forward, reverse, turns, and stop
void forward() {

servoRight.write(0);
}

void reverse() {

servoRight.write(180);
}

Setting up a servo is extremely easy. The red cable is for power, black for ground and white for signal. An easy setup for servo’s is 5v into the breadboard, power and ground go into the relevant pins and signal will go into a digital output say pin 9.

 

Happy Accident

While working on the PWM project, I changed one part of the code which was the

redValue = max(0, min(redSensorValue * 1.4 – 840, 255));

greenValue = max(0, min(greenSensorValue * 0.9 – 400, 255));

blueValue = max(0, min(blueSensorValue * 0.8 – 250, 255));

to

 

// redValue = redSensorValue 255;
// greenValue = greenSensorValue 255;
// blueValue = blueSensorValue 255;

 

What this does is blink the light red and blue depending on light source,  and stops when covered.

Example here:

PWM (Pulse Width Modulation)

Using PWM to create a three colour light sensitive LED.

To create this project I striped the board apart from the red power cable and the black ground cable connected between the Arduino board and breadboard. Then I added:

Three photo-resistors one for green, blue and red.  These when then attached to grounding resistors (10K), analogue A0,A1,A2 pins and power from the opposite side of the board by a wire attached across one end to the other.

Next was to add the Multi-colour LED ( cathode) which has a Red pin, then the cathode pin (for grounding), then the Blue and Green pins.  Once in each pin has to be powered by a Digital PWM pin (3,9,10,11,12) or any pin with a tilde sign ~  and then each grounded by 220 ohms…..code time.

Here is an image of my code working and the project working.

WP_20140529_023 WP_20140529_024 WP_20140529_025WP_20140529_026