Monday, September 1, 2014

Bubble Display, Shift Register and a TMP36 (Showing temperature on a tiny led display)

A few days ago a friend of mine lend me an SparkFun ProMicro. It's a 5v, 16mhz Arduino compatible development board that fits on a breadboard.

After i soldered its legs i had to run something cool on it, so i thought it would be fun to hook up an (also small) hp 7 segment bubble led display, but it's easy to connect it directly to the atmega pins, so i got it connected with 7 instead of 12 cables thanks to an 74HC595 Shift Register, just to add a little more complexity fun on this.

So, what is a shift register? it's a kind of buffer chip on which you load the state of the n-bits (8 in the case of the 74hc595), and then you say: "ok, show me what i wrote there", and the D0-to-D7 pins on the chip get HIGH or LOW, depending on which bitmask you stored earlier.

That's perfect for the 7 segments plus dot of the LED display, with 3 pins you decide the state of the 8 segments of the display.

But this Bubble display got 4 characters, so i still need 4 pins to control the 4 common cathodes of the display, and choose where to display each character. This could be controlled with another 595, but each one use 3 pins, and there was just 4 cathodes, so there wasn't much advantage on using another chip.

Here a pic of this super tiny led display:



I got the code from luiSantos instructables tutorial, with some modifications, some of them pointed out in the comments below that tutorial, where a guy posted a modified version that works with common cathode displays, like this little bubble display.

I got the bitmasks for the characters from SparkFun's SevSeg Library. on the ".h" file you can find the definitions for the character's segments on the display.

They are written as 0bABCDEFGX, where x is the decimal dot, which the display has for every character block, and A-to-G are the segments of the display on this form:

  aaa
f        b
f        b
f        b
  ggg
e       c
e       c
e       c
  ddd   X

The display needs to be continuously updated because only one character is displayed each time. I found 7msec to be a good refresh rate for this display, lower than this makes the display blurry (with 6msec got the decimal dot jumping between the sides or appearing twice), with 8msec it gets flickery and is hard to read on it.


The parts:

4x    330 ohm Resistor
1x    74HC595 Shift register
1x    HP QDSP-6064 Bubble 7 segments LED display
1x    TMP36 Analog devices Temperature sensor
1x    SparkFun's ProMicro Arduino compatible Board
1x    Breadboard (or 2 half-sized ones)
Alot  Jumper cables


This is the assembling of what i did with the parts:



There are a lot of cables, but just a few more than when i tried the display alone with the arduino, and now got me only 7 arduino pins instead of 12, so you can for example keep rx and tx serial pins (0,1) and some PWM capable pins on the Arduino UNO free for other tasks better than show your room's current temperature.


The Code looks like this:



/*
* created by Rui Santos, http://randomnerdtutorials.com
* modified by: Raphango
* Still more changes by: rccursach 2014
* Temperature Sensor Displayed on 4 Digit 7 segment common CATHODE
* 2013
* A Small portion from SparkFun's SevSeg Libray https://github.com/sparkfun/SevSeg
* SparkFun, Nathan Seidle, 2012 (Beerware license). Based on http://arduino.cc/playground/Main/SevenSegmentLibrary Dean Reading, 2012.
*/


const int digitPins[4] = {4,5,6,7}; //4 common CATHODE pins of the display.
const int clockPin = 10;    //74HC595 Pin 11
const int latchPin = 16;    //74HC595 Pin 12
const int dataPin = 14;     //74HC595 Pin 14
const int tempPin = A0;     //tmp36 temperature sensor pin

//As seen on SparkFun's SevSeg Library
const byte digit[10] =      //seven segment digits
{
  0b11111100, // 0
  0b01100000, // 1
  0b11011010, // 2
  0b11110010, // 3
  0b01100110, // 4
  0b10110110, // 5
  0b10111110, // 6
  0b11100000, // 7
  0b11111110, // 8
  0b11110110  // 9
};

int digitBuffer[4] = {0};
int digitScan = 0;
float tempC;

