1

only display this:

ARDUMOTIVE
ArduinoBased
compassn.Compass
Michalis Vas.

nothing else. I tried another Arduino Uno, Mega, Nano, but execute only set up. Display is working.No compiler errors.Sketch upload it with out errors!! i found here https://www.instructables.com/id/Arduino-Digital-Magnetic-Compass-HMC5883L-2-displa/

Code is here.Thank you.

`*          Ardumotive.com Project Corner
   Arduino digital magnetic Compass with two display modes
   using 3-Axis Digital Compass IC HMC5883L board
   Dev. Vasilakis Michalis / Ver. 1.0 / Date:17/3/2015      */

//Include libraries
#include <SPI.h>
#include <Wire.h>
#include <Button.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Adafruit_HMC5883_U.h>
#include <Adafruit_Sensor.h>

//Init Display (SCLK, DIN, D/C, CS, RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
//Init HMC5883L sensor
Adafruit_HMC5883_Unified compass = Adafruit_HMC5883_Unified(12345);

//Constants
const int lcdLight = 13; //BL pin of LCD to arduino pin 13 (BL = Back Light)
Button buttonA = Button(2,HIGH); //Button to turn on/off display light
Button buttonB = Button(8,HIGH); //Button for changing display mode
// For 1st display mode:
static unsigned char Leters[] = { 'N' , 'E' , 'S' , 'W' }; 
const static unsigned char PROGMEM arrow_bmp[] ={B00100000, B00100000, B01110000, B01110000, B11111000,};
// For 2nd display mode:
int r = 24; // radius of compass rose
int x0= 60; // x-origin
int y0= 24; // y-origin

//Variables
int flag=0;//Store buttonA condition 0:1st display mode / 1: 2nd display mode
int i=1;   //Store buttonB condition 1: light on / 0: light off

void setup() {
  pinMode(lcdLight, OUTPUT); // lcd led
  compass.begin();
  Wire.begin();
  display.begin();
  display.setContrast(60);
  //Print a welcome message in startup for 6sec.//////////
  digitalWrite(lcdLight,HIGH);
  display.clearDisplay();   // clears the screen and buffer
  display.setTextColor(BLACK);
  display.setCursor(0,2);
  display.print("  ARDUMOTIVE  ");
  display.setCursor(0,13);
  display.print(" ArduinoBased ");
  display.setCursor(0,23);
  display.print(" compassn.Compass ");
  display.setCursor(0,33);
  display.print(" Michalis Vas.");  
  display.display(); // show splashscreen
  digitalWrite(lcdLight,LOW);
  delay(6000);
  /////////////////////////////////////////////////////////
  display.clearDisplay();   // clears the screen and buffer
}

void loop() {

  //if buttonB is pressed, 'flag' will change between 0 and 1
    if(buttonB.isPressed()){
      delay(100);//small delay
    if(flag==0){ flag=1; }
    else{ flag=0; }
  }
  //if buttonA is pressed, 'i' will change between 0 and 1
  if(buttonA.isPressed()){
    if(i==0){ i=1; }
    else{ i=0; }
  }
  //if i is equal with 0, set display light on, else set display light off
    if (i==0){  
  digitalWrite(lcdLight,HIGH);}
    else{
  digitalWrite(lcdLight,LOW);
   }
  //Reading and calculate degrees:   /////////////////////////////////////
  //Get new sensor value every time that loop starting again
  sensors_event_t event; 
  compass.getEvent(&event);
  //Variable heading stores value in radians
  float heading = atan2(event.magnetic.y, event.magnetic.x);

  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the compassnetic field in your location.
  // Find yours here: http://www.compassnetic-declination.com/
  // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
  float declinationAngle = 0.22;  //<--Change 0.22 with yours. If you can't find your declination juct delete those lines ;)
  heading += declinationAngle;
  // Correct for when signs are reversed.
  if(heading < 0)   heading += 2*PI;
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)heading -= 2*PI;
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 
  //Convert float to int
  int angle=(int)headingDegrees;
  //////////////////////////////////////////////////////////////////////////

  display.clearDisplay(); // Just in case...

  //if flag is equal with 0 - 1st Display Mode 
  if (flag==0){
  DrawRow(angle); //call DrawRow function
  display.drawBitmap(40, 24,  arrow_bmp, 5, 5, 1);
  display.drawLine(42, 0, 42, 24, BLACK);
  display.setCursor(32,34);  
  display.print(angle);
  display.setTextSize(1);
  display.print("o");
  display.display();
  delay(500); //print new value every 0.5sec
  }

  //if flag is equal with 1 - 2nd Display Mode
  if (flag==1){
  // Calculation of compass needle on lcd pointing to the direction 
  DrawCircle(angle); //call DrawCircle function
  // Display actual heading
  display.setTextSize(2);
  display.setTextColor(BLACK);
  display.setCursor(0,0);
  display.println(angle);

  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(x0-2,0);
  display.println("N");

  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor((x0+r)-5,y0-3);
  display.println("E");

  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(x0-2,y0+r-8);
  display.println("S");

  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor((x0-r)+5,y0-3);
  display.println("W");

  // Triangle for direction
  display.drawTriangle(0, 46, 20, 46, 10, 18, BLACK);
  display.fillTriangle (0, 46, 20, 46, 10, 18, BLACK);
  display.display();
  delay(500); //print new value every 0.5sec
  }
}/// end of program

////////////      Functions    ////////////

//Draw rows and print letters to display - 1st Display Mode
void DrawRow(int angle) {

  display.drawLine(0, 0, 84, 0, BLACK);
  display.drawLine(0, 1, 84, 1, BLACK);

  display.drawLine(0, 22, 91, 22, BLACK);
  display.drawLine(0, 23, 95, 23, BLACK);

  display.setTextSize(2);
  display.setTextColor(BLACK);  

  int start = 42 - angle / 3 ;
  if (start > 120) start += -120 ;

  int x = 0 ;
  int y = 18 ;
  for (int i=0; i<4; i++) {
      x = start + (i*30) -1;
      if (x>119) x += -120;
      display.drawPixel(x+1, y-2, 1);
      display.drawPixel(x, y, 1);
      display.drawPixel(x+1, y, 1);
      display.drawPixel(x+2, y, 1);
      display.drawPixel(x, y-1, 1);
      display.drawPixel(x+1, y-1, 1);
      display.drawPixel(x+2, y-1, 1);
      display.setCursor((x-4),(y-16)); 
      display.write(Leters[i]);
   }
  for (int i=0; i<24; i++) {
      x = start + (i*5) -1;
      if (x>119) x += -120;
      display.drawPixel(x+1, y+1, 1);
      display.drawPixel(x, y+2, 1);
      display.drawPixel(x+1, y+2, 1);
      display.drawPixel(x+2, y+2, 1);
      display.drawPixel(x, y+3, 1);
      display.drawPixel(x+1, y+3, 1);
      display.drawPixel(x+2, y+3, 1);
   };

  for (int i=0; i<8; i++) {
      x = start + (i*15)-1;
      if (x>119) x += -120;
      display.drawPixel(x+1, y-1, 1);

      display.drawPixel(x, y, 1);
      display.drawPixel(x+1, y, 1);
      display.drawPixel(x+2, y, 1);
      display.drawPixel(x, y+1, 1);
      display.drawPixel(x+1, y+1, 1);
      display.drawPixel(x+2, y+1, 1);

   };
}

////Draw circle and print letters to display - 1st Display Mode
void DrawCircle(int angle) {
  if (angle >= 0 && angle <=45) {    // 0-45 degrees
    int angle = angle/2;
    display.drawLine(x0, y0, x0+angle, y0-r, BLACK);
  } 
  else if (angle >45 && angle <=90) {// 46-90 degrees
    int angle = (angle-44)/2 ;
    display.drawLine(x0, y0, x0+r, (y0-r)+angle, BLACK);
  } 
  else if (angle >90 && angle <=135) {// 91-135 degrees
    int angle = (angle-90)/2;
    display.drawLine(x0, y0, x0+r, y0+angle, BLACK);
  } 
  else if (angle >135 && angle <=180){// 136-180 degrees
    int angle = (angle-134)/2;
    display.drawLine(x0, y0, (x0+r)-angle, y0+r, BLACK);
  } 
  else if (angle >180 && angle <=225){// 181-225 degrees
    int angle = (angle-178)/2; 
    display.drawLine(x0, y0, x0-angle, y0+r, BLACK);
  } 
  else if (angle >225 && angle <=270){// 226-270 degrees
    int angle = (angle-222)/2; 
    display.drawLine(x0, y0, x0-r, (y0+r)-angle, BLACK);
  } 
  else if (angle >270 && angle <=315){// 271-315 degrees
    int angle = (angle-270)/2;
    display.drawLine(x0, y0, x0-r, y0-angle, BLACK);
  } 
  else if (angle >315 && angle <=360){// 316-360 degrees
    int angle = (angle-312)/2;
    display.drawLine(x0, y0, (x0-r)+angle, y0-r, BLACK);
  }
}
Andreasmon
  • 11
  • 2
  • 2
    it doesn't even clear after 6 seconds? – Jaromanda X Feb 22 '19 at 01:00
  • 1
    Please give links to the modules of the display and the magnetometer that you use. How are they connected? Do you use extra resistors or extra level shifters? You could send messages to the serial port, to know what is going on. – Jot Feb 22 '19 at 06:12
  • And start debug, by adding Serial.Print in the loop you can verify if it is running or not. There is also a more comprehensive library available for debugging, https://playground.arduino.cc/Code/SerialDebugger – MatsK Feb 22 '19 at 08:51
  • no,it does not clear after 6 seconds.I tried with same pin connection the lcd nokkia 5510 the scetch of Adafruit_PCD8544 library sketch pcdtest and lcd works fine.I also put in if{} and code display.print(" something "); to see if code execute and enter in loop,but nothing.I now try serial print to see what happen (like MatsK said).Is first time happens someting like it to me.Thank you very much all for your help. – Andreasmon Feb 22 '19 at 11:15
  • lcd screen 51110 is this https://www.aliexpress.com/item/High-Quality-8448-84x84-LCD-Module-blue-backlight-adapter-PCB-for-Nokia-5110-for-Arduino/32614334972.html?spm=a2g0s.12269583.0.0.313e7cdfn3UXYH – Andreasmon Feb 22 '19 at 11:22
  • As you have a lot of test in the loop; I'll suggest first to add a "Serial.println("here"); at the beginning of the loop in order to see if yo enter the loop. Then, print some message in each "if/else" to see what's happening. – Peter Feb 22 '19 at 11:25
  • i edit code and add serial print.Now serial is printing when button A or B is pressed,flag when change 0 or 1,i when change 0 or 1(all this execute in the loop!),but does not lcd print nothing,only freeze in first message of set up-->ARDUMOTIVE ArduinoBased compassn.Compass Michalis Vas. i believe,but i can not find,that maybe sensor is faulty and code allow display to show data of digital compass,ONLY when sensor send data!!!but i can not find this in code to overide,if is exist;Any help; – Andreasmon Feb 22 '19 at 20:29
  • thank you very much all.I delete code of the compass sensor,overided it,and now lcd show graphics of digital compass,but no data.The sensor do not working.thank you very much all for you value help.I will buy another sensor module compass. – Andreasmon Feb 22 '19 at 20:51
  • You might run into the same problem with a new sensor. The 5110 is sold as 3.3 to 5v, but it is not for 5v, only for 3.3v. We would like to know if you use a resistor for the backlight. Do you use a breadboard? Some breadboards have bad contacts. Sparkfun has a guide for the 5110 connected to a 5v arduino board: https://learn.sparkfun.com/tutorials/graphic-lcd-hookup-guide/all The HMC5883L magnetometer is a 3.3v chip. We do not know which module you have and how you have connected a 5v arduino board to a 3.3v sensor. – Jot Feb 22 '19 at 21:28

0 Answers0