0

programming help for a VL6180X
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

programming help for a VL6180X

by justatech on Mon Mar 19, 2018 8:36 am

Hello, I am need of some programming help. I am using a feather 32U4 with feather oled and a VL6180X to measure distance (obvious but..)
I want to use it in snapshot mode but am having a problem understanding the code provided by SMtech (could be spelt wrong sorry).

where I am stuck is how on earth do I 1.) clear a byte 2, write to a byte and 3.) read a byte.

the attached code was/is in the example prg from the manufacturer and I simply do not understand it.

Any help is appreciated and I will also except the abuse that will invariably accompany the help.

I am a PLC/CNC programmer and trying to learn something new..
thank you for any help you can provide:

Code: Select all | TOGGLE FULL SIZE
 

char WriteByte (wchar_t reg,char data) { 
  char data_write[3];

  data_write[0] = (reg >> 8) & 0xFF; //MSB of register address
  data_write[1] = reg & 0xFF; // LSB of register address
  data_write[2] = data & 0xFF;
}

  char ReadByte(wchar_t reg) {
  char data_write[2];
  char data_read[1];

  data_write[0] = (reg >> 8) & 0xFF; //MSB
  data_write[1] = reg & 0xFF; // LSB
  }
Last edited by adafruit_support_bill on Mon Mar 19, 2018 9:40 am, edited 1 time in total.
Reason: fixed [code] tags

justatech
 
Posts: 46
Joined: Thu Oct 11, 2012 6:34 pm

Re: programming help for a VL6180X

by justatech on Mon Mar 19, 2018 9:31 am

here is the code in its entirety , I hope someone can help with this.
Code: Select all | TOGGLE FULL SIZE
  [code]

/*This program is to measure various ranges from a stand alone bracket
 * range readings are in mm */



#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include <Wire.h>
#include <Adafruit_VL6180X.h>

Adafruit_VL6180X vl = Adafruit_VL6180X();

Adafruit_SSD1306 display = Adafruit_SSD1306();

#define BUTTON_A 9
#define BUTTON_B 6
#define BUTTON_C 5

#if (SSD1306_LCDHEIGHT != 32)
 #error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()

 Serial.begin(9600);
   
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // init done
  display.display();
  delay(1000);
 
  Serial.println("Adafruit VL6180x test!");
  if (! vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("Sensor found!");

  // text display big!
  display.setTextSize(4);
  display.setTextColor(WHITE);
 
  pinMode(BUTTON_A, INPUT_PULLUP);
  pinMode(BUTTON_B, INPUT_PULLUP);
  pinMode(BUTTON_C, INPUT_PULLUP);
}

char WriteByte (wchar_t reg,char data) { 
  char data_write[3];

  data_write[0] = (reg >> 8) & 0xFF; //MSB of register address
  data_write[1] = reg & 0xFF; // LSB of register address
  data_write[2] = data & 0xFF;
}

  char ReadByte(wchar_t reg) {
  char data_write[2];
  char data_read[1];

  data_write[0] = (reg >> 8) & 0xFF; //MSB
  data_write[1] = reg & 0xFF; // LSB
  }

int VL6180x_init()   {
 
 WriteByte(0x016,0); //clear the input value
 
 if ( !digitalRead(BUTTON_A)) display.print("Zero");
     delay(10);
      display.display();
 return 0;
}
 
   //clear the interrupts and zero the readings
int VL6180x_Clear_Interrupt()   
{
    //char clear;
 if (! digitalRead(BUTTON_A));
   
    WriteByte(0x015,0);
 return 0;
 
 }


 
int VL6180x_Start_Range()  {
     
   if (!digitalRead(BUTTON_B)); 
     
   WriteByte(0x018,1);
   display.setCursor(0,0);
   display.print("start reading" );
   display.display();
     delay(1000);
 return 1;
 }

 
int VL6180x_Read_Range() {
  int range;
  int offset = (range <= 30);
  int actrange = (range - offset);
 
 
  if (!digitalRead(BUTTON_C));
 
   range=ReadByte(0x062);
   
      // Read range result     
    range=vl.readRange();
          delay(500);
       
  return actrange;


void loop()

   uint8_t range = vl.readRange();
   uint8_t status = vl.readRangeStatus();
   
   int offset = (range <= 30);
   int actrange = (range - offset) ;
   
   if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.println(actrange);
    display.clearDisplay();
    display.setCursor(0,0);
    display.print(actrange);
    delay(5000);     
    display.print("mm");
    display.display();
    Serial.println(); }
   else {
    display.display();
    display.clearDisplay();
    return;}
}

