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

Tuesday, 10 December 2013

Color fading with arduino

Yesterday evening I encountered this question on StackOverflow website.


In my opinion, the low quality of the original code is the main problem here. I suggested Tristan to use the Arduino Tinker Library and I wrote an example just for him.

#include <Vect3d.h>
#include <SerialLog.h>

Tinker::Vect3d<float> red(255,0,0);
Tinker::Vect3d<float> green(0,255,0);
Tinker::SerialLog serialLog;

void setup(){
  Serial.begin(9600);
  serialLog.display("Fade color example");  
  serialLog.endline();
}

void loop(){
  //fade factor computation
  const uint32_t t   = millis()%10000;
  const float cosArg = t/10000.*3.1415*2;
  const float fade   = abs(cos(cosArg));

  //Here's the color computation... as you can see is very easy to do!! :)
  Tinker::Vect3d<uint8_t> finalColor(red*fade + green*(1-fade));

  //We print the vect3d on the arduino serial port
  Tinker::LOG::displayVect3d(finalColor,&serialLog);
  serialLog.endline();
  delay(500);
}
This example will print the following output on the serial port:
Fade color example
V[255;0;0]
V[242;12;0]
V[206;48;0]
V[149;105;0]
V[78;176;0]
V[0;254;0]
V[79;175;0]
V[150;104;0]
V[206;48;0]
V[242;12;0]
V[254;0;0]
V[242;12;0]
V[205;49;0]
V[148;106;0]
V[77;177;0]
V[1;253;0]
V[80;174;0]
V[151;103;0]

If you want to fade multiple colors in one of your sketch you can simply use this code! :)

PS: Today I found this question on stack overflow: http://stackoverflow.com/questions/20482642/how-to-fade-between-two-vectors-by-value.

I'm really proud that someone is using the Arduino Tinker Library :)

Saturday, 26 October 2013

Arduino LCD Logger

The post "Arduino Tinker Library"  introduces the technique by which an LCD logger with no effort can be created with a different concrete implementation of the AbstractLog class.

My set up consist of an LCD display which is contained in the Arduino Starter Kit. You can easily program it with the LiquidCrystal Library.

Connecting LCD Display

The hardware set up is really simple


And here's the result



LiquidCrystalLog

Now we are ready to use the LCD display as a logger. In the LiquidCrystalLog.h file you can find a concrete implementation of the class AbstractLog.
You can also find a new example, called vect3d_LiquidCrystalLog. As you can see, the original source code has not beeing modified, except for the log variable: it's now a LiquidCrystalLog type variable.
Here's a video of the new logger



You can write every type of logger, SD Card logger, Bluethoot logger, RF logger and so on.

Friday, 25 October 2013

Arduino Tinker Library



I've just started working on my Arduino C++ library. You can find it on GitHub: feel free to download it and use it for your projects.

I'll try to build a modern C++ library with advanced techniques and many examples. I'll suggest you a couple of books by which I am inspired:

Log Interface

AbstractLog.h contains an abstract class that you can use in your algorithm class to log every operation.
In SerialLog.h you will find a concrete implementation of AbstractLog which write your algorithm log on the Arduino serial port
In the future I'll show you how to log on a LCD display instead!

Vect3d

Vect3d.h contains a class that represent a 3-dimensional vector. You can use it to do some calculation on a generic 3D space (euclidean space, RGB color space ...)


If you install the Arduino Tinker Library you will find into Arduino IDE an example on how to use Vect3d in your code (File -> Examples -> ArduinoTinkerLibrary -> vect3d)

If you open the Arduino Serial Monitor (Tools -> Serial Monitor or Ctrl+Shift+M) you should see the following output

Vect3d Example
Constructor
V[0;0;0]
V[128;128;128]
V[64;64;64]
V[0.00;0.00;0.00]
V[1.00;1.00;1.00]
V[1.00;1.00;1.00]
V[123.00;456.00;789.00]
Copy
V[64.00;64.00;64.00]
V[1.00;1.00;1.00]
Test
f1 == b2
f3 != b2
Sum
V[124.00;457.00;790.00]
V[124.00;458.00;792.00]
V[129;129;129]
V[130;131;132]
Sub
V[122.00;455.00;788.00]
V[122.00;454.00;786.00]
V[129;130;131]
V[127;127;127]
Scalar Mul
V[307.50;1140.00;1972.50]
V[3.00;3.00;3.00]
Scalar Div
V[49.20;182.40;315.60]
V[1.00;1.00;1.00]
External functions
NormL1(V[1.00;1.00;1.00] - V[64.00;64.00;64.00]): 189.00
NormL2(V[1.00;1.00;1.00] - V[64.00;64.00;64.00]): 13.86
dot(V[1.00;1.00;1.00] - V[64.00;64.00;64.00]): 192.00
cross(V[123.00;456.00;789.00] - V[64.00;64.00;64.00]): V[-21312.00;42624.00;-21312.00]