void setup(){               
  for(int i=0;i<4;i++)
  {
    pinMode(digitPins[i],OUTPUT);
  }
  pinMode(tempPin, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  Serial.begin(9600);
}


void loop(){
  float temp_v = (float)(analogRead(tempPin) * (5.0 / 1023.0));
  tempC = (100 * temp_v) - 50;

  printDisp(tempC, 1000); //params are: Float value, msec it will be displayed
}


void printDisp(float value, int msec){
  clearDisp();
  digitBuffer[3] = (int(value) % 1000)/10;
  digitBuffer[2] = (int(value) % 10);
  digitBuffer[1] = (int(value*10) % 10);
  digitBuffer[0] = (int(value*100) % 10);
  
  //Get it displayed until msec Milliseconds passed
  long ti = millis();
  long tf = millis();
  while(tf-ti < msec){
    tf = millis();
    updateDisp();
  }
}

//writes the temperature on display
void updateDisp(){
  for(int i=0; i<4; i++){
    clearDisp();
    digitalWrite(digitPins[i], LOW); //Changed to LOW for turning the leds on.
    
    if(i==2) //Add decimal dot
      shiftOut(dataPin, clockPin, LSBFIRST, digit[digitBuffer[i]] | 0b00000001);
    else
      shiftOut(dataPin, clockPin, LSBFIRST, digit[digitBuffer[i]]);
    
    digitalWrite(latchPin, HIGH);
    digitalWrite(latchPin, LOW);
    
    delay(7); //If not delayed, digits are seen brurry, if the value is 8 you migth see the display frickering.
  }
}

void clearDisp(){
  for(byte j=0; j<4; j++) {digitalWrite(digitPins[j], HIGH);} // Turns the display off. Changed to HIGH
}


Because this code is just demostrative, now i want to make a library for this little displays to be managed with the shift register, maybe auto-refreshed with a timer. If i got some time i will write it and put the link on this post as an update.

Now this is how it looks like:




After i put on my protoboard a 5v Step-up converter and 2 AA Batteries and got it running all night just because this little red numbers look pretty cool in the dark.

And that's it!

Monday, June 30, 2014

Object Oriented Arduino

As we all noticed, Arduino is not more than a bunch of (Pretty nice organized/integrated though) libraries (Arduino, Wiring, some LCDs, etc), bootloader, and the AVR gcc compiler.

But as i was programing some sketches for arduino, i noticed that using a lot of functions to try not repeat myself on the setup() loop() of the ".ino" kind of sketches, i was building a big pile of "spaguetti code"... it was harder to follow the program execution as the code was growing.

Then just out of curiosity, intrigued by how those 2 functions and the others functions and loose variables written on a ".ino" sketch file ended into a C/C++ source, i opened my arduino installation directory, and started searching for some kind of template or something where my sketches were included/inserted.

Arduino cores source files

Here, i said "hey! 'main.cpp', let's have a look at this". this is the content of this main.cpp:


#include <Arduino.h>

int main(void)
{
init();


#if defined(USBCON)
USBDevice.attach();
#endif

setup();
 
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
     
return 0;
}


Then, if you have a look to the Arduino.h file, there is some of the avr libs, arduino constants, functions and other definitions.

This means that you can actually write plain C/C++ code and include it, just like any other C++ program you have written before.

Though that is not a secret (that you can write objects), the small code examples we get from internet (which are never OO based examples) and the way the IDE is designed, aren't of any help when you want/need to work with a big (nor medium) number of classes and files.

Now, the arduino IDE interface is kind of ugly when you try to add more files to your project, i can't remember exactly why, but you can try and shure you'll say "mmmh, isn't another ide for arduino out there?". Yes, there are.

I'm now using SublimeText with the Stino plugin:

Stino and SublimeText
Stino and SublimeText


In the last image i show you the setup() and loop() functions of a sketch that sends through XBee the value of a Sensor and the values obtained from a GPS if the data is obtained successfully from it.

In this case, every sensor implements a common interface, so i can add more sensors or exchange them without much trouble.
In the first line of loop(), the object sh calls a method named read() which returns a string of data. This public method was inherited from the implementation of this code:

/*
* sensors.h
* This is a class definition for the Sensors interface.
* The list of virtual methods are common to all sensors
* to perform basic tasks.
*
* (C) Ricardo Carrasco Cursach 2014
*
* last modified: 16-03-14
* by: rccursach
*
*/

#ifndef   SENSORS_H
#define   SENSORS_H

#include <Arduino.h>

class Sensors{
  public:
    virtual void begin() = 0;
    virtual String read() = 0;
    virtual int getRequiredPins() = 0;
    virtual int getReqSerialSpeed() = 0;
    virtual ~Sensors() = 0;
};

#endif

If you like to do the same this is what you need:

Sublime text: http://www.sublimetext.com/2
Package Control for Sublime: https://sublime.wbond.net/installation
Arduino IDE http://arduino.cc/en/Main/Software
Install Stino plugin from Package Control.

I don't need to mention how this is useful to keep your code readability, maintainability, and all the other benefits of an object oriented approach in your arduino code.

Sunday, February 2, 2014

ruby2.0.0 (RVM) and Heroku on Ubuntu 13.10

Ruby2.0.0 (RVM) Rails and Heroku on Ubuntu 13.10

With my recently updated (K)Ubuntu 13.10 and a project we delayed until vacations, came up the decision of use Rails 4 to continue the development.

This is a web application we started as a Symfony2 but, as we're on vacations with the summer and all that, i decided make it fun, also we have like one month to go back.

What i'm going to do now is describe how i set up my dev environment for a base Web Application in heroku with Rails 4 and Ruby 2.0.0

Also, i used RVM onto a separate user to avoid break things with other code laying around on my laptop.

1. New User account  and RVM

As i mentioned before, i did create a new user account, this user account, just in case, was created into the sudo group, i came handy later :)

