Project 12 - A Speedometer Display

<< Click to Display Table of Contents >>

Navigation:  Projects >

Project 12 - A Speedometer Display

This project shows how to draw a speedometer using the CleO Graphics Primitive commands Bitmap() and Needle().

 

 

Component(s)

 

This project uses the following component(s) -

 

Rotary Pot IO

 

 

Wiring Diagram

 

 

Project 12 -Speedometer Display

 

 

Code

 

The Setup() routine starts by loading the bitmap file. Here is the sample code snippet -

 

int16_t dial;

void setup() {

 CleO.begin();

 dial = CleO.LoadImageFile("@Pictures/speedo.png", 0);

}

 

The loop() routine takes the reading from the analog pin A0 and maps this value to gauge the value using the gauge_angle() function. The rescale() routine is used to map any value between (0, 1023) to (-2, 263). Now the mapped angle value is used to draw the needle. Here is the sample code snippet -

 

void loop() {

 int f = FONT_MEDIUM;           // Font for numbers

 int analog = analogRead(0);   // sample analog pin A0

 int angle = gauge_angle(analog);

 int cx = 240;                 // X center

 int cy = 160;                   // Y Center

 

 CleO.Start();

 CleO.BitmapJustification(MM);

 CleO.BitmapTransform(1.60, 0);

 CleO.Bitmap(dial, cx, cy);

 

// Red needle

 CleO.NeedleWidth(15);

 CleO.NeedleColor(RED);

 CleO.NeedleXY(cx, cy, 190, angle);

 

 CleO.Show();

}

 

// Rescale x from the range (x0, x1) to the range (y0, y1)

float rescale(int x, int x0, int x1, int y0, int y1) {

 x = max(x0, min(x1, x));

 return y0 + ((x - x0) * long(y1 - y0) / float(x1 - x0));

}

 

float gauge_angle(int x) {

 return rescale(x, 0, 1023, -2, 263);

}

 

 

Output

 

 

Project12-ASpeedometerDisplay