Count Time

 

 

 

 

 

Have you ever been curious how long time has went from previous loop to current loop?

or since Arduino has started?

You can use function, millis() or micros(). they returns time since arduino is started in millisecond or microsecond.

Try code below.

unsigned long time = 0;
unsigned long ptime = 0;

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

void loop(){
time = millis();
Serial.print("Time: ");
Serial.print(time);
Serial.print(" Time Lapse: ");
Serial.println(time - ptime);
delay(1000);
ptime = time;
}

Screenshot 2014-07-11 14.59.00

Screenshot 2014-07-11 14.58.58

Start Apache Ant – 2/2

This post is based on Tutorial: Hello World with Apache Ant

 

 

Now we will try Ant. Result will be same with last time, Start Apache Ant – 2/2, but much simpler.

Make Build.xml file in project’s main directory.

Screenshot 2014-06-16 15.50.09

 

 

paste below. It’ll be the most simple and basic form of Ant’s Build.xml file.

Screenshot 2014-06-16 15.51.58

<project>

    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="com.HelloWorld"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/HelloWorld.jar" fork="true"/>
    </target>

</project>

Then, command as below

ant compile
ant jar
ant run

Screenshot 2014-06-16 15.51.51

In Build.xml file, target names’ are used as options in command line after ant.

Each line works as command. like delete directory in line 4, make directory in line 8, Compile in line 9. executing Ant with target name as option will execute every line between <target … </target>. So on. You don’t need to make and write manifest file In Ant, manifest is included in build file as line 15~17.

You can keep these complicated long commands in build file and execute them with there target name.

 

 

It’s done but Let me do little more that is enhancing build file. try and check code below.

<project name="HelloWorld" basedir="." default="main">

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="com.HelloWorld"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>

 

See now the project has it’s name. Writing a Simple Build File

<property …> look like sort of variable and it needs ${…} around its name to be called. Property Task

Start Apache Ant – 1/2

This post is based on Tutorial: Hello World with Apache Ant

 

Today we’ll try Apache Ant.

Apache Ant is a software tool for automating software build processes. Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. For simple, Ant’s first advantage is the separation of source files and result files.

Before start it, you, of course, will need to install Apache Ant first.

 

And… again before we start Ant, let’s try to build HelloWorld project that its result files to be separated from source files. If you don’t want to try it but directly start Ant, go Start Apache Ant – 2/2.

I’ll use and empty Git initiated directory that I made last time, Create a Project in GitHub. Below is brief of it for people who tried it but didn’t keep the directory.

Screenshot 2014-06-09 17.01.55

 

We’ll make source directory, src, and package directory, com, in src and a java file, HelloWorld.java, in com. as below.

Screenshot 2014-06-09 17.03.06

 

open HelloWorld.java and write basic code as below.

Screenshot 2014-06-16 14.35.11

 

Compiling a java file in source directory from project directory will be little tricky.

As picture bellow, go project directory and make build directory, and classes directory in it. and compile as below command.

javac -sourcepath src -d build\classes src\com\HelloWorld.java

this command will compile HelloWorld.java in directory src\com and store resulting HelloWorld.class file in build\classes directory. Then excute it as below command.

java -cp build\classes com.HelloWorld

Screenshot 2014-06-09 17.05.05

 

class file is created as below.

Screenshot 2014-06-09 17.10.17

Now we will create jar file. jar file is archive file that contains one or number of java packages. This comes handy when your java program consist of number of packages and java files. In order to create jar file, you need Manifest file as below. It means executing jar file will execute  main function in com.HelloWorld.

MARK there must be an additional line in the end. So the total number of line is 2.

Screenshot 2014-06-09 18.36.57

 

make a directory jar in build directory and create jar file.

jar cfm build\jar\HelloWorld.jar Manifest.txt -C build\classes .

Execute it

java -jar build\jar\HelloWorld.jar

Screenshot 2014-06-09 18.37.51

 

All those things are what we have to do to separate result files from source files.

Let’s try Ant and see how things go simpler and easier.

Start Apache Ant – 2/2

Simple User Interface

We tried Hello World in Android last time, Hello World in AndroidHello World in Android in Command Line.

In this post, I’m going to build user interface as below. Original document here.

Screenshot_2014-05-24-11-10-20 Screenshot_2014-05-24-11-10-44 Screenshot_2014-05-24-11-10-50

 

 

 

