วันเสาร์ที่ 11 พฤษภาคม พ.ศ. 2556

processing basic


Buttons, Switches, LEDs



We'll start our tutorial with three simple light circuits. In the first one, the LED is permanently on. In the second, the LED only lights up when a button is pressed and a circuit is completed. In the third example, we'll replace the manual switch with an Arduino pin (set to output mode), so we can control the LED from our program. Here are three simple circuits to build on your breadboard:

#1: Power an LED from a battery (always on)

First, take the shield of your Arduino board and set the power jumper to the right, so the board will draw power from the power jack instead of the USB bus:
Arduino power jumper
Put the shield back on.

Build the following circuit on your breadboard. Use a 220Ohm resistor (red red brown gold) [confused which reshandy resistor calculator here].

Once you have checked it, connect the 9V battery to your board. The LED should light up and stay on. IF it doesn't you probably need to switch the orientation of the LED. The longer lead, the anode, should be connected to the Resistor, the shorter, cathode, to ground.
IMG_0004
IMG_0005

#2: Make a light switch

Next, we'll insert a switch into the circuit. The momentary switches in your kit are "normal open", meaning that the circuit is interrupted in the idle state, when the switch is not pressed. Pressing the switch closes the circuit until you let go agagin.

IMG_0007
IMG_0006

Optional: If you're ahead, replace the momentary switch with the rocker switch in your kit. or change the circuit so the light is on by default and goes off when you press the momentary switch.


#3: Toggling LED with a mouse button


In the third example, we'll replace the manual switch with an Arduino pin (set to output mode), so we can control the LED from our program. The safe way to do this is to let the Arduino pin sink current - if we toggle the pin low, it acts as ground and current flows through the resistor and the LED as it did in the previous examples. When we take the pin high, to 5V, there is no potential difference and no current flows - the LED stays off.

Since we're now communicating with a computer, we can take power from USB. Disconnect the 9V battery. Remove the shield. Move the power jumper to the left, so the board will draw power from USB.

IMG_0003

I've chosen to connect to pin 2 in this example. You cannot use pins 0 and 1, since they are also connected to the USB serial port that Arduino uses to communicate to Processing or Flash. You may pick a different pin - you'll have to be dilligent to also reflect that change in the code we use to control the pin.

Here's the breadboarded circuit:
IMG_0010
IMG_0011

The code for this example is led_control_01.


Processing: led_control_01.pde
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int ledPin = 2;
void setup() {
size(200, 200);
arduino = new Arduino(this, Arduino.list()[1], 57600);
arduino.pinMode(ledPin, Arduino.OUTPUT);
arduino.digitalWrite(ledPin, Arduino.HIGH);
}
void draw() {
if (mousePressed == true) {
arduino.digitalWrite(2,Arduino.LOW);
} else {
arduino.digitalWrite(2,Arduino.HIGH);
}
}
Note:Sometimes, you'll have to replace the index into Arduino.list()[] with an appropriate setting for your computer. How do you find out what to use? Run the test script you were assigned for homework that prints out a list of serial ports on your computer. On the PC, you'll usually want to use the number listed next to the highest COM port. If that doesn't work, you'll have to find out which COM port the Arduino board is listed as by going to the Device Manager and looking through the port list (Bjoern can demo this). On the mac, look for an entry like this "/dev/tty.usbserial-A1001NTo" and use the index of that entry.

Optional: If you are ahead, change your code so the light stays on when you press the mouse button, and stays off when you press it again. After that, change your code so the light blinks on/off. Then, have the mouse button switch the light between on and blinking.



#4 Sensing buttons in software

We've used code to trigger output - what about the other direction, sensing physical input in code? Just as easy. Here is a simple switch circuit; we're using Digital I/O pin 2 again, as input this time:

When the switch is open, the Arduino pin (set to input mode) is pulled to 5V - in software, we'll read Arduino.HIGH. When the switch is closed, the voltage at the Arduino pin falls to 0V (because the resistance through the switch is essentially 0 Ohms - see VoltageDividers) - in software, we'll read Arduino.LOW. The pull-up resistor is used to limit the current going through the circuit. In software, we can check the value of the pin and switch between graphics accordingly.
IMG_0012
The corresponding code is in example switch_input_01:
Processing: switch_input_01.pde
/*
* background color changes depending on switch state
*/
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int switchPin = 2;
void setup() {
size(200, 200);
arduino = new Arduino(this, Arduino.list()[1], 57600);
arduino.pinMode(switchPin, Arduino.INPUT);
}
void draw() {
if(arduino.digitalRead(switchPin)==Arduino.LOW) {
background(255, 0, 0);
} else {
background(0, 0, 0);
}
}

