Week 6

This week we are going to try to send our sensor data to Processing by using “Serial Communication”.

To download and learn more about processing, visit www.processing.cc

Below code will print “Hello World” in your serial monitor every 1000ms.

void setup() {
  Serial.begin(9600); //declare serial port
}

void loop() {
  Serial.println("Hello World!"); //send "Hello World!" message to serial port
  delay(1000); //wait for 1000 ms.
}

Use the code below for Processing. This code will print “Hello World” in Processing message panel.

import processing.serial.*; //import serial library

Serial myPort;  // create object from Serial class
String val;     // string received from the serial port

void setup() {
  // I know my port is #3
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  //You can print out available port list by doing this:
  printArray(Serial.list());
  String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) 
  {  // If data is available,
    val = myPort.readStringUntil('\n');         // read it until end of the line and store it in val
  } 
  println(val); //print it out in the console
}

Use this code to send binary values to Processing

void setup() {
  Serial.begin(9600);
}

void loop() {
  int analogValue = analogRead(A0); // read the sensor value
  analogValue = map(analogValue, 0, 1023, 0, 255); // map sensor values to between 0 - 255 
  Serial.write(analogValue);          // send the value serially as a binary value
}

Use this code to draw your sensor values as a chart on Processing

import processing.serial.*;
Serial myPort;


// at the top of the program:
float xPos = 0; // horizontal position of the graph
float yPos = 0; // vertical position of the graph



void setup () {
  size(800, 600);        // window size

  // List all the available serial ports
  printArray(Serial.list());
  // change the number below to match your port:
  String portName = Serial.list()[3];
  myPort = new Serial(this, portName, 9600);
  background(#081640);
}


void serialEvent (Serial myPort) {
  // get the byte:
  int inByte = myPort.read();
  // print it:
  println(inByte);
  yPos = height - inByte;
}

void draw () {
  // draw the line in a pretty color:
  stroke(#A8D9A7);
  line(xPos, height, xPos, yPos);
  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
    // clear the screen by resetting the background:
    background(#081640);
  } else {
    // increment the horizontal position for the next reading:
    xPos++;
  }
}

 

Week 5

This example shows you how to set up a threshold value for your sensors once you start running your Arduino.

int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
float threshold = 0.0; // threshold value
int numOfReading = 10; //number of readings (number of samples)
float totalVal = 0.0; //sum up of all sensor readings

const int LED = 11;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (int i = 0; i < numOfReading; i++) {
    totalVal = totalVal + getFah();
    Serial.print("total Value ");
    Serial.println( totalVal);
    delay(2);
  }
  threshold = totalVal / numOfReading;
  Serial.print("Threshold Value ");
  Serial.println( threshold);

  pinMode(LED, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  float fah = getFah();
  if (threshold < fah) {
    float dif = fah - threshold;
    float brightness = map(dif, 0, 20 , 0, 255);
    analogWrite(LED, brightness);
  }
}


float getFah() {
  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  return temperatureF;
}


float getCel() {
  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage = voltage / 1024.0;
  float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
  return temperatureC;
}

 

This example shows you how to keep last 10 sensor readings in Arduino’s mind, and sum up those 10 values to find an average of the 10 values. You can use this example for smoothing your sensor values.

const int numArray = 10;
float val[numArray];
const int acc = 0;
float smooth = 0.0;
float sum = 0.0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (int i = 0; i < numArray; i++) {
    val[i] = 0.0;
  }
}

void loop() {
  // put your main code here, to run repeatedly:

  for (int i = 1 ;  i < numArray; i++) {
    val[i - 1] = val[i];
    Serial.print( i );
    Serial.print("  " );
    Serial.println(val[i]);
  }
  val[9] = analogRead(acc);

  sum = 0 ;

  for (int i = 0 ;  i < numArray; i++) {
    sum = sum + val[i];
  }

  smooth = sum / (numArray);
  Serial.print("Smooth ");
  Serial.println( smooth);
}

 

 

Week 2

This weeks slides are on this link: Presentation_Week2

This is a helpful link for understanding color coding of resistors: http://www.instructables.com/id/Resistor-Color-Coding/

You can also use this link to calculate your resistor value based on it is colors: http://www.dannyg.com/examples/res2/resistor.htm

This week’s assignments are these 4 labs:

Making Multiple LED’s Blink

What do you need:

  • Arduino
  • Breadboard
  • 2 LED’s
  • 2 220 Ohm Resistors
  • Wires

 

LED LAb_05

Code:

// the setup function runs once when you press reset or power the board

void setup() {

  // initialize digital pin 13 as an output.

  pinMode(13, OUTPUT);

  pinMode(12, OUTPUT);

}

// the loop function runs over and over again forever

void loop() {

//Lets turn on/off the first LED now.

  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(1000);              // wait for a second

  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW

  delay(1000);              // wait for a second

//Lets turn on/off the second LED now.

  digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(1000);              // wait for a second

  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW

  delay(1000);              // wait for a second

}

 

Switch and an LED

What do you need:

  • Arduino
  • Breadboard
  • 1 LED
  • 1 220 Ohm Resistor
  • 1 10 Kilohm Resistor
  • 1 switch
  • Wires

LED LAb_06

Code:

// set pin numbers:
int buttonPin = 2; // the number of the pushbutton pin
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);
}
}

