Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

1/22/2014

(Arduino Study) the method to make library

Original source code is like that

---
//sos_without_lib.pde
void dot();
void dash();

int pin = 13;

void setup()
{
  pinMode(pin, OUTPUT);
}

void loop()
{
  dot(); dot(); dot();
  dash(); dash(); dash();
  dot(); dot(); dot();
  delay(3000);
}

void dot()
{
  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
  delay(250);
}

void dash()
{
  digitalWrite(pin, HIGH);
  delay(1000);
  digitalWrite(pin, LOW);
  delay(250);
}

...


To make library, we have to do 4 steps process,

step 1, write .h , .cpp files,   it is same c++ class, and copy into libraries\morse folder.
step 2, make keywords.txt,  and copy into libraries\morse folder.
step 3, make SOS.pde and copy libraries\morse\Examples\SOS
step 4, programing using the library

.h, .cpp

...
.h ->
#ifdef Morse_H
#define Morse_H

#include "WProgram.h"

class Morse{
  public:
    Morse(int pin);
    void dot();
    void dash();
  private:
    int _pin;

#endif

.cpp ->
#include "WProgram.h"
#include "morse.h"

Morse::Morse(int pin)
{
  pinMode(pin, OUTPUT);
  _pin = pin)
}

void Morse::dot()
{
  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
  delay(250);
}

void Morse::dash()
{
  digitalWrite(pin, HIGH);
  delay(1000);
  digitalWrite(pin, LOW);
  delay(250);
}
---



...
//SOS.pde
#include < Morse.h>

Morse morse(13);
void setup(){}

void loop(){
     morse.dot();morse.dot();morse.dot();
     morse.dash();morse.dash();morse.dash();
     morse.dot();morse.dot();morse.dot();
     delay(3000);
}

---


so the directory is like that

\arduino-0022\libraries\Morse\keyword.txt,  Morse.cpp, Morse.h
\arduino-0022\libraries\Morse\Examples\SOS\SOS.pde


1/14/2014

(Arduino Study) Led on/off using piezo speaker knock.




source code


...
const int ledPin = 13;
const int knockSensor = A0;
const int threshold = 100;

int sensorReading = 0;
int ledState = LOW;

void setup(){
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  
}

void loop(){
  sensorReading = analogRead(knockSensor);
  Serial.println(sensorReading);
  
  if(sensorReading >= threshold)
  {
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
    Serial.println("Knock!");
  }
  
  delay(100);
}
---

1/13/2014

(Arduino Study) temperature sensing

Temperature sensing  using thermister
Thermister gives resistance value depend on temperature changing.







...
#include < math.h>

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