Open the black activity project you made last time or just make new one.

 

1. Add interfaces(original document here)

Open res\values\strings.xml, then change as below.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_activity_display_message">My Message</string>
</resources>

Screenshot 2014-05-24 13.28.25

Open layout\fragment_main.xml, then remove everything and paste below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />
</LinearLayout>

Screenshot 2014-05-24 13.42.33

Open src\com.example.myfirstapp\MainActivity.java and

add

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

Screenshot 2014-06-02 16.38.24

and also add

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
}

Screenshot 2014-06-02 16.40.44

Then press ctrl + shift + o. All error will be solved.

 

2. Create second Activity(original document here)

Click New Icon on Toolbar and make Android Activity as below

Screenshot 2014-05-17 16.38.56
Screenshot 2014-05-17 16.38.50
Screenshot 2014-05-17 16.38.48

Open src/com.example.myfirstapp/DisplayMessageActivity.java and choose all onCreate function and delete.

Screenshot 2014-06-02 16.49.41

then paste

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
}

Screenshot 2014-06-02 16.50.01

Again,  press ctrl + shift + o to solve all errors.

It’s all done. Try build and upload.

[LM317] Adjustable Voltage Regulator

Hello We will try LM-317 Adjustable Voltage Regulator.

If you want to have voltage source but you don’t exactly know what voltage you need or it’s tend to vary. LM-317 can be a solution. It’s output current 1.5A will be enough for most of applications. You can simply change it’s output voltage with a potentiometer. check below

LM317 datasheet

LM317_001

 

It says Output-Adjustable up to 37V, but It’ll be limited by input voltage.

 

LM317_002

 

You will need two capacitors, 0.1㎌, 1㎌, one resistor 240Ω, one potentiometer.

LM317_003

I couldn’t really find 240Ω resistor so I used 500Ω potentiometer that is adjusted to 240Ω.

 

Arduino code would be like following.

void setup(){
  Serial.begin(9600);
}
void loop(){
  Serial.print(float(analogRead(0))*5/1024);
  Serial.println(" volt");
  delay(100);
}

LM317_004

 

 

 

After compile and upload. try to adjust output voltage.

LM317_005

LM317_006

[LD1117] Voltage Regulators

 

 

 

Hello Today we will try LD1117 Voltage Regulator.

It’s the one of most frequently used IC in digital circuit that drops to specific output voltage from some higher input voltage. There’re number of versions by designs and output voltages, mine is LD33cv that has output voltage 3.3v. Check below. those images are from its datasheet

LD1117 datasheet

LD1117_001

Make your circuit as below

LD1117_002

LD1117_004

LD1117_005

 

Arduino code would be like following.

void setup(){
  Serial.begin(9600);
}
void loop(){
  Serial.print(float(analogRead(0))*5/1024);
  Serial.println(" volt");
  delay(100);
}

LD1117_003

 

 

 

 

It’s not quite 3.3v but close enough.

LD1117_006

Add existing project to GitHub

We create a repository in GitHub last time, Create a Project in GitHub.

This time we’ll add a existing project to GitHub. Personally I’ll add the android project I made, Hello World in Android.

Same as last time, create a repository

Screenshot 2014-05-23 14.55.31

 

this time, the repository name will be same with the android project name.

Screenshot 2014-05-24 14.11.39

Screenshot 2014-05-24 14.12.00

It will be similar with creating local repository.

Go to project directory and make README.md, Initialize the project directory to Local repository.

Last it was only README.md but now there’re too many file to add one by one. Luckily there is a option to add all files.

git add .

Screenshot 2014-05-24 14.18.13

Commit like last time.

Screenshot 2014-05-24 14.18.50
Screenshot 2014-05-24 14.18.57
Screenshot 2014-05-24 14.19.07

 

 

Connecting Local and GitHub repository and Push is also same.

Screenshot 2014-05-24 14.20.08
Screenshot 2014-05-24 14.23.55

After Push is done, go to GitHub, you can see your local repo is pushed to GitHub repo

Screenshot 2014-05-24 14.48.54

Create a Project in GitHub

In post Start GitHub, we made our account.

Now let’s create a project in GitHub.

1. Create a Repository in GitHub