[/code]                     

justatech
 
Posts: 46
Joined: Thu Oct 11, 2012 6:34 pm

Re: programming help for a VL6180X

by adafruit_support_carter on Mon Mar 19, 2018 12:12 pm

where I am stuck is how on earth do I 1.) clear a byte 2, write to a byte and 3.) read a byte.

Clear a byte or a bit? Typically you clear / set individual bits of a byte:
https://en.wikipedia.org/wiki/Bit_manip ... g_language

For examples of how to write/read a byte, see the write8 and read8 functions.
https://github.com/adafruit/Adafruit_VL ... X.cpp#L296
https://github.com/adafruit/Adafruit_VL ... X.cpp#L257

adafruit_support_carter
 
Posts: 7643
Joined: Tue Nov 29, 2016 2:45 pm

Re: programming help for a VL6180X

by justatech on Mon Mar 19, 2018 12:51 pm

Thank you I will ponder that for a bit then dig back in
Thank you

Arlie

justatech
 
Posts: 46
Joined: Thu Oct 11, 2012 6:34 pm

Re: programming help for a VL6180X

by justatech on Fri Mar 23, 2018 1:35 pm

Ok so I tried the write8 and read8, all I get is a " write8 not defined in this scope" The same for the read8...

How and where do I define these functions?? sorry thought I was getting somewhere and then hit this brick wall

Thanks for any help

justatech
 
Posts: 46
Joined: Thu Oct 11, 2012 6:34 pm

Re: programming help for a VL6180X

by adafruit_support_carter on Fri Mar 23, 2018 1:53 pm

The read8/write8 functions are private, so you can't access them directly from your code. What you're trying to do is essentially add functionality to the library. One way to do that would be to modify the library code itself. You can use the existing code as reference for how the current functions work, like readRange:
https://github.com/adafruit/Adafruit_VL ... X.cpp#L146

You could then add new functions to the class and in those functions you'd be able to use the read8/write8 functions.

adafruit_support_carter
 
Posts: 7643
Joined: Tue Nov 29, 2016 2:45 pm

Re: programming help for a VL6180X

by justatech on Mon Apr 16, 2018 10:44 am

hello again,,,

I am now getting a weird error. not in my program but in the supporting .h file. did not change anything but gettting the following:
Code: Select all | TOGGLE FULL SIZE
Arduino: 1.8.5 (Windows 7), Board: "Adafruit Feather 32u4"

In file included from C:\Users\Adubois\Documents\Arduino\Plate_Plot_test\Plate_Plot_test.ino:9:0:

C:\Users\Adubois\Documents\Arduino\libraries\Adafruit_VL6180X/Adafruit_VL6180X.h:89:11: error: 'sysRangestart' has not been declared

    write8(sysRangestart,0x01);

           ^

C:\Users\Adubois\Documents\Arduino\libraries\Adafruit_VL6180X/Adafruit_VL6180X.h:89:25: error: expected identifier before numeric constant

    write8(sysRangestart,0x01);

                         ^

C:\Users\Adubois\Documents\Arduino\libraries\Adafruit_VL6180X/Adafruit_VL6180X.h:89:25: error: expected ',' or '...' before numeric constant

Multiple libraries were found for "Wire.h"
 Used: C:\Users\Adubois\AppData\Local\Arduino15\packages\adafruit\hardware\avr\1.4.12\libraries\Wire
 Not used: C:\Users\Adubois\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\libraries\Wire
exit status 1
Error compiling for board Adafruit Feather 32u4.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


any help appreciated

justatech
 
Posts: 46
Joined: Thu Oct 11, 2012 6:34 pm

Re: programming help for a VL6180X

by adafruit_support_carter on Mon Apr 16, 2018 11:09 am

I'm not sure what's going on. That line isn't even in the header file:
https://github.com/adafruit/Adafruit_VL ... _VL6180X.h

Did you accidentally copy / paste something in there?

adafruit_support_carter
 
Posts: 7643
Joined: Tue Nov 29, 2016 2:45 pm

Re: programming help for a VL6180X

by justatech on Mon Apr 16, 2018 11:51 am

hmmm. yes I may have. was sick last week while working on this file....

I will take it out and see what happens....

another question would be how/can I add or remove items from the header file?

the .cpp file??

sorry to bother you

justatech
 
Posts: 46
Joined: Thu Oct 11, 2012 6:34 pm

Please be positive and constructive with your questions and comments.