double Thermister(int RawADC){
  double Temp;
  Temp = log(((10240000/RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;  
  
  return Temp;
}

void printTemp(void){
  double fTemp;
  double temp = Thermister( analogRead(0) );
  
  //read sensor value
  Serial.println("Temperature is:");
  Serial.println(temp);
}

void loop(void)
{
  printTemp();
  delay(1000);
}
---

1/10/2014

(Arduino Study) LED control using potentiometer





...
int potPin = 0;
int ledPin = 13;

int val=0;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop(){
  val = analogRead(potPin);
  Serial.print(val);
  Serial.print("\n");
  digitalWrite(ledPin, HIGH);
  delay(val);
  digitalWrite(ledPin, LOW);
  delay(val);
}
--- LED brightness control using potentiometer ...
int potPin = 0;
int ledPin = 12;

int val=0;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop(){
  val = analogRead(potPin);
  Serial.print(val);
  Serial.print("\n");
  analogWrite(ledPin, val/4);
}
---

1/08/2014

(Arduino Study) Led on/off duration changed by value of brightness is checked by potentiometer

the simple source code

---
int sensorPin = A0;
int ledPin = 13;
int ledBrightnessPin = 12;
int sensorValue = 0;

void setup(){
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}


void loop(){
  sensorValue = analogRead(sensorPin);
  
  Serial.print(sensorValue);
  Serial.print("\n");
  digitalWrite(ledPin, HIGH);
  delay(sensorValue);
  digitalWrite(ledPin, LOW);
  delay(sensorValue);
}
...




1/07/2014

(Arduino study) LED brightness change by pwm ( analogWrite function )

A analogWrite function generate PWM pules easy.
It can use to a motor control.




very very simple source code.
---
int ledPin = 10;

void setup(){
}

void loop(){
  //increase brightness step by +5
  for(int fadeValue = 0; fadeValue < 256; fadeValue +=5)
  {
    analogWrite(ledPin, fadeValue);
    delay(30); //wait 0.01sec
  }
  
  //decrease brightness step by -5
  for(int fadeValue=255; fadeValue >=0; fadeValue-=5)
  {
    analogWrite(ledPin, fadeValue);
    delay(30); //wait 0.01sec
  }
}
...

1/05/2014

(Arduino study) 피에쑰 μŠ€ν”Όμ»€λ‘œ μ†Œλ¦¬λ‚΄κΈ° ( piezo speaker make a sound )

simple sound source code

generate 1000hz, 1000 duration sound


---
int speakerPin = 10;

void setup(){
  
}

void loop(){
 
 tone(speakerPin, 1000, 1000); //5000hz, duration 1sec
 delay(2000); //rest 2sec
}
---


---
#include "pitches.h"

int speakerPin = 8;
int melody[]={
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, 
  NOTE_B3, NOTE_C4};
  
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
  
void soundGo(){
  for(int thisNote = 0; thisNote < 8; thisNote++){
    //melody repeat
    int noteDuration = 1000/noteDurations[ thisNote ] ;
    tone(speakerPin, melody[thisNote], noteDuration);
    //delay between sounds
    int pauseBetweenNotes = noteDuration * 1.30;
    delay( pauseBetweenNotes);
    noTone(8);
  }
}

void setup(){
  
}

void loop(){
  soundGo();
}

---
In the demo video, we can know principle of piezo speaker that is sounded by vibration.


pitches.h source code is here
---
/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

...

12/23/2013

(Arduino Study) Arduino LED on/off by Serial communication

I'm sorry very easy~!



(Arduino Study) Arduino serial communication and led on/off on the board(pin 13)

Very Easy and Fun.
Later, Serial Communication window can be used by debug window~~

digitalWrite(12, HIGH); -> This code is the function to use pull up register.





12/13/2013

(Arduino study) LED brightness control by switch connection

The source code is combine of Brightness changing(http://feelmare.blogspot.kr/2013/12/arduino-study-led-brightness-changing.html) and LED turn on/off(http://feelmare.blogspot.kr/2013/12/arduino-led-onoff-using-switch-aduino.html).


The LDE brightness is changed when switch is connected.

////
const int LED=9;
const int BUTTON = 7;

int val = 0;

int old_val = 0;
int state = 0;

int brightness = 128;
unsigned long startTime = 0;

void setup(){
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
}

void loop()
{
  val = digitalRead(BUTTON);
  
  if( (val == HIGH) && (old_val == LOW) ){
    state = 1-state;
    startTime = millis();
    delay(10);
  }
  
  
  if( (val == HIGH) && (old_val==HIGH) ){
    
    if(state == 1 && (millis() - startTime) > 500 ){
      
      brightness++;
      delay(10);
      
      if(brightness > 255){
        brightness=0;
      }
    }
  }
  
  
  old_val = val;
  if(state == 1)
  {
    analogWrite(LED, brightness);
  }
}
////





(Arduino Study) LED brightness changing, using analog pin-port, (analogWrite function)

'analogWrite' output value's range is 0~255.
In the source code, 'i' is changed 0 to 255 and 255 to 0.
So LED brightness would be changed.


//////
const int LED = 9;
int i=0;

void setup(){
  pinMode(LED, OUTPUT);
}

void loop(){
  
  for(i=0; i< 255; i++){
    analogWrite(LED, i);
    delay(10);
  }
  
  for(i=255; i> 0; --i)
  {
    analogWrite(LED, i);
    delay(10);
  }
}

//////






12/11/2013

Arduino, LED On/Off using switch, (Aduino study),

Today, I studied about input signal.
So, my arduino can turn on/off LED by switch.
In the picture, black wire works the role of switch.






///
const int LED = 13;
const int BUTTON = 7;

int val = 0;
int old_val = 0;
int sw_OnOff=0;

void setup(){
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
}


void loop(){
  val = digitalRead(BUTTON);
  
  if((old_val==LOW) && (val==HIGH)){
    sw_OnOff=1-sw_OnOff;
    delay(100);
  }
  
  old_val = val;
  
  
  digitalWrite(LED, sw_OnOff);
  
}
////


But I still don't know why it need register and why the wire connect with there??
But, it is very fun~

12/10/2013

avrdude stk500_recv() programmer is not responding Error ( simple solution in my case.. ), Arduino beginner

I meet the error message when I upload source code to the board.


In my case, I set correct board type, at the tools->board menu.
On the Mac, you can see your board type at the system information application.


Change you correct board type and try again.

In my case, communication port is like this picture.




12/26/2012

My first Arduino art (5led on/off example source)

The advantage of Arduino is beginners can access electrical and electronic experiment easily.
I think the reason is the arduino support abundant examples  and functions.
Nervertheless I don't know ATmega, I could led on/off easily.

This is demo video and picture.
To experiment my case, you need arduino board, resistance 220om and led.

And to setup your arduino is explained very well on the homepage.
http://arduino.cc/en/Guide/HomePage
http://arduino.cc/en/Guide/Windows





Thank you.



source code
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int mode=0;
int order=1;
int count=0;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led-1,OUTPUT); 
  pinMode(led-2,OUTPUT);
  pinMode(led-3,OUTPUT);
  pinMode(led-4,OUTPUT);
      
}

// the loop routine runs over and over again forever:
void loop() { 

  if(mode == 0)
    count = count+1;
  else
    count = count-1;

  for(int i=0; i< 5 i=i+1) {
    if(count%5 == i)
      digitalWrite(led-i, HIGH);   // turn the LED on (HIGH is the voltage level)     
    else
      digitalWrite(led-i, LOW);   // turn the LED on (HIGH is the voltage level)   
  }
  delay(200);

  if(mode == 0)
  {
    if(count == 50)     
      mode=1;
  }else{
    if(count == 0)
      mode=0;   
  } 
}