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 rotating the display to set the screen in landscape mode and then loads the bitmap file. Here is the sample code snippet -

 

int16_t dial;

void setup() {        

 CleO.begin();

 CleO.DisplayRotate(2, 0);

 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

                            // CleO IO - Rotary Pot connects to CN11

 int angle = gauge_angle(analog);

 int cx = 240;                 // X center

 int cy = 160;                   // Y Center

 

 CleO.Start();

 CleO.Bitmap(dial, 0, 0);

 

// Red needle

 CleO.NeedleWidth(10);

 CleO.NeedleColor(RED);

 CleO.NeedleXY(cx, cy, 153, 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

 

 

Project 12 - A Speedometer Display