Arduino

Arduino Engineering
Arduino Engineering

So the perfect evolution from mobile device programming is arduino/microcontroller programming.

It’s basically moving from controlling software to controlling hardware.  Ever feel like coding apps is cool but you wish they could actually move things?

I started off with a basic arduino kit with motors and leds.

Arduino Engineering
Arduino Engineering

Of course I started out with the:

– LED blink (on-board)

– LED blink with breadboard

I went a little crazy and my head overflowed with ideas for projects.  I even made Arduino Pancakes!  Very tough btw.

Arduino Engineering
Arduino Engineering

 

Pretty soon I was getting shields and sensors, motors, displays and a soldering iron.

Arduino Engineering
Arduino Engineering

I quickly moved onto:

– LCD display

– DC motor

– Servo Motor

Arduino Engineering
Arduino Engineering

Then I started looking into making the base for a robot that would have red eyes like ultra (just saw the avengers) and voice recognition, mobility and cloud connected.  I started getting into RMF values for motors and then I decided to hack an old RC my kid had.  I figured how to make the wheels go and turn and I was set.

Arduino Engineering
Arduino Engineering

I moved on to the sensors.  I got a Grove shield and sensors for:

– Temperature and Humidity

– Light and UV

– CO2 and NO

– VHOCs

– Dust

Arduino Engineering
Arduino Engineering

At this point I got sidetracked into learning:

– Bluetooth HC-05

– TinySine WiFi shield

– Sainsmart GSM/GPRS SIM900

Arduino Engineering
Arduino Engineering

This is where I got a better grip on:

– Serial Communication (Hardware vs Soft)

– Software libraries

– Shields and their components

So I learned to configure the WiFly module (component in the TinySine Wifi Shield) and how to use the GPRS module.

I even had to throw in a little Android:

Android Engineering
Android Engineering

I must confess I did impulsively buy a SainSmart OLED display. I may add it to the robot in the future but it was really overkill.

So my “robot” will have mobility, data collection and wireless communication capabilities.  If I could just make it fly!

Arduino Engineering
Arduino Engineering

First Android App – Part 6

My First Android App

Android Studio Tutorial by Marcio Valenzuela Santiapps.com
Android Studio Tutorial

Now we are going to receive the input of this message and use the button to send it.

To do so, edit your Button declaration to look like this:

<Button

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”@string/button_send”

android:onClick=”sendMessage” />

We are simply telling it to respond to the onClick button action by calling the sendMessage method.

So we must declare this method in code, of course.  Open your MainActivity.java file and add the following:

/** Called when the user clicks the Send button */

public void sendMessage(View view) {

// Do something in response to button

}

We are declaring a public method that returns void, is called sendMessage and takes a View type to be referenced locally as view.

In order to do this you will need imports such as:

import android.view.View;

Now we are going to have this method declare what is called an Intent.  An intent is an action that we wish to carry out.  We do so by inserting this line into the method:

Intent intent = new Intent(this, DisplayMessageActivity.class);

Here we create a ‘new’ intent for this class to call the DisplayMessageActivity class and we assign it to an Intent type called intent! 🙂

Ok great, but what is this intent going to do?!  Add this code right below:

EditText editText = (EditText) findViewById(R.id.edit_message);

String message = editText.getText().toString();

intent.putExtra(EXTRA_MESSAGE, message);

First we get a reference to our EditText and assign it to a new variable called, rightly so, editText.  We get to our EditText view by finding the view through its id, which is edit_message.  Now we get that editText object and call its getText method concatenated to its toString method.  This converts the editText’s text value into a string.  We are assigning it to a String type variable called message.

Finally we call an intent object’s putExtra method to send that message variable along with an EXTRA_MESSAGE value.

You need 2 more imports here:

android.content.Intent

android.widget.EditText

Now let’s define the EXTRA_MESSAGE inside our MainActivity by adding this line right below the MainActivity public class declaration:

public class MainActivity extends Activity {

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

}

Now add the line that actually calls the new activity and your code should look like this:

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);

startActivity(intent);

}

Let’s create the second activity by right clicking on our java folder and selecting new Android Activity like so:

Screenshot 2014-02-08 15.49.32

Once again select a Blank Activity, the click Next.  Now fill in the following window as follows:

Android Studio Tutorial Adding New Activity by Marcio Valenzuela Santiapps.com
Android Studio Tutorial Adding New Activity

The Activity Name is self explanatory.  The layout file name is provided for you as are the others but remember that we are not using fragments.  So replace the fragment name again, with the same value as above in the Layout Name.  Leave the Title as is but for Hierarchichal Parent add in the name of the calling activity (MainActivity) preceded by your package name (which you can find in the AndroidManifest.xml in case you forgot.

Also remember to remove that fragment method created by default, Only If Its There!

Trim off some other unused stuff so that the final code looks like this:

public class DisplayMessageActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_display_message);

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case android.R.id.home:

NavUtils.navigateUpFromSameTask(this);

return true;

}

return super.onOptionsItemSelected(item);

}

}

We are doing the same thing here, which is call super.onCreate just to make sure something gets created.  Then we set the content view to be connected to this activity’s layout file.

Go to your strings.xml file and add a new string like so:

    <string name=”title_activity_display_message”>My Message</string>

Whenever we create a new activity we must declare it in the AndroidManifest.xml file.  Ours should now look like this:

<application … >

<activity

android:name=”com.example.myfirstapp.DisplayMessageActivity”

android:label=”@string/title_activity_display_message”

android:parentActivityName=”com.example.myfirstapp.MainActivity” >

<meta-data

android:name=”android.support.PARENT_ACTIVITY”

android:value=”com.example.myfirstapp.MainActivity” />

</activity>

</application>

Here we added the DisplayMessageActivity activity and its label as well as its parent.

So with our new activity created and the intent from the calling class, we now need to receive that intent in the new class.  Here is the code:

@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);

}

We again create an Intent object and get the message from the MainActivity.  We then get a TextView reference, set its text size and set its text property to that in the gotten message.  Finally we set the content view to what it needs to be now.

Voila!  Build & Run and enjoy your first android app.