Alat Penghitung Putaran CW / CCW Sensor HMC5883L LCD OLED
Pada kesempatakn kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang bisa menghitung jumlah putaran dalam rotasi, jadi saat alat ini berputar dia akan menghitung +1 jika CW, namun jika CCW maka akan -1 namun harus tekan tombol setting dulu untuk pemilihan arah putarnya. untuk lebih jelasnya berikut adalah komponen dan kodingnya.
1. Komponen
2. Program Arduino IDE
#include <Wire.h>
#include <HMC5883L.h>
#include <MPU6050.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);
//rorasi 156 timur, barat 55
//TILT -500 datar, 1000 miring
int x, y, rotasi, kemiringan, sudut;
char tmp_string1[8];
char tmp_string2[8];
char tmp_string3[8];
char tmp_string4[8];
char tmp_string5[8];
char tmp_string6[8];
float headingDegrees;
HMC5883L compass;
MPU6050 mpu;
int bt1 = 2;
int bt2 = 3;
int bt3 = 4;
int bt4 = 5;
int mark, mark2;
int bt1x;
int bt2x;
int bt3x;
int bt4x;
int tambah, kurang, tambah2, kurang2;
int rotasitotal;
int fullrotasi;
void draw(void) {
//float dua angka dibelakang koma
dtostrf(headingDegrees, 4, 2, tmp_string1);
dtostrf(sudut, 4, 2, tmp_string2);
dtostrf(rotasitotal, 4, 0, tmp_string3);
dtostrf(fullrotasi, 4, 0, tmp_string4);
dtostrf(tambah, 4, 0, tmp_string5);
dtostrf(kurang, 4, 0, tmp_string6);
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
u8g.drawStr(0, 15, "Rotasi= ");
u8g.drawStr(60, 15, tmp_string1);
u8g.drawStr(0, 30, "FULL= ");
u8g.drawStr(60, 30, tmp_string2);
u8g.drawStr(0, 45, "R%= ");
u8g.drawStr(10, 45, tmp_string3);
u8g.drawStr(0, 60, "T%= ");
u8g.drawStr(10, 60, tmp_string4);
u8g.drawStr(81, 45, "+");
u8g.drawStr(83, 45, tmp_string5);
u8g.drawStr(81, 60, "-");
u8g.drawStr(83, 60, tmp_string6);
}
void setup(void) {
Serial.begin(9600);
pinMode(bt1,INPUT_PULLUP);
pinMode(bt2,INPUT_PULLUP);
pinMode(bt3,INPUT_PULLUP);
pinMode(bt4,INPUT_PULLUP);
//Serial.println("Initialize MPU6050");
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
//Serial.println("tidak ada sensor MPU6050 yang terpasang!");
delay(500);
}
//Kalibrasi gyrometer
mpu.calibrateGyro();
mpu.setThreshold(3);
// Initialize Initialize HMC5883L
Serial.println("Initialize HMC5883L");
while (!compass.begin())
{
Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
delay(500);
}
// Set measurement range
compass.setRange(HMC5883L_RANGE_1_3GA);
// Set measurement mode
compass.setMeasurementMode(HMC5883L_CONTINOUS);
// Set data rate
compass.setDataRate(HMC5883L_DATARATE_30HZ);
// Set number of samples averaged
compass.setSamples(HMC5883L_SAMPLES_8);
// Set calibration offset. See HMC5883L_calibration.ino
compass.setOffset(0, 0);
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(void) {
bt1x = digitalRead(bt1);
bt2x = digitalRead(bt2);
bt3x = digitalRead(bt3);
bt4x = digitalRead(bt4);
rotasitotal = (100 * rotasi) / fullrotasi;
if(bt1x == 0){
tambah = 1;
kurang = 0;
}
if(bt2x == 0){
kurang = 1;
tambah = 0;
}
if(bt3x == 0){
delay(200);
fullrotasi++;
}
if(bt4x == 0){
delay(200);
fullrotasi--;
}
Vector rawAccel = mpu.readRawAccel();
sudut = rawAccel.XAxis;
Vector norm = compass.readNormalize();
// Calculate heading
float heading = atan2(norm.YAxis, norm.XAxis);
float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / M_PI);
heading += declinationAngle;
// Correct for heading < 0deg and heading > 360deg
if (heading < 0)
{
heading += 2 * PI;
}
if (heading > 2 * PI)
{
heading -= 2 * PI;
}
// Convert to degrees
headingDegrees = heading * 180/M_PI;
// picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
if((headingDegrees > 100)&&(headingDegrees < 160)&&(mark == 0)&&(tambah == 1)){
mark = 1;
}
if((headingDegrees > 50)&&(headingDegrees < 100)&&(mark == 1)&&(tambah == 1)){
mark = 0;
rotasi++;
}
if((headingDegrees > 100)&&(headingDegrees < 160)&&(mark == 0)&&(kurang == 1)){
mark = 1;
}
if((headingDegrees > 50)&&(headingDegrees < 100)&&(mark == 1)&&(kurang == 1)){
mark = 0;
rotasi--;
}
if((sudut > -700)&&(sudut < -400)&&(mark2 == 0)&&(tambah2 == 1)){
mark2 = 1;
}
if((sudut > 800)&&(sudut < 1200)&&(mark2 == 1)&&(tambah2 == 1)){
mark2 = 0;
kemiringan++;
}
if((sudut > -700)&&(sudut < -400)&&(mark2 == 0)&&(kurang2 == 1)){
mark2 = 1;
}
if((sudut > 800)&&(sudut < 1200)&&(mark2 == 1)&&(kurang2 == 1)){
mark2 = 0;
kemiringan--;
}
// rebuild the picture after some delay
delay(500);
}
#include <HMC5883L.h>
#include <MPU6050.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);
//rorasi 156 timur, barat 55
//TILT -500 datar, 1000 miring
int x, y, rotasi, kemiringan, sudut;
char tmp_string1[8];
char tmp_string2[8];
char tmp_string3[8];
char tmp_string4[8];
char tmp_string5[8];
char tmp_string6[8];
float headingDegrees;
HMC5883L compass;
MPU6050 mpu;
int bt1 = 2;
int bt2 = 3;
int bt3 = 4;
int bt4 = 5;
int mark, mark2;
int bt1x;
int bt2x;
int bt3x;
int bt4x;
int tambah, kurang, tambah2, kurang2;
int rotasitotal;
int fullrotasi;
void draw(void) {
//float dua angka dibelakang koma
dtostrf(headingDegrees, 4, 2, tmp_string1);
dtostrf(sudut, 4, 2, tmp_string2);
dtostrf(rotasitotal, 4, 0, tmp_string3);
dtostrf(fullrotasi, 4, 0, tmp_string4);
dtostrf(tambah, 4, 0, tmp_string5);
dtostrf(kurang, 4, 0, tmp_string6);
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
u8g.drawStr(0, 15, "Rotasi= ");
u8g.drawStr(60, 15, tmp_string1);
u8g.drawStr(0, 30, "FULL= ");
u8g.drawStr(60, 30, tmp_string2);
u8g.drawStr(0, 45, "R%= ");
u8g.drawStr(10, 45, tmp_string3);
u8g.drawStr(0, 60, "T%= ");
u8g.drawStr(10, 60, tmp_string4);
u8g.drawStr(81, 45, "+");
u8g.drawStr(83, 45, tmp_string5);
u8g.drawStr(81, 60, "-");
u8g.drawStr(83, 60, tmp_string6);
}
void setup(void) {
Serial.begin(9600);
pinMode(bt1,INPUT_PULLUP);
pinMode(bt2,INPUT_PULLUP);
pinMode(bt3,INPUT_PULLUP);
pinMode(bt4,INPUT_PULLUP);
//Serial.println("Initialize MPU6050");
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
//Serial.println("tidak ada sensor MPU6050 yang terpasang!");
delay(500);
}
//Kalibrasi gyrometer
mpu.calibrateGyro();
mpu.setThreshold(3);
// Initialize Initialize HMC5883L
Serial.println("Initialize HMC5883L");
while (!compass.begin())
{
Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
delay(500);
}
// Set measurement range
compass.setRange(HMC5883L_RANGE_1_3GA);
// Set measurement mode
compass.setMeasurementMode(HMC5883L_CONTINOUS);
// Set data rate
compass.setDataRate(HMC5883L_DATARATE_30HZ);
// Set number of samples averaged
compass.setSamples(HMC5883L_SAMPLES_8);
// Set calibration offset. See HMC5883L_calibration.ino
compass.setOffset(0, 0);
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(void) {
bt1x = digitalRead(bt1);
bt2x = digitalRead(bt2);
bt3x = digitalRead(bt3);
bt4x = digitalRead(bt4);
rotasitotal = (100 * rotasi) / fullrotasi;
if(bt1x == 0){
tambah = 1;
kurang = 0;
}
if(bt2x == 0){
kurang = 1;
tambah = 0;
}
if(bt3x == 0){
delay(200);
fullrotasi++;
}
if(bt4x == 0){
delay(200);
fullrotasi--;
}
Vector rawAccel = mpu.readRawAccel();
sudut = rawAccel.XAxis;
Vector norm = compass.readNormalize();
// Calculate heading
float heading = atan2(norm.YAxis, norm.XAxis);
float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / M_PI);
heading += declinationAngle;
// Correct for heading < 0deg and heading > 360deg
if (heading < 0)
{
heading += 2 * PI;
}
if (heading > 2 * PI)
{
heading -= 2 * PI;
}
// Convert to degrees
headingDegrees = heading * 180/M_PI;
// picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
if((headingDegrees > 100)&&(headingDegrees < 160)&&(mark == 0)&&(tambah == 1)){
mark = 1;
}
if((headingDegrees > 50)&&(headingDegrees < 100)&&(mark == 1)&&(tambah == 1)){
mark = 0;
rotasi++;
}
if((headingDegrees > 100)&&(headingDegrees < 160)&&(mark == 0)&&(kurang == 1)){
mark = 1;
}
if((headingDegrees > 50)&&(headingDegrees < 100)&&(mark == 1)&&(kurang == 1)){
mark = 0;
rotasi--;
}
if((sudut > -700)&&(sudut < -400)&&(mark2 == 0)&&(tambah2 == 1)){
mark2 = 1;
}
if((sudut > 800)&&(sudut < 1200)&&(mark2 == 1)&&(tambah2 == 1)){
mark2 = 0;
kemiringan++;
}
if((sudut > -700)&&(sudut < -400)&&(mark2 == 0)&&(kurang2 == 1)){
mark2 = 1;
}
if((sudut > 800)&&(sudut < 1200)&&(mark2 == 1)&&(kurang2 == 1)){
mark2 = 0;
kemiringan--;
}
// rebuild the picture after some delay
delay(500);
}
3. VIDEO HASILNYA
No comments:
Post a Comment