Driving a digipot from a millis output with potentiometers for adjusting

Hello,

I am trying to drive a digipot with a duration time i receive from my timer. I would like to use the input from 2 potentiometers to set the thresholds. I have built the circuit and the arduino is running the code i have created so far to connect to the digipot, read the timers and read the pots but I'm a little lost as to how i would tie the rest of it together. Any help would be greatly appreciated.

these would be the thresholds to the 2 pots
const int POTL
const int lowlow = 5000
const int lowhigh = 3000
const int POTH
const int highlow = 3000
const int highhigh = 1000

#include <Adafruit_DS3502.h>
Adafruit_DS3502 ds3502 = Adafruit_DS3502();
#define WIPER_VALUE_PIN A0

byte button = 2;
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
unsigned long timerMillis;
byte timerRunning;
const int POTL = 1;  // Pot on Analog Pin 1
const int POTH = 2;  // Pot on Analog Pin 2
int lowspeed = 0;    // Low speed set
int highspeed = 0;   // High Speed set
int lowrpm = 0;      // Low RPM kick in
int highrpm = 0;     // High RPM kick in
const int lowlow = 5000
const int lowhigh = 3000
const int highlow = 3000
const int highhigh = 1000

void setup() {
  pinMode (button, INPUT);
  Serial.begin(9600);
  while (!Serial) {
    delay(1);
  }

  Serial.println("Adafruit DS3502 Test");

  if (!ds3502.begin()) {
    Serial.println("Couldn't find DS3502 chip");
    while (1);
  }
  Serial.println("Found DS3502 chip");
}
void loop()
{
  if (timerRunning == 0 && digitalRead(button) == HIGH) {
    startTime = millis();
    timerRunning = 1;
    timerMillis = millis();
  }
  if (timerRunning == 1 && digitalRead(button) == LOW) {
    endTime = millis();
    timerRunning = 0;
    duration = endTime - startTime;
    lowspeed = analogRead(POTL);
    highspeed = analogRead(POTH);
    Serial.print("LOW ");
    Serial.print(lowspeed);
    Serial.print("\t\t");
    Serial.print("HIGH ");
    Serial.println(highspeed); Serial.print ("time in milliseconds: ");
    Serial.println (duration);
  }
  if (timerRunning == 1 && millis() - timerMillis >= 5000)
  {
    timerRunning = 0;
    duration = 5000;
    lowspeed = analogRead(POTL);
    highspeed = analogRead(POTH);
    Serial.print("LOW ");
    Serial.print(lowspeed);
    Serial.print("\t\t");
    Serial.print("HIGH ");
    Serial.println(highspeed); Serial.print ("time in milliseconds: ");
    Serial.println (duration);
  }
}

Why are you starting a new post when continuing your last post would help volunteers with continuity ?


You never set the wiper, why ?

ds3502.setWiper( some value );

Sorry i was setting up here for the complete project now that i have the timer part sorted. Im just learning my way around the arduino landscape. I have added
ds3502.setWiper(0);
to the start of the loop

Fully describe what you want to happen here:


  if (timerRunning == 1 && digitalRead(button) == LOW) {
    endTime = millis();
    timerRunning = 0;
    duration = endTime - startTime;
    lowspeed = analogRead(POTL);
    highspeed = analogRead(POTH);
    Serial.print("LOW ");
    Serial.print(lowspeed);
    Serial.print("\t\t");
    Serial.print("HIGH ");
    Serial.println(highspeed); Serial.print ("time in milliseconds: ");
    Serial.println (duration);
  }

That is just so i can see the outputs that i am getting while i build the mechanical side of the machine and figure out the finer details of where i need to set the parameters. the finished program won't need to output any information to the serial monitor just drive the digipot according to the time interval

“I am trying to drive a digipot with a duration time i receive from my timer.”

Where in your sketch are you doing this ?


BTW
Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.

I have not written the code for that yet, I’m not really sure how I go about it, this is where I’m stuck

What wheel sensor you are using ?


A4 and A5 should not be connected to 5v ?


So you have a wheel sensor that needs a pull down resistor, 2 potentiometers, 1 switch, a digital potentiometer ?


What is a pedal box ?


Please confirm BOTH the Red power rails on the solderless breadboard are connected to the Arduino +5v pin ?

Same with the Blue GND power rails connected to Arduino GND.

We cannot see where the top red and blue power rails are connected to the bottom red and blue power rails.