$ sudo adduser rubydev
...
$ sudo adduser rubydev sudo


right after i logged into "rubydev" account and installed RVM.
(it might be necessary to install "git" and "curl" packages through apt-get if they are not already installed)

$ \curl -L https://get.rvm.io | bash -s stable

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   184  100   184    0     0    125      0  0:00:01  0:00:01 --:--:--   125
100 20511  100 20511    0     0   7169      0  0:00:02  0:00:02 --:--:-- 59798
Downloading https://github.com/wayneeseguin/rvm/archive/stable.tar.gz

Installing RVM to /home/rubydev/.rvm/                                                                                                                                                           
    Adding rvm PATH line to /home/rubydev/.profile /home/rubydev/.bashrc /home/rubydev/.zshrc.
    Adding rvm loading line to /home/rubydev/.bash_profile /home/rubydev/.zlogin.
Installation of RVM in /home/rubydev/.rvm/ is almost complete:

  * To start using RVM you need to run `source /home/rubydev/.rvm/scripts/rvm`
    in all your open shell windows, in rare cases you need to reopen all shell windows.

# rubydev,
#
#   Thank you for using RVM!
#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.

In case of problems: http://rvm.io/help and https://twitter.com/rvm_io


Cool, now we got RVM, there should be some way that i don't know to make rvm command functional right after it's installed. but i just logged out and logged back in.

Gemsets

With RVM you can set up different gemset files and later choose wich gemset and ruby version use, we will get stick with the default gemset, because we just made and entire user just to test around Ruby2.0.0 and Rails. (On my normal/daily user account i got Ruby1.9.3).

Anyways, if you prefer to create a separate gemset you should type:

$ rvm use ruby-2.0.0-p353@heroku-expedit --create

(my gemset would be called heroku-expedit, name yours whatever you want)

With that you will create a gemset using ruby-2.0.0, and use the recently created gemset with your project.

For more on gemsets: http://rvm.io/gemsets

2. Installing Ruby and Rails

Now with "rvm list known" we can see the possible ruby versions we can install. (a lot).

$ rvm list known

# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-p374]
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p320]
[ruby-]1.9.3[-p484]
[ruby-]2.0.0-p195
[ruby-]2.0.0[-p353]
[ruby-]2.1.0
[ruby-]2.1.0-head
ruby-head

The list goes on with JRuby, Rubinius, MacRuby and others interpreters and versions of them, but we want just one of the MRI versions. we will take "ruby-2.0.0-p352", so:

$ rvm install ruby-2.0.0-p352

Checking requirements for ubuntu.
Installing requirements for ubuntu.
Updating systemrubydev password required for 'apt-get --quiet --yes update': 
...........
Installing required packages: gawk, libreadline6-dev, libyaml-dev, sqlite3, libgdbm-dev, libncurses5-dev, bison, libffi-dev.......
Requirements installation successful.
Installing Ruby from source to: /home/rubydev/.rvm/rubies/ruby-2.0.0-p353, this may take a while depending on your cpu(s)...


In the line where password was required, we tanks that we were sudoers, because RVM tried to install some libs into our system.

After a while we will get this message and our command line back:

Install of ruby-2.0.0-p353 - #complete
$ _



Now we can just install Rails from gem:

$ gem install rails
Fetching: atomic-1.1.14.gem (100%)
Building native extensions.  This could take a while...
Successfully installed atomic-1.1.14
Fetching: thread_safe-0.1.3.gem (100%)
Successfully installed thread_safe-0.1.3
Fetching: tzinfo-0.3.38.gem (100%)
Successfully installed tzinfo-0.3.38
Fetching: multi_json-1.8.4.gem (100%)
Successfully installed multi_json-1.8.4
Fetching: i18n-0.6.9.gem (100%)
Successfully installed i18n-0.6.9
Fetching: activesupport-4.0.2.gem (100%)
Successfully installed activesupport-4.0.2
Fetching: erubis-2.7.0.gem (100%)
Successfully installed erubis-2.7.0
Fetching: rack-1.5.2.gem (100%)
...
Fetching: bundler-1.6.0.pre.1.gem (100%)
Successfully installed bundler-1.6.0.pre.1
...
Fetching: rails-4.0.2.gem (100%)
Successfully installed rails-4.0.2
...
14 gems installed

$ _



Now we got installed Rails, Bundler, Activerecord, and all the usual. Still we need some other gems to get this working with Heroku.


3. Heroku gems and stuff

Next steps, more or less, follow this guide provided by Heroku Dev Center: devcenter.heroku.com - getting started with rails4

Mandatory gems

This two gems are necessary to get your application working with Heroku, "pg", and "rails_12factor".

"pg" gem is needed because Heroku will work with postgreSQL instead of SQLite3, because SQLite is not a production grade database engine, and Heroku provides postgreSQL databases as a service.

"rails_12factor" is needed to supply integration functions between Heroku and Rails, such as logging and static assets serving.

It's posible that compiling "pg" gem fails if "libpq-dev"  is not installed in our system (why would be installed anyways?), these are the headers and static libs for the PostgreSQL Library.

So, install the libpq5 libraries first:

$ sudo aptitude install libpq-dev

And then the gems:

$ gem install pg rails_12factor


Heroku toolbelt

As is written in their home page, this will install some tools neded to deploy your application to Heroku platform.

This will install on your computer the Heroku Client, a command line tool to manage your apps. Foreman, and Git. Foreman is an optional tool used to run your apps locally.

More details here: toolbelt.heroku.com

Now we will install the tools with:

$ sudo wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

Not shure if sudo is necessary because the script will ask you for your sudo password anyways.

The tools will get installed through apt automatically.

Now login into heroku with the CLI tool will help us manage our apps:

$ heroku login Enter your Heroku credentials. Email: mycoolname@mail.com Password (typing will be hidden): Could not find an existing public key. Would you like to generate one? [Yn] Y Generating new SSH public key. Uploading SSH public key /home/rubydev/.ssh/id_rsa.pub... done Authentication successful. $ _

And that's it. nex we will try an Example app.


4. Example App