go to GitHub.com. On top right, click + then New repository.
Screenshot 2014-05-23 14.55.31

Type repository name and maybe Description. leave rest and click Create repository.
Screenshot 2014-05-24 14.26.55

GitHub gives you instruction.
Screenshot 2014-05-24 14.27.03

2. Create a Local repository

Git Shell is command prompt that comes with GitHub for window. It must be in your desktop or start menu.
Screenshot 2014-05-22 15.52.04
It will start from your GitHub directory. Go and Make a directory that you’d like to start your project. You can make the directory in GUI or type as below

mkdir myproject

Then make a readme.md file. README.md file is in workspace at this point

touch README.md

Initialize the directory as a Workspace.

git init

Add README.md file to Index. Now README.md will be added to Index from workspace.

git add README.md

Screenshot 2014-05-24 14.30.25
Index is sort of middle point in order to commit files to your Local Repository. Those concepts are quite difficult. You may can understand it as including or excluding some stuffs in or out of angle before you take photo, a snapshot. those files are in angle but photo is no taken yet. just don’t get headaches, you don’t need to understand it now. just remember files are in Index, a middle point.

Now commit it

git commit -m "First commit"

Screenshot 2014-05-24 14.30.57
Commit is an action that we are going to do ultimately. Through Commit, current state of project, a snapshot, is stored to Local repository and you can revert to this point any time. It’s done at this point unless you don’t want to share or back-up your repository.

If you want to share or back-up it, then you have to connect your Local repository to GitHub repository. Most of manual says as

git remote add origin https://github.com/crashcjw/myproject.git

but it didn’t work for me as below. If it works for you? you can proceed. If it didn’t try

git remote set-url origin https://github.com/crashcjw/myproject.git

Screenshot 2014-05-24 14.32.40
Push Local repository to GitHub repository

git push -u origin master

It’ll take some time. now everything are done.

The picture below is going to be useful to understand each stages and steps done

Take stairway with BMP180

I posted [BMP180] ATMOSPHERIC PRESSURE SENSOR last time.

I was thinking about some application and here it is.

 

 

20140513_164215

 

In the example code, paste below

at the end of setup() function

for(int i = 2; i < 10; i++){

pinMode(i,OUTPUT);

}

at the end of loop() function

for(int i = 2; i < 10; i++){

if(int(altitude)-196 == i – 4){

digitalWrite(i, HIGH);

}

else{

digitalWrite(i, LOW);

}

}

the number 196 has too be changed by your places elevation.

 

 

 

[BMP180] Atmospheric Pressure Sensor

BMP180_1

BMP-180 is an atmospheric pressure sensor that we can use to measure altitude, within a foot difference error, because atmospheric pressure is inverse proportional to altitude. According to it’s datasheet, BMP-180 can measure from 300 to 1100 hPa in step of 0.01 hPa with ±0.02 hPa error( 9000m to -500m in step of 0.08m with ± 0.17 m error in altitude) But atmospheric pressure changes through days and seasons. BMP 180 needs calibration quite often.

BMP-180 also can measures temperature from -40 to 85℃ in step of 0.1℃.

 

 

BMP-180 has I2C interface. Connect as below.

20140513_183728

Red – 3.3V to Vin

Purple – GND to GND

Yellow – SCL to SCL

Green – SDA to SDA

 

Let’s get the library.

you might remember or tried MPU-6050 in my blog. it’s about same.

1. download i2c development library

dirct download : https://github.com/jrowberg/i2cdevlib/archive/master.zip

GitHub : https://github.com/jrowberg/i2cdevlib

(If you don’t know how to GitHub, circuitholic.wordpress.com/2014/04/13/start-github/)

2. Copy the downloaded folder [i2cdevlib] to your [documents\arduino\libraries] folder.

3. Launch Arduino IDE and click [File]-[Example]-[i2cdevlib]-[arduino]-[bmp085]-[examples]-[bmp085_basic]

4. Drag BMP085.h and BMP085.cpp file in BMP085 folder to Arduino IDE.

5. Compile and Upload.

 

Screenshot 2014-05-13 18.36.11

T/P/A (; Temperature/Pressure/Altitude(m)) is the order of serial transfer. so

It’s 23.8 ℃, 989 hPa, 197 m. which is about correct, amazing for me.

Wikipedia says my city’s elevation is 199m.