The black sensor (?) cable looks like it is connected between Pin D2 and GND with a switch connected between GND and D2. There is a resistor from D2 to the red power rail. This is contrary to your schematic ?

The switch being connect from D2 to GND means a closed switch is a LOW, not a HIGH:
if (timerRunning == 0 && digitalRead(button) == HIGH)

Sorry that schematic is a little out of date from before i had the breakout board, i have updated and will include.

The wheel sensor is a simple magnetic switch from a bike computer that is being used to pick up the speed of the crank on a stationary bike.

A4 an A5 are not connected to 5V as per updated drawing.

The push button is just to replicate the wheel sensor when im not connecter to the actual bike.

I want to use the 2 potentiometers to adjust the lower and upper limit of where i will map the digipot too.

The pedal box just contains the actual potentiometer that im replacing with the digipot.

Both sets of rails on the breadboard are connected the the Arduino.

Everything is working, the timer works as needed and i can set the digipot but im not sure how i go about writing the code to map the pots to their time variables POTL (lowlow - low high) POTH (highlow - high high) and then have those mapped to the digipot according to the duration var

Things make a bit more sense now.



For confirmation in the final circuit:

You will have 2 manual potentiometers, one wheel sensor and the digital potentiometer will be sending a resistance value to the ‘pedal box’

The 2 manual potentiometers will set the lower and upper values that the digital potentiometer can vary between.

The wheel sensor timing will determine the setting of the digital potentiometer wiper.

Is this correct ?

The manual pots will determine the range of the timing,

say POTL is set all the way down (lowlow) and POTH is all the way down (highlow) if the timer was 5000 the digipot wiper would be a 0 and if the timer was at 3000 it would set the wiper to 127, if POTH was all the way to (highhigh) then it would take the timer to be at 1000 to get to the wiper to 127 so at all times the wiper has full range but how it determines the timer is set by the pots

const int lowlow = 5000;
const int lowhigh = 3000;
const int highlow = 3000;
const int highhigh = 1000;

Here is your sketch modified a bit (well more than a bit).

Can you check that it works as good as the version as in your top post.

This version still needs more work.

// Version  YY/MM/DD  Description
// 1.00     21/06/11  Converted old version to this
// 1.01     21/06/12  Added thresholds  


#include <Adafruit_DS3502.h>                  // <-----<<<<<
Adafruit_DS3502 ds3502 = Adafruit_DS3502();   // <-----<<<<<
the DS3502 can be set from 0 to 127 using I2C // <-----<<<<<


#define magnetDetected       LOW
#define noMagnet             HIGH

#define weAreTiming          true
#define weAreNotTiming       false

const byte WIPER_VALUE_PIN = A0;
const byte POTL            = A1;  // Pot on Analog Pin 1
const byte POTH            = A2;  // Pot on Analog Pin 2

bool timerRunningFlag      = weAreNotTiming;

const byte sensor          = 2;
const byte heartbeatLED    = 13;

byte lastWheelState        = noMagnet;
byte digitalPot;

int lowspeed               = 0;   // Low speed set
int highspeed              = 0;   // High Speed set
int lowrpm                 = 0;   // Low RPM kick in
int highrpm                = 0;   // High RPM kick in
int lowValue              = 0;
int highValue                = 0;

const int lowLow           = 5000; //reading 0
const int lowHigh          = 3000; //reading 1023

const int highLow          = 3000; //reading 0
const int highHigh         = 1000; //reading 1023

//timing stuff
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
unsigned long timerMillis;
unsigned long heartbeatMillis;
unsigned long wheelMillis;

//*********************************************************************
void setup()
{
  pinMode (sensor, INPUT_PULLUP);

  pinMode (heartbeatLED, OUTPUT);

  Serial.begin(9600);

  while (!Serial)
  {
    delay(1);
  }

  Serial.println("Adafruit DS3502 Test");

  if (!ds3502.begin())                             // <-----<<<<<
  {                                                // <-----<<<<<
    Serial.println("Couldn't find DS3502 chip");   // <-----<<<<<
    while (1);                                     // <-----<<<<<
  }                                                // <-----<<<<<

  Serial.println("Found DS3502 chip");

} //END of setup()


