Marcel's Blog

Endless experimentation

  • I am using

    Firefox Download Button
  • Readers

    • 35,475 Visits
  • Archive

  • Ce-i cu blogu’ asta ?

    Details at "About" Page. Postez pe acest blog pur si simplu fiindca imi place si sunt dornic sa impartasesc cu toata lumea experimentele mele, filmarile, chestiile DIY si multe alte lucruri. Ma simt eu mai bine daca o fac :). Blogul este in engleza, fiind o limba de circulatie internationala. Cred ca majoritatea va descurcati sa o intelegeti ;). Details at "About" Page.
  • Calendar Blog

    May 2012
    M T W T F S S
    « Apr    
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  
  • Meta

  • Visitors

    free counters Since 26th June 2010. Total visits are displayed up

Posts Tagged ‘programming’

Visualizing temperature as color using an RGB led, a LM35 sensor and Arduino

Posted by sjackm on 26 March 2012

I decided this weekend to work on a mini-project, since it came to my mind during the last week.

I found that my Arduino starter kit actually had an LM35 temperature sensor, it’s just that I noticed that quite late. The LM35 is a sensor that varies its ouput voltage proportionally to the temperature and it’s specially designed for the Celsius scale. The limits of what it can sense is from -55°C to 150°C. You can read more about it if you want from its datasheet.

When I set out to make this mini-project work, I thought it would be really simple to program it. And simple it was, until I had a couple of *facepalm moments.

I wanted to make the LED shine from blue to green, to yellow then red as the temperatures increases. I believe there are probably better methods of accomplishing this, but my logic told me to this:

  • the blue led will be at maximum brightness if the temperatures drops bellow 0°C, and will gradually decrease in intensity from 0°C to 45°C.
  • the green led will increase its brightness from 15°C to 35°C and then will gradually decrease until 75°C
  • the red led will increase its brightness from 45°C to 90°C, and will stay lit after 90°C

That was apparently simple until I opened the Arduino IDE. First of all, I used some code I found on this blog. I was mainly interested on the voltage-to-celsius conversion part, and I build upon that.

The problem was that I had no idea how to correctly decrease the intensity. At first, I instinctively implemented something like the function f(x)=1/x, but that obviously didn’t work. I then figured out that I should use something like f(x)=a-x, where a is a constant defined by me. Using the blue pin as an example, “a” would have been 25. So at 0°C I would have f(o)=25-0=25. At 24°C f(24)=25-24=1. But then that obviously needs to be converted to a value between 0 and 255, because that’s how PWM works on Arduino.

But then somebody suggested I use the map function with reversed values here. That was a glorious *facepalm moment. It was that simple! All I had to do was: map(tempC, 0, 25, 255, 0);

It’s not a perfect code, it’s not the perfect solution, but it works. Here’s the code:

float tempC;
int tempPin = 0;
int redPin= 3;
int greenPin= 5;
int bluePin= 6;

int blueTemp= 0; int greenTemp= 0; int redTemp= 0;

void setup()
{
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}

void loop()
{
tempC = analogRead(tempPin);           //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature
Serial.println((byte)tempC);             //send the data to the computer

if(tempC<0){
analogWrite(bluePin, 255);}
else if(tempC>0&&tempC<=45){
blueTemp= map(tempC, 0, 45, 255, 0);
analogWrite(bluePin, blueTemp);}
else if(tempC>45){
analogWrite(bluePin, 0);}

if(tempC<15){
analogWrite(greenPin, 0);}
else if(tempC>15&&tempC<=35){
greenTemp = map(tempC, 15, 35, 1, 254);
analogWrite(greenPin, greenTemp);}
else if(tempC>35&&tempC<=75){
greenTemp = map(tempC, 35, 75, 255, 0);
analogWrite(greenPin, greenTemp);}
else if(tempC>75){
analogWrite(greenPin, 0);}

if(tempC<45){
analogWrite(redPin, 0);}
else if(tempC>=45){
redTemp= map(tempC, 45, 90, 1, 255);
analogWrite(redPin, redTemp);}
else if(tempC>90){
analogWrite(redPin, 255);}

delay(200);                           //wait 200 ms before sending new data
}