Switch and Three LED’s

What do you need:

  • Arduino
  • Breadboard
  • 3 LED’s
  • 3 220 Ohm Resistors
  • 1 10 Kilohm Resistors
  • 1 switch
  • Wires

LED LAb_07_bb

/*
This example is very similar to previous switch example. The difference is when you press the switch (HIGH) your lights will light up from left to right on your breadboard. If you don't press on your switch (LOW), your LED's will light up from right to left. 

Feel free to play with your delay times, the order of LED's.

Mustafa
*/

// set pin numbers:
int buttonPin = 2; // the number of the pushbutton pin

int ledPin0 = 13; // the number of the first LED pin
int ledPin1 = 12; // the number of the second LED pin
int ledPin2 = 11; // the number of the third LED pin

 
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
 
void setup() {
// initialize the LED pins as an output:
pinMode(ledPin0, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, 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(ledPin0, HIGH);
delay(500);
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin2, HIGH);
delay(500);
// turn LED off:
digitalWrite(ledPin0, LOW);
delay(500);
digitalWrite(ledPin1, LOW);
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
} else {
// turn LED on:
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin0, HIGH);
delay(500);
// turn LED off:
digitalWrite(ledPin2, LOW);
delay(500);
digitalWrite(ledPin1, LOW);
delay(500);
digitalWrite(ledPin0, LOW);
delay(500);
}
}

3 Switchs and a RGB LED

What do you need:

  • Arduino
  • Breadboard
  • 1 RGB LED
  • 3 220 Ohm Resistors
  • 3 10 Kilohm Resistors
  • 3 switch
  • Wires

RGB LED that’s included in your Arduino kit has a common cathode (ground). You can read more about it here.

Here is what RGB LED pins in Arduino Starter Kit look like:

rgb-01

LED LAb_08_bb

Code:

// set pin numbers:
int buttonPin0 = 7; // the number of the first pushbutton pin
int buttonPin1 = 6; // the number of the second pushbutton pin
int buttonPin2 = 5; // the number of the third pushbutton pin

int ledPin0 = 13; // the number of the first LED pin
int ledPin1 = 12; // the number of the second LED pin
int ledPin2 = 11; // the number of the third LED pin

int buttonState0 = 0; //the first button state
int buttonState1 = 0; //the second button state
int buttonState2 = 0; //the third button state


// variables will change:

void setup() {
  // initialize the LED pins as an output:
  pinMode(ledPin0, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  // initialize the pushbutton pins as an input:
  pinMode(buttonPin0, INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop() {
  buttonState0 = digitalRead(buttonPin0); //read the first button state
  buttonState1 = digitalRead(buttonPin1); //read the second button state
  buttonState2 = digitalRead(buttonPin2); //read the third button state

  if (buttonState0 == 1) { //if the first button is pressed
    digitalWrite(ledPin0, HIGH); //turn on the first LED
  } else {                 //if the first button is not pressed
    digitalWrite(ledPin0, LOW); //turn off the first LED
  }
  if (buttonState1 == 1) { //if the second button is pressed
    digitalWrite(ledPin1, HIGH);//turn on the second LED
  } else {                 //if the second button is not pressed
    digitalWrite(ledPin1, LOW);//turn off the second LED
  }
  if (buttonState2 == 1) { //if the third button is pressed
    digitalWrite(ledPin2, HIGH);//turn on the third LED
  } else {                 //if the third button is not pressed
    digitalWrite(ledPin2, LOW);//turn off the second LED
  }

}