Triggering Potentiometer-Servo control with interrupt.

Hello, for one of my course projects, the professor is making us make a circuit where an interrupt triggers a code that allows a potentiometer to be used to manipulate a servo. I made an interrupt, and a code for controlling thew servo which I put in an infinite while loop so the code would just sit there forever once the interrupt is triggered…only thing is, it doesn’t work at all. The servo just turns all the way in one direction once the code is triggered and stays there. I will include a circuit diagram and code if anyone can provide guidance, thank you.

Please post your code here rather than a picture of it and use code tags when you do to make it easy to copy for examination in an editor

Which Arduino board are you using, what are you using to generate the interrupt and which pin is it connected to ?

Does the project brief specifically say you need to use an interrupt, or is that your interpretation?

UKHeliBob:
Which Arduino board are you using, what are you using to generate the interrupt and which pin is it connected to ?

I’m using an Arduino uno, and we have to use an interrupt to trigger the code. I am using pin 2 (INT0) for the interrupt.

#include <Servo.h>

Servo servo;

int pos;

void setup()
{
  pinMode(0, INPUT);
  pinMode(2, INPUT);
  pinMode(5, OUTPUT);
  servo.attach(5);
  attachInterrupt(digitalPinToInterrupt(2), Servo_run, RISING);
}

void loop(){
}

void Servo_run(){

  while(1==1){
  pos = analogRead(0);
  pos = map(pos, 0, 1023, 0, 180);
  servo.write(pos);
  }
}

Call Servo_run as a normal function and in it print the value of pos. What do you see ?

NOTE : you cannot use Serial.print() in an ISR

Try using A0 as the pin number in analogRead(), assuming that you actually have the post connected to A0 and not 0.   pinMode(0, INPUT);
is not needed because pins default to INPUT and for analogue pins it is irrelevent anyway and it refers to a pin 0 and not pin A0 anyway

I have taken off the pin 0 assignment, but unfortunately it still does not function correctly.....

What about my other suggestion of calling the function normally and printing pos ?

An infinite loop in interrupt context isn’t going to allow the servo library to do its stuff.

I can’t really tell what the interrupt is even doing there.

I thought that you couldn't use delays or serial statements in an interrupt? And the interrupt is basically functioning as a switch.....it doesn't make much sense to use an interrupt that way, but this is what was on the quiz a few days ago. He doesn't know what he is talking about half the time, so I am sure that this type of circuit might just fundamentally not work with an interrupt, but I am getting evidence it doesn't before I email him.

You shouldn’t put serial I/O in interrupt context (it may work; it may not work) and long delays may not work, but infinite loops in interrupt context are a definite no-no, unless you explicitly re-enable interrupts, which you are not doing.

Can't you just use the ISR to set a flag? Then do the rest in loop(). If the flag is set read the pot, write to the servo then unset the flag.

Generally, setting flags with interrupts is a good idea so you're in the ISR for as short a time as possible.

Steve