Now the delay was set to 200ms, and I didn’t really bother with it. Perhaps setting it to something lower like 100ms would produce a more fluent change in the led’s color.

Posted in DIY electricity | Tagged: , , , , , , | 4 Comments »

HTML Color Table Generator

Posted by sjackm on 20 October 2011

When I’m bored I can spend my time creating weird, pointless things. Such as this HTML Color Table generator. What does it do?

Well, it takes only 2 inputs: the number of rows and numbers of colons. It then creates (rows)*(colons) number of cells, each cell being a different color( might be even unique but I guess there’s a high chance 2 cells will have the same color). Bonus: each cell is numbered! :) )

It’s a simple console C++ program that makes use of the randomization function( well, computers have a pseudo-randomization algorithm but it works…).

Here’s how the program looks:

And here’s how the output .html file looks like for the above numbers(10 rows and 20 colons):

Meh, doesn’t have a point right? I made it just for fun :P

If you want you can download the program here (I think it only works under Windows).

And here’s the source code if you’re interested:

// uses style=”background-color:rgb(r,g,b)” and rand()%256 for generating the color

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <fstream>
using namespace std;

int main(){

srand(time(NULL));
ofstream table(“colortable.html”);
int rows, colons; int i,j, r,g,b;
int number=0;

cout<<”WARNING! DO NOT IMPUT HIGH NUMBERS! (number of cells = rows*colons!) => Will take forever to generate…”<<endl<<endl<<”It is also recommended to avoid a big number of colons to avoid horizontal scrolling!”<<endl<<endl;

cout<<”Input the number of <<rows>> the color table will have:”<<endl; cin>>rows;
cout<<endl<<”Input the number of <<colons>> the color table will have:”<<endl; cin>>colons;
cout<<endl<<”We are ready to start!”<<endl;

table<<” <html><html><head><title>Random Color Table</title></head><body><table border=’1′ align=’center’>”;

for(i=0; i<rows; i++){
table<<”<tr>”;
for(j=0; j<colons; j++){
table<<”<td style=’background-color:rgb(“<<rand()%256<<”,”<<rand()%256<<”,”<<rand()%256<<”)’>”<<number<<”</td>”;
cout<<”Cell number”<<number<<endl;
number++;
}
}

table<<”</table></body></html>”;

return 0;}

Have fun!

Posted in Internet, Programare | Tagged: , , , , , , , | 4 Comments »

Allegro 5 Video Beginner Tutorials

Posted by sjackm on 29 August 2011