//*********************************************************************
void loop()
{
  //***********************
  //time to toggle the heartbeatLED ?
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //***********************
  //time to check the wheel sensor ?
  if (millis() - wheelMillis >= 10)
  {
    //restart the TIMER
    wheelMillis = millis();

    checkWheelSensor();
  }

  //***********************
  //if we are timing, have we reached the upper timing limit ?
  if (timerRunningFlag == true && millis() - timerMillis >= 5000)
  {
    //disable the timing
    timerRunningFlag = false;

    duration = 5000;

    lowspeed = analogRead(POTL);  //0 to 1023
    lowValue = map(lowspeed, 0, 1023, lowLow, lowHigh);     //5000-3000

    highspeed = analogRead(POTH); //0 to 1023
    highValue = map(highspeed, 0, 1023, highLow, highHigh); //3000-1000

    Serial.print("LOW ");
    Serial.print(lowspeed);
    Serial.print(" ");
    Serial.print(lowValue);

    Serial.print("\t\t");

    Serial.print("HIGH ");
    Serial.print(highspeed);
    Serial.print(" ");
    Serial.println(highValue);

    Serial.print ("Time in milliseconds: ");
    Serial.println (duration);

    printDigitalPot();
  }

} //END of loop()


//*********************************************************************
void checkWheelSensor()
{
  byte thisState = digitalRead(sensor);

  //***********************
  if (lastWheelState != thisState)
  {
    //update to the new state
    lastWheelState = thisState;

    //***********************
    //if we are not Timing has the sensor detected the magnet ?
    if (timerRunningFlag == weAreNotTiming && thisState == magnetDetected)
    {
      //enable timing
      timerRunningFlag = weAreTiming;

      //start this TIMER
      startTime = millis();

      //start this TIMER
      timerMillis = millis();
    }

    //***********************
    if (timerRunningFlag == weAreTiming && thisState == noMagnet)
    {
      endTime = millis();

      //disable the timing
      timerRunningFlag = weAreNotTiming;

      duration = endTime - startTime;

      lowspeed = analogRead(POTL);  //0 to 1023
      lowValue = map(lowspeed, 0, 1023, lowLow, lowHigh);

      highspeed = analogRead(POTH); //0 to 1023
      highValue = map(highspeed, 0, 1023, highLow, highHigh);

      Serial.print("LOW ");
      Serial.print(lowspeed);
      Serial.print(" ");
      Serial.print(lowValue);

      Serial.print("\t\t");

      Serial.print("HIGH ");
      Serial.print(highspeed);
      Serial.print(" ");
      Serial.println(highValue);

      Serial.print ("Time in milliseconds: ");
      Serial.println (duration);

      printDigitalPot();
    }

  } //END of  if(lastWheelState != thisState)

} //END of checkWheelSensor()


//*********************************************************************
void printDigitalPot()
{
  Serial.print("Digital potentiometer set to: ");
  int temp = map( duration, highValue, lowValue, 127, 0);
  digitalPot = constrain(temp, 0, 127);
  Serial.println(digitalPot);
  Serial.println("");

} //END of  printDigitalPot()

What happens if:
say POTL is set all the way down (lowlow 5000) and POTH is all the way down (highlow 3000) if the timer was 5000 the digipot wiper would be a 0 and if the timer was at 3000 it would set the wiper to 127
And if the timer was 1000, what would the wiper be set to ?


What happens if:
if POTH was all the way to (highhigh 1000) and the timer was 500, what would the wiper be set to ?



What happens if:
say POTL is set all the way down (lowHIGH 3000) and POTH is all the way down (highlow 3000)
And if the timer was 3000, what would the wiper be set to ?

I am running the new code, the magnet timer works but it doesn't seem to time out and output after the 5 seconds anymore.

I would like to constrain things so that if POTH was set to (highhigh) and the timer was 500 it would just have the wiper set to 127

Maybe we need to change the (lowhigh) to 2800 and then there will still be some range in th

This is what prints here if the test switch is held for 5000ms:

Adafruit DS3502 Test
Found DS3502 chip
LOW 2 4997 HIGH 3 2995
Time in milliseconds: 5000
Digital potentiometer set to: 0

Minor changes added:

// Version  YY/MM/DD  Description
// 1.00     21/06/11  Converted old version to this
// 1.01     21/06/12  Added thresholds
// 1.02     21/06/12  Added comments

#include <Adafruit_DS3502.h>                  // <-----<<<<<
Adafruit_DS3502 ds3502 = Adafruit_DS3502();   // <-----<<<<<
the DS3502 can be set from 0 to 127 using I2C // <-----<<<<<


#define magnetDetected       LOW
#define noMagnet             HIGH

#define weAreTiming          true
#define weAreNotTiming       false