From now we will assume you have installed a postgreSQL server to test/dump/whatever you database.

First we will create a test application:

$ rails new test_site --database=postgresql

We wait a while, in my case 'bundle install' keeps me waiting for a few minutes, so i went to the kitchen for a cup of tea in the meantime.

After the app is created we enter its directory with
$ cd test_site/

Now we need to create a root page as Rails 4 will no longer offer a static index page in production:

$ rails generate controller welcome index


And change the routing in our app so welcome#index is shown when we visit it, editing the routes file.

config/routes.rb
root 'welcome#index'


Preparing app to Heroku

Now we need to edit the database config file to remove the username from the database parameters, as we don't have any user named 'test_site'. (At least i don't).

config/database.yml
development:
  adapter: postgresql
  encoding: unicode
  database: test_site_development
  pool: 5
  #username: test_site
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: test_site_development
  pool: 5
  #username: test_site
  password:

production:

  adapter: postgresql
  encoding: unicode
  database: test_site_development
  pool: 5
  #username: test_site
  password:


To make a little test, i will create the databases and run Rails as web server:

$ rake db:create
$ rake db:migrate
$ rails s


Now in some browser y typed:  http://localhost:3000

This way we will take a look and check if everything goes fine.



I got this basic app, you can modify the welcome index view to something fancy if you want.



5. Deploying to Heroku


Final tuning

First thing we need to do before deploying to heroku is edit the Gemfile and add one of the bundles we talked a while ago, that are necessary to Heroku integration with Rails.

We will  take to birds in one shoot editing the Ruby version also at the end of the Gemfile.

./Gemfile
gem 'rails_12factor', group: :production
...
ruby "2.0.0"

then we need to run bundle install to generate a new 'Gemfile.lock':

$ bundle install


Upload

At this point, every thing would run just great. so we will upload it to Heroku, wich internally relies on Git for this task.

So, into the Rails project directory we will create a local repo and commit our files.

$ git init
$ git add .
$ git commit -m "init"


Then with the Heroku command line tool (assuming we are logged in) we will create the remote repo to upload our app:
$ heroku create

And push the changes to Heroku with git:
$ git push heroku master


Now we have deployed our code to heroku. :)


6. Config on heroku side

Heroku at the time this is written gives you computer time to run your application on a development context for free for one month, every month. Then you can pay and scale up on databases number, speed on a production environment and leave your application running there.

Their measurement unit are the 'Dyno-hours'. you are allowed to run your app at 1x dyno-hour, for long as 750 dyno-hours per month, which is enough to keep your app running for one month free (on a development context of course).

It's better explained here: devcenter heroku - billing

Once our code is on heroku if it needs a database we will need to create one on heroku side, to run any command on heroku we use 'heroku run'. As example we will create our databases.

$ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.2971

We can see our app and database on the dashboard on heroku site. yet, i won't upload images from the Heroku site. you can go and have a look at your own.

Now we can check if our app is running, and at how many dynos, we type:
$ heroku ps
=== web (1X): `bin/rails server -p $PORT -e $RAILS_ENV`
web.1: up 2014/02/02 02:00:33 (~ 40m ago)




We can see how our app is running on one 1x dyno, we can set it to 0, or maybe set it to 1 1x-dyno if it wasn't running because we already turned it down.
$ heroku ps:scale web=1
Scaling dynos... done, now running web at 1:1X.



How if we open our test site?

we can try this, or open our app from the dashboard in your heroku account. Heroku will provide us a url that can be changed later when/if we scale up to a production environment.

$ heroku open

Mine looks like this:



There are more detailed instructions on the Heroku devcenter articles, especially this one i already mention at the start of this entry: devcenter Heroku - Getting started with Rails 4



...And that's it. Now i will start coding this thing!



* Heroku is a registered mark (?) or belongs to Heroku, Inc. US. i'm not affiliated or associated in any way, and any coincidence on texts, descriptions, etc. might be product of the previous reading of their articles and are not intentional. As consequence, are not in any way an attempt of any kind of copyright infringement.