1

The following code is for a pyrograph power supply that uses an ATtiny85. I want to change it, so instead of a push button I have a potentiometer being the control of the temperature. Can anyone guide me on how to accomplish this with this code?

//Written by Tyler Gerritsen
//2017-10-21
//Pyrography Pen Power Supply Controller
//Written for ATTiny85 w/ Internal 1MHZ Clock

const byte btn = 2;
const byte led = 1;
const byte pwr = 0;
byte rate = 0;
unsigned long nextChange = 0;
boolean onOff = false;
unsigned long blinkyCount = 2000000000;
unsigned long nextBlink = 0;
unsigned long debounceTimer = 2000000000;
const unsigned int debounceDelay = 400;
const unsigned int onDelay[5] = {6800, 6300, 5800, 6300, 4800};
const unsigned int offDelay = 5000;

void setup() {
  pinMode(btn, INPUT_PULLUP);
  digitalWrite(btn, HIGH);
  pinMode(led, OUTPUT);
  pinMode(pwr, OUTPUT);
  digitalWrite(pwr, LOW);
  debounceTimer = 60000000;
  while (digitalRead(btn)) {
    checkButton();
  }
  analogWrite(led, 0);
  nextChange = micros();
  debounceTimer = millis() + debounceDelay;
  blinkyCount = 1;

}

void loop() {
  runBurner();
  checkButton();
}

void runBurner() {
  if (micros() > nextChange) {
    if (onOff) {
      digitalWrite(pwr, LOW);
      onOff = false;
      nextChange += onDelay[rate];
    } else {
      digitalWrite(pwr, HIGH);
      onOff = true;
      nextChange += offDelay;
    }
  }
}

void checkButton() {
  if (!digitalRead(btn) && millis() > debounceTimer) {
    debounceTimer = millis() + debounceDelay;
    rate++;
    rate %= 5;
    blinkyCount = (rate) * 2 + 5;
    nextBlink = millis();
  }
  if (blinkyCount > 1 && millis() > nextBlink) {
    if (blinkyCount % 2) {
      digitalWrite(led, LOW);
      blinkyCount--;
      nextBlink += 150;
    } else {
      analogWrite(led, 10);
      nextBlink += 150;
      blinkyCount--;
    }
  } else if (blinkyCount == 1) {
    analogWrite(led, 10);
    blinkyCount = 0;
  }
}
2

This is a simple sketch that shows how to read a potentiometer and do something with the value, namely, adjust the delay of the blinking led.

[Source: https://www.arduino.cc/en/tutorial/potentiometer]

/* Analog Read to LED
 * ------------------ 
 *
 * turns on and off a light emitting diode(LED) connected to digital  
 * pin 13. The amount of time the LED will be on and off depends on
 * the value obtained by analogRead(). In the easiest case we connect
 * a potentiometer to analog pin 2.
 *
 * Created 1 December 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 * http://arduino.berlios.de
 *
 */

int potPin = 2;    // select the input pin for the potentiometer
int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

void setup() {
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}

void loop() {
  val = analogRead(potPin);    // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(val);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  delay(val);                  // stop the program for some time
}

So, in every loop iteration you read the potPin and using that value for delay.

AnalogRead gives you a value between 0-1023, which you have to adjust to give the desired value for temperature control. That is all you need.

Use map function to adjust the value. Look at https://www.arduino.cc/reference/en/language/functions/math/map/

  • thank you for the help. I will try this, I am new to programming this type of things I will keep you guys updated – Gabriel Rivas Feb 19 '18 at 18:56
1

This bit of code from you is the key part:

if (!digitalRead(btn) && millis() > debounceTimer) {
  debounceTimer = millis() + debounceDelay;   
  rate++;   
  rate %= 5;   
  blinkyCount = (rate)*2+5;   
  nextBlink = millis();   
}   

Specifically, the rate++ and rate %= 5 lines.

You have a 5-step temp setting. The analogRead() function returns a value from 0 to 1023, which you have to map to 0 to 4 (5 steps). Also, you can read the Potentiometer without worry about debouncing; thus:

if (true) {
  rate = map(analogRead(A0), 0, 1023, 0, 4);
  blinkyCount = (rate)*2+5;   
  nextBlink = millis();   
}   

If you desire to have more than 5 "settings" for temperature, you could get a continuous range by eliminating the table onDelay[] and mapping the analog value between the high and low range of the acceptable onDelay values, so you get a finer control of the "on" delay time.

// No longer needs to be an array or a const
unsigned int onDelay = 6800;

Within checkButton():

if (true) {

  // Key Change here
  onDelay = map(analogRead(A0), 0, 1023, 6800, 4800);

  blinkyCount = (rate)*2+5;   
  nextBlink = millis();   
}   

Update to runBurner():

void runBurner(){   
  if (micros() > nextChange){   
    if (onOff){   
      digitalWrite(pwr, LOW);   
      onOff = false;   

      // Key Change here:
      nextChange += onDelay;

    }   
    else {   
      digitalWrite(pwr, HIGH);   
      onOff = true;   
      nextChange += offDelay;   
    }   
  }   
}   
  • thank you so much this really helps clear some things, and will try this code to give it a test, I really appreciate your time helping me with this – Gabriel Rivas Feb 20 '18 at 16:32
  • You forgot the rollover issue. – user31481 Feb 20 '18 at 16:50
  • Correct - I only addressed the value-from-pot question and did not further debug the code. – jose can u c Feb 20 '18 at 18:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.