#5 Fading LEDs (optional)

What about those "breathing" LEDs on Mac Powerbooks? The fading from bright to dim and back is done using pulse-width modulation (PWM). In essence, the LED is toggled on and off rapidly, say 1000 times a second, faster than your eye can follow. The percentage of time the LED is on (the duty) controls the perceived brightness. To control an LED using PWM, you'll have to connect it to one of the pins that support PWM output - 9, 10 or 11 on the Arduino. Then use the analogWrite(pin,value) command to set duty between 0 and 255. In this example, I've connected the LED to pin 9.

IMG_0013

Here is the corresponding code example is led_control_05
Processing: led_control_05.pde
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int pwm=0;
int ledPin=9;
boolean rising=true;
void setup() {
size(200, 200);
arduino = new Arduino(this, Arduino.list()[1], 57600);
arduino.pinMode(ledPin, Arduino.OUTPUT);
arduino.analogWrite(ledPin, pwm);
}
void draw() {
arduino.analogWrite(ledPin, pwm);
println(pwm);
if(rising) {
pwm+=2;
if(pwm>=255) {
rising=false;
}
}
else {
pwm-=2;
if(pwm<=0) {
rising=true;
}
}
}
 
ref: http://transformativedesign.pbworks.com/w/page/22424426/Arduino%20Tutorial%202%3A%20Switches%20and%20LEDs

TIP เล็กแต่อย่างมองข้าม INPUT OUTPUT Arduino


วันพฤหัสบดีที่ 9 พฤษภาคม พ.ศ. 2556

Do You Have The Time? DS1307 RT Clock + Arduino

Most microcontrollers are time-agnostic, meaning they are unaware of the time around them, but that’s ok as most things we make have no need for it. But… every once in awhile you come up with an idea that requires knowing the actual time. Mostly this is used for data-logging, or creating this year’s newest clock. (Why do we come up with harder ways to tell time?) But whatever your reason, we have a solution: The DS1307 RTC (Real Time Clock).
For this article we will be discussing and using Sparkfun’s RTC Module because it incorporates everything you need to make it work, including a backupbattery good for a minimum of 9 years. It also comes with the time preset so you may not even need to configure it when you get it.

Why write this article?

If you look around, this chip has been used for years, and there are dozens of tutorials on using it. So why write another? I read a lot of them and they all had slightly different information, and most of them talked about the chip without the crystal or other needed addons. So im writing this specifically about using Sparkfun’s RTC module version, and specifically for use on an Arduino.

About

The DS1307 is actually a very simple I2C chip. It just gives you back 7 bytes of information that is the time. So all we need to do is receive it … and transcode it to decimal. “Transcode it to decimal?!?” Yeah, don’t worry, it sounds scary, but it’s not. See the DS1307 encodes all the data in “Binary Coded Decimal” or BCDBCD is just a way to encode numbers, and works by encoding each digit in 4 digits of binary… like so:
157 = 0001 0101 0111
1 = 0001
5 = 0101
7 = 0111
Normally you would say that 157 = 10011101. But that is 157, BCD encodes each digit separately. 1, 5, 7 not 157. hence 0001, 0101, 0111 not 10011101.
Ok, now that we know we will be receiving the data as BCD, we just need a way to convert that to decimal. Luckily, John Boxall over at TronixStuff(incredibly awesome site) has a pretty simple function to do that for us. Lucky for me too because my method was much more complicated.


















Time Shifting

Hooking it up

Code

//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include "Wire.h"
#define DS1307_ADDRESS 0x68

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

void loop(){
  printDate();
  delay(1000);
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);

  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}

RUNNING THE FOLLOWING CODE WILL RESET THE TIME
You will need to set the correct time in the setDateTime function – I did it this way to be a little cleaner looking. Also because the time is set at setup(), it will set the time to that value every time the Arduino restarts. So after you have set it, either upload the above code, or comment out setDateTime from setup, and re-upload the sketch.
If you need to change the time a bunch, or just make it easy, you could set it up so you can change it via the serial terminal. That could be cool!
//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527


void setup(){
  Wire.begin();
  Serial.begin(9600);
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
  printDate();
  delay(1000);
}

void setDateTime(){

  byte second =      45; //0-59
  byte minute =      40; //0-59
  byte hour =        0; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    1; //1-31
  byte month =       3; //1-12
  byte year  =       11; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}






วันศุกร์ที่ 3 พฤษภาคม พ.ศ. 2556

รวมข้อมูล digitalและ arudino อื่นๆ ของ รศ.ณรงค์ บวบทอง https://sites.google.com/site/eplearn/home