const byte WIPER_VALUE_PIN = A0;
const byte POTL            = A1;  // Pot on Analog Pin 1
const byte POTH            = A2;  // Pot on Analog Pin 2

bool timerRunningFlag      = weAreNotTiming;

const byte sensor          = 2;
const byte heartbeatLED    = 13;

byte lastWheelState        = noMagnet;
byte digitalPot;

int lowspeed               = 0;   // Low speed set
int highspeed              = 0;   // High Speed set
int lowrpm                 = 0;   // Low RPM kick in
int highrpm                = 0;   // High RPM kick in
int lowValue               = 0;
int highValue              = 0;

const int lowLow           = 5000; //reading 0
const int lowHigh          = 3000; //reading 1023

const int highLow          = 3000; //reading 0
const int highHigh         = 1000; //reading 1023

//timing stuff
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
unsigned long timerMillis;
unsigned long heartbeatMillis;
unsigned long wheelMillis;

//*********************************************************************
void setup()
{
  pinMode (sensor, INPUT_PULLUP);

  pinMode (heartbeatLED, OUTPUT);

  Serial.begin(9600);

  while (!Serial)
  {
    delay(1);
  }

  Serial.println("Adafruit DS3502 Test");

  if (!ds3502.begin())                             // <-----<<<<<
  {                                                // <-----<<<<<
    Serial.println("Couldn't find DS3502 chip");   // <-----<<<<<
    while (1);                                     // <-----<<<<<
  }                                                // <-----<<<<<

  Serial.println("Found DS3502 chip");

} //END of setup()


//*********************************************************************
void loop()
{
  //***********************
  //time to toggle the heartbeatLED ?
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //***********************
  //time to check the wheel sensor ?
  if (millis() - wheelMillis >= 10)
  {
    //restart the TIMER
    wheelMillis = millis();

    checkWheelSensor();
  }

  //***********************
  //if we are timing, have we reached the upper timing limit ?
  if (timerRunningFlag == weAreTiming && millis() - timerMillis >= 5000)
  {
    //disable the timing
    timerRunningFlag = weAreNotTiming;

    duration = 5000;

    printDigitalPot();
  }

  //***********************
  //Other non-blocking code goes here
  //***********************

} //END of loop()


//*********************************************************************
void checkWheelSensor()
{
  byte thisState = digitalRead(sensor);

  //***********************
  //was there a change in state ?
  if (lastWheelState != thisState)
  {
    //update to the new state
    lastWheelState = thisState;

    //***********************
    //if we are 'not' timing has the sensor detected the magnet ?
    if (timerRunningFlag == weAreNotTiming && thisState == magnetDetected)
    {
      //enable timing
      timerRunningFlag = weAreTiming;

      //start this TIMER
      startTime = millis();

      //start this TIMER
      timerMillis = millis();
    }

    //***********************
    if (timerRunningFlag == weAreTiming && thisState == noMagnet)
    {
      //disable the timing
      timerRunningFlag = weAreNotTiming;

      endTime = millis();

      duration = endTime - startTime;

      printDigitalPot();
    }

  } //END of  if(lastWheelState != thisState)

} //END of checkWheelSensor()


//*********************************************************************
void printDigitalPot()
{
  //read manual pots, map to the required thresholds
  lowspeed = analogRead(POTL);  //0 to 1023
  lowValue = map(lowspeed, 0, 1023, lowLow, lowHigh);

  highspeed = analogRead(POTH); //0 to 1023
  highValue = map(highspeed, 0, 1023, highLow, highHigh);

  Serial.print("LOW ");
  Serial.print(lowspeed);
  Serial.print(" ");
  Serial.print(lowValue);

  Serial.print("\t\t");

  Serial.print("HIGH ");
  Serial.print(highspeed);
  Serial.print(" ");
  Serial.println(highValue);

  Serial.print ("Time in milliseconds = ");
  Serial.println (duration);

  //map the sensor duration to the potentiometer threshold range to 0-127
  Serial.print("Digital potentiometer set to: ");
  int temp = map( duration, highValue, lowValue, 127, 0);

  //stay within the 0-127 range
  digitalPot = constrain(temp, 0, 127);
  Serial.println(digitalPot);
  Serial.println("");

} //END of  printDigitalPot()

These may be need the other way around in your setup, i.e.:

  #define magnetDetected       HIGH
  #define noMagnet             LOW

It's looking pretty good, thank you for your help. Im going to be focused on building the mechanical side of the machine now so i'll come back here if i have any problems as it all comes together

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.