At last I have found some good tutorials to follow. After spending the whole night searching for some good Allegro 5 tutorial series and only finding ‘Allegro Tutorial Part 5′ tutorials for Allegro 4.2.3 X(, I am now quite wasted and unable to follow any instructions to learn something. My eyelids are of lead…

But, for future reference and for whoever will stumble upon my blog post, here’s the link:

http://fixbyproximity.wordpress.com/2011/07/13/2d-game-dev-your-first-allegro-5-program/

The tutorials are explained for Microsoft’s IDE, Visual Studio. The guy works with the professional version, but the express version works as well. Now, because I enjoy Code::Blocks I will also search for a Code::Blocks configuration tutorial sometime… and then follow those tutorials.

And I know everybody says 4.2.3 is easier than the new branch, but this is the future… I was never motivated to make a project or learn more with the 4.2.3 version, so I’d better just learn the modern version.

The bed calls me… oh the pillow… here I come. Bye!

Posted in Programare | Tagged: , , , , , , | 4 Comments »

Programming languages history and current popularity

Posted by sjackm on 11 August 2011

Here’s very nice infographic about programming languages history. At the end of it you’ll find a comparison chart with the most popular programming languages as of May 2011.

 

Posted in Computer, Programare | Tagged: , , , | Leave a Comment »

The Arduino Documentary

Posted by sjackm on 12 February 2011

You can watch it HD here.

 

Posted in Arduino, Hardware, Programare, Robotics | Tagged: , , , , , | Leave a Comment »

C++ in Code::Blocks -2-

Posted by sjackm on 26 September 2010

Numere prime

Prime numbers

Here’s the code:

#include <iostream>
#include <math.h>
using namespace std;

int prim(int n){
int i;
bool test_prim=1;
for(i=2;i<=sqrt(n);i++){

if(n%i==0){
test_prim=0;
break;
}
}
if(test_prim){
return 1;
}
else{
return 0;
}}

int main(){
int n;
bool continua=1;
while(continua){

cout<<”Introduceti un numar de la tastatura: “<<endl;
cin>>n;
if(prim(n)==1){
cout<<”Numarul este prim!”<<endl;
}
else{
cout<<”Numarul nu este prim!”<<endl;
}
cout<<”Doriti sa continuati? (Da=1, Nu=0)”<<endl;
cin>>continua;
}
return 0;
}

Posted in Programare | Tagged: , , | 4 Comments »

C++ in Code::Blocks -1-

Posted by sjackm on 22 September 2010

These will probably be a series of  c++ programming tutorials. Just trying to help my colleagues, I’m not a programmer myself :P .

Unfortunately most of them will be in romanian I think(if not all…). If it’s really necessary I’ll try to include English captions as well.

Code::Blocks <-1-> De la Borland la CodeBlocks

Watch it HD on youtube.

Posted in Uncategorized | Tagged: , , , | 2 Comments »

Random Poetry Generation(RPG? ..meh…)

Posted by sjackm on 3 February 2010

Ok, I’ll have to write in romanian here because it’s in no way related to english poetry, so it would be just useless to write about it here.

Recent mi-a venit din nou cheful de c++ si mi-a venit o idee traznita: dupa inventatorul de cuvinte( fara noima), de ce sa nu incerc si un inventator de poezii( fara noima!!) ?

M-am pus pe treaba

Acum va servesc pe tava

Un inventator de poezii anormal

Cu 200 de cuvinte in vocabular .

:D

Meh… e doar asa de distractie. Nu are sens si nici n-o sa ma chinuiesc sa-l fac sa aiba. Probabil ca o sa mai adaug cuvinte (momentan sunt 4 array-uri: subiect,atribut,complement,predicat, fiecare array avand 50 de cuvinte). E mai greu de adaugat cuvinte in arrayul “complement”(e cuvantul de la sfarsitul versului) fiindca ar trebui sa fie un procentaj 50%/50% de cuvinte care se aseamna la sfarsit( pentru a produce rima). Sau cel putin sa nu existe cuvinte “unice” din punct de vedere al asemanarii(adica sa nu fie nici un alt cuvant cu care ar rima in array).  Nici vorba de masura sau alte “standarde” care trebuie respectate cand faci o poezie. Chiar si asa programul uneori da crash din cauza procentajului de care vorbeam.

Programul e simplu. Iti cere numarul de strofe dorite( pentru numarul real e defapt numar-1, adica daca doriti 1 strofa veti scrie 0, daca doriti 2 scrieti 1 etc… , si asta fiindca daca incerc sa o fac automat, da crash programul si nu am mai avut chef sa ma uit prin cod sa corectez).

Va afisa (ca sa stiti totusi ce se intampla) ca strofele se genereaza. Eu cel mai mare numar de strofe care l-am generat, fara sa dea crash programul, a fost 51(LE: Am depasi recordul, am atins 270 :D ). Uneori da crash chiar dupa prima strofa generata… totul depinde de arrayul “complement” daca da crash sau nu.

Strofele sunt scrise automat intr-un fisier denumit sugesti “Poezii.txt” in directorul curent( adica unde e pus programul). Daca programul  nu da crash, dati enter si ati iesit din el. Daca da… well… o sa apara mesajul cu “close”.

In program este afisata la un moment dat o sugestie: sa stergeti fisierul “Poezii.txt” inainte de a-l porni din nou. E optional… initial am crezut ca de acolo provine eroarea… oricum fisierul e rescris.

Have fun… dupa asta cred ca urmeaza “Prozatorul Drogat”  =)).

——===>DOWNLOAD LINK -> Version1.  <====——-

__________________________________________________________________________________

Posted in Computer, Software | Tagged: , , , , , , , , , , , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.