Mini code : Potentiomètres // Gamebuino Meta

Voici le code commenté permettant de tester facilement vos beaux potentiomètres tout neufs avec votre Gamebuino.


Si vous souhaitez apprendre à réaliser ce programme par vous-même où n’arrivez pas à comprendre le code commenté brut (quand on débute c’est un peu sec, ça ira mieux avec un peu d’habitude) vous pouvez suivre notre incroyable Tuto pas-à-pas : potentiomètres


/*
 * Code by Tom from Gamebuino
 * V 1.0 16/05/2021
 * Free to use and share
 */

#include <Gamebuino-Meta.h>
//we include the math library to get access to sin() and cos() functions as well as a value of PI used in the program
#include <math.h>

//variable declaration for the potentimeters values and their conversion to angle
int32_t i32_L_pot_value;
int32_t i32_L_pot_angle;

int32_t i32_R_pot_value;
int32_t i32_R_pot_angle;

//variable declaration for the points used to draw lines
int16_t i16_L_point_x;
int16_t i16_L_point_y;

int16_t i16_R_point_x;
int16_t i16_R_point_y;

//define of the analog pins used to plug the potentiometers, here A1 for left and A2 for right
#define L_POT_PIN A1
#define R_POT_PIN A2

//define of our circle properties, here it has a radius of 15 and is centered on the screen
#define CIRCLE_R 15
#define L_CIRCLE_X gb.display.width()/4
#define R_CIRCLE_X (gb.display.width()/4)*3
#define CIRCLE_Y gb.display.height()/2


void setup() {
  // put your setup code here, to run once:
  gb.begin();

}

void loop() {
  // put your main code here, to run repeatedly:
  while (!gb.update());

  // the read_and_calc function will handle the data acquisition and math needed for the program
  read_and_calc();

  //the draw_interface function will handle the display of all the elements needed for the program
  draw_interface();
}

void  read_and_calc(){
  // we read the value from the potentiometer plugged to the analog port A1
  i32_L_pot_value = analogRead(L_POT_PIN);
  i32_R_pot_value = analogRead(R_POT_PIN);
  // we convert the value from a range of 0 to 1023 to an angle within 360 degrees
  i32_L_pot_angle = (i32_L_pot_value * 360) / 1023;
  i32_R_pot_angle = (i32_R_pot_value * 360) / 1023;

  // we calculate the coordinates of a point on the circle perimeter corresponding to the value of the angle found above
  // note the multiplication by PI/180, this is because the angle we calculated in degrees must be converted to radians
  i16_L_point_x = L_CIRCLE_X + CIRCLE_R * cos(i32_L_pot_angle * M_PI/180);
  i16_L_point_y = CIRCLE_Y + CIRCLE_R * sin(i32_L_pot_angle * M_PI/180);
  
  i16_R_point_x = R_CIRCLE_X + CIRCLE_R * cos(i32_R_pot_angle * M_PI/180);
  i16_R_point_y = CIRCLE_Y + CIRCLE_R * sin(i32_R_pot_angle * M_PI/180);
}

void  draw_interface(){
  // the clear function is needed to reset the screen between each frame, otherwise frames would mix together and get messy
  gb.display.clear();

  // we print the value read from the potentiometer and its value when converted to an angle
  gb.display.setCursor(gb.display.width()/2 - 12, 0);
  gb.display.printf("Values");
  gb.display.setCursor(gb.display.width()/2 - 12, gb.display.height() - 6);
  gb.display.printf("Angles");

  //raw values display
  gb.display.setCursor(gb.display.width()/4 - 5, CIRCLE_Y - CIRCLE_R - 7);
    gb.display.printf("%d", i32_L_pot_value);

  gb.display.setCursor((gb.display.width()/4)*3 - 5, CIRCLE_Y - CIRCLE_R - 7);
    gb.display.printf("%d", i32_R_pot_value);

  //angles' values display
  gb.display.setCursor(gb.display.width()/4 - 5, CIRCLE_Y + CIRCLE_R + 5);
    gb.display.printf("%d", i32_L_pot_angle);

  gb.display.setCursor((gb.display.width()/4)*3 - 5, CIRCLE_Y + CIRCLE_R + 5);
    gb.display.printf("%d", i32_R_pot_angle);


  // we draw the circle with the properties we defined at the beginning, its center's position and radius
  gb.display.drawCircle(L_CIRCLE_X, CIRCLE_Y, CIRCLE_R);
  gb.display.drawCircle(R_CIRCLE_X, CIRCLE_Y, CIRCLE_R);
  // we draw a line between the center of the circle and the point on its perimeter corresponding to the potentiometer value we read
  gb.display.setColor(RED);
  gb.display.drawLine(L_CIRCLE_X, CIRCLE_Y, i16_L_point_x, i16_L_point_y);
  gb.display.drawLine(R_CIRCLE_X, CIRCLE_Y, i16_R_point_x, i16_R_point_y);
}

Vous êtes libres de le modifier à votre convenance et de le partager.![démo_pot_code|375x500]