Moving UNO code with interrupt to ATTINY85

Hi

I have the following code that works on a UNO but i need to make it work on a ATTINY85

I know that the attach interrupt does not work on the ATTINY85.

What is the best way to change the interrupt to work

#include <SwitecX12.h>

const int STEPS = 265*12;
const int A_STEP = 8;
const int A_DIR = 9:
const int RESET = 10;

// Define the potentiometer pin
#define POT_PIN A0

// Define the RPM sensor pin
#define RPM_PIN 2


// Create a SwitecX25 object
SwitecX12 motor1(STEPS, A_STEP, A_DIR);  // steps for 265-degree movement

volatile int rpmCount = 0;
unsigned long lastTime = 0;
unsigned long currentTime = 0;
int rpm = 0.0;

void setup() {

  // Initialize motor
  digitalWrite(RESET, HIGH);  
  motor1.zero();
  motor1.setPosition(0);  // Move needle to zero position



  // Setup RPM sensor interrupt
  pinMode(RPM_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(RPM_PIN), rpmISR, RISING);
}


void loop() {
  currentTime = millis();
  // Calculate RPM
  if (currentTime - lastTime >= 1000) {  // Calculate every 1 second


    noInterrupts();
    rpm = (int)(rpmCount * (60.0 / 0.66644) / 6);  // Convert to RPM
    rpmCount = 0;
    interrupts();
    lastTime = currentTime;


    // Read potentiometer value for fine adjustment
    int potValue = analogRead(POT_PIN);
    int fineAdjustment = map(potValue, 0, 1023, -500, 500);      // Adjust between -500 and 500 RPM
    int adjustedRPM = constrain(rpm + fineAdjustment, 0, 5000);  // Constrain to 0-5000 RPM



    // Map RPM to motor position (0 to 260 degrees)
    int motorPosition = map(adjustedRPM, 0, 5000, 0, STEPS);  // Adjust the steps according to your motor's range
    motor1.setPosition(motorPosition + 16);
  }

  // Update motor movement
  motor1.update();
}

// RPM sensor interrupt service routine
void rpmISR() {
  rpmCount++;
}

On the ATTINY85 I have the pins set as

const int A_STEP = 4;
const int A_DIR = 1:
const int RESET = 0;

// Define the potentiometer pin
#define POT_PIN A3

// Define the RPM sensor pin
#define RPM_PIN 2

I think interrupts work on the ATtiny85. Check any pinout for PCINTx. Here is a simulation that might help...

  • some libraries will not work with ATtiny85.

Thank you

Can I use INT0 instead of PCINT0 or can PCINT0 be used with rising edge

INT0 on the ATtiny85 works just like INT0 on the Uno. Pin change interrupts are just that, they react to either change in the pin status.

Will this work before I test it on my tiny

#include <AccelStepper.h>
#include <avr/interrupt.h>
#include <avr/io.h>

// defines pins numbers for X12
const int stepPin = 4;
const int directionPin = 1;
const int enablePin = 0;

// Define the potentiometer pin
const int POT_PIN = A3;

// Define the RPM sensor pin
const int RPM_PIN = PINB2; // PB2 INT0

const int STEPS = 265 * 12;

// Define a stepper and the pins it will use
// 1 or AccelStepper::DRIVER means a stepper driver (with Step and Direction pins)
AccelStepper motor(AccelStepper::DRIVER, stepPin, directionPin);

volatile int rpmCount = 0;
unsigned long lastTime = 0;
unsigned long currentTime = 0;
int rpm =0.0;

void setup() {

  cli(); // CLEAR interrupts enabled while configuring interrupts
  GIMSK |= (1 << RPM_PIN); // Enables external interrupt
  MCUCR |= B00000011; // RISING mode
  sei(); // SET interrupts enabled after confiuration is complete

  pinMode(RPM_PIN, INPUT);
 

  motor.setMaxSpeed(1000);
  motor.setSpeed(500);
  digitalWrite(enablePin, HIGH);
  motor.moveTo(0);
}

void loop() {

  currentTime = millis();

  if (currentTime - lastTime >= 1000) {

    cli();

    rpm = (int)(rpmCount * (60.0 / 0.66644)/6);
    rpmCount = 0;

    sei();
    
    lastTime = currentTime;

    int potValue = analogRead(POT_PIN);
    int fineAdjustment = map(potValue, 0, 1023, -500, 500);
    int adjustedRPM = constrain(rpm + fineAdjustment, 0, 5000);

    int motorPosition = map(adjustedRPM, 0, 5000, 0, STEPS);

    motor.moveTo(motorPosition + 16);
    
  }

}

ISR(INT0_vect) {
  cli();
  rpmCount++;
  sei();
}

Test it in the simulator.

will the simulator have my driver chip on it

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