Using VL6180 proximity sensor to control digital potentiometer

Hello, I am extremely new to writing code and I've been attempting to control a MCP4151 digital POT with a VL6180 I2C proximity sensor.

Starting off I'm using a Micro and my goal is to control the resistance of the POT based upon the distance an object is from the sensor. I've been able to get to the point where I can read the I2C input from the sensor in the serial monitor and I'm able to send the "SPI.transfer(val)" to the pot to set a specific value.

My question is this:

Is there any way to control the POT continuously or "live" (for lack of a better term) rather than send it a specific value once with the SPI transfer line? Please forgive my complete lack of understanding... I'm only on day two of all this and before then I'd never written a single line of code in my life.

Essentially I'd like to run the data I see in the serial monitor through a "map" that puts it into the 0-255 range for the POT and have it continually update based on that value. I may be trying something thats not possible and I've scoured the forums and worn google out trying to find an answer.

I'll attach the code I'm using (I'm sure its a laughable mess to anyone who is actually proficient in this stuff)

Obviously that last line "SPI.transfer(map);" is silly and doesn't work, I just put it there temporarily to show what I am attempting to make happen.

Any advice or information would be greatly appreciated.

Thank you!

#include <VL6180X.h>
#include <Wire.h>
#include <SPI.h>
byte address = 0x11;
int tof_address = 0x29;
int CS = 4;


 
VL6180X vl;
SevSeg sevseg;

void setup() {
  pinMode(CS,OUTPUT);
  SPI.begin();
  Serial.begin(115200);
  if (!vl.init()) Serial.println("VL6180X init failed");
  Serial.println("INIT OK");
  pinMode(VL_INT_PIN, INPUT);
  digitalWrite(VL_INT_PIN,HIGH);
  pinMode(VL_SW_PIN, INPUT);
  digitalWrite(VL_SW_PIN,HIGH);

  
 
}


void loop() 
{

  
  Wire.beginTransmission(tof_address);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(tof_address, 8);
  while(Wire.available() == 0);
  int distance = Wire.read();

         Serial.print("  OUTPUT =   ");
         vl.read();
          Serial.println(vl.range);
          delay(15);
          int val = vl.range;
          val = map(val, 20, 1000, 1, 255);
          SPI.transfer(map);
          
          
}

What do those 8 bytes mean, which you read from tof_address?

Pick out the distance value, map it into the pot range, and transfer the resulting value to the pot.

Thanks for the reply!

My problem is that the only method I can find to transfer data to the pot is in the form of "SPI.transfer()". I can only send a specific value to the pot, not the continuous sensor feed if that makes any sense. Is there a code I can use to do that?

I have a much more simplified code that strips out a bunch of useless garbage by the way.

Thank you for the info!

#include <VL6180X.h>
#include <Wire.h>
#include <SPI.h>
int tof_address = 0x29;
int CS = 4;


 
VL6180X vl;
SevSeg sevseg;

void setup() {
  pinMode(CS,OUTPUT);
  SPI.begin();
  Serial.begin(115200);
  if (!vl.init()) Serial.println("VL6180X init failed");
  Serial.println("INIT OK");
  pinMode(VL_INT_PIN, INPUT);
  digitalWrite(VL_INT_PIN,HIGH);
  pinMode(VL_SW_PIN, INPUT);
  digitalWrite(VL_SW_PIN,HIGH);

  
 
}


void loop() 
{

  
         
         Wire.requestFrom(tof_address,8);
         //Here I am printing the sensor output to the serial monitor
         Serial.print("  OUTPUT =   ");
         vl.read();
          Serial.println(vl.range);
          delay(15);
          
          
          
          
}

mdlockyer77:
My problem is that the only method I can find to transfer data to the pot is in the form of "SPI.transfer()". I can only send a specific value to the pot, not the continuous sensor feed if that makes any sense. Is there a code I can use to do that?

Don't worry, the pot is updated continuously, in every run of loop(). There is no abbreviation, the new value must be obtained from the (distance) sensor, then sent to the actor (pot).