How can i create a internal interrupt with a analogic value?

How can i create a internal interrupt to move a servo instantly when i move the trim-pot? I get some "delay" while the program is doing the instructions and move the servo some seconds after i move the trim-pot.

Anyone have an example?

Thankou!

I get some "delay" while the program is doing the instructions

What program? Post the code, using code tags, and forum members will help you fix it.

You can't. Reading the analog inputs takes time, and it requires software to decide what to do with the value after it's read. There is no mechanism for triggering an interrupt based on the value of an analog read.

Regards,
Ray L.

Look up blink without delay and running multiple devices.

elyancorrea:
How can i create a internal interrupt to move a servo instantly when i move the trim-pot?

Define "instantly". I hope you don't mean "in zero seconds" because code takes time to execute.

The code in planning and implementing a program includes moving a servo with a pot. I have not noticed any delay in its response and it does not use interrupts.

...R

This might be too patronising/newbie for you.... if so apologies, i just thought maybe people were assuming some knowledge you might not have.

Interrupts have their place, but often you can solve the problem by ensuring your code loops run fast enough to allow 'polling' (periodic checking in a loop). To do this you need to make sure you dont have chunks of code that sit waiting for things before they execute (things like while loops etc).

Well written code will execute your main loop very rapidly, so to a human (we're slow) things look to be happening immediately.

Perhaps you could post your code, in the hope of some constructive comments to improve its execution speed.

It takes less than 1/8th of a millisecond to read analog. You see motion at 24 FPS. TV is 30, that's 33 ms per frame and an analog read takes a small part of 1. It's not the reads that are lagging your code. You might inspect it for delay() or the use of some other stop-and-wait command.

Sorry, my fault.

The code is a bit messy, but works, i will try to straighten up later. :smiley:

This code is a joint of some codes running together.

//DHT9X
//Sensor | Arduino
//1 = D5
//4 = D4
//Voltage
//Out = A0
//Current
//Sig = A1
//PWM
//D9
//Pot
//A2



#include <Sensirion.h>
#include <Servo.h>

// Referido ao DHT

const uint8_t dataPin  =  4;
const uint8_t clockPin =  5;

float temperature;
float humidity;
float dewpoint;

Sensirion tempSensor = Sensirion(dataPin, clockPin);

// Servo

Servo myservo;

// PWM
 
int potpin = 2;
int val; 

// Current Sens

int sensorPin =A1;
 
int sensorValue_aux = 0;
float sensorValue = 0;
float currentValue = 0;
float voltsporUnidade = 0.004887586;// 5%1023

// Referido ao Excel

int ROW = 0;         // variavel que se refere as linhas do excel
int LABEL = 1;   

void setup(){
  
// Referido ao pino do servo
  
 myservo.attach(9);
 
 // Referido ao pino do sensor de corrente
 
  pinMode(sensorPin, INPUT);
  
  // Referido a comunicação e Excel

Serial.begin(9600);                     //  inicialização da comunicação serial
Serial.println("CLEARDATA");            // Reset da comunicação serial
Serial.println("LABEL,Time,PWM,Temp,Hum,Dew,Voltage,Current");   // Nomeia as colunas
}

void loop(){

// Temperatura, Umidade, P. Orvalho
  
  tempSensor.measure(&temperature, &humidity, &dewpoint);

// Tensão

int VoltageValue = analogRead(A0);
float voltage = VoltageValue / 43.38;

// Corrente

for(int i=100; i>0; i--){
    sensorValue_aux = (analogRead(sensorPin) -511); // le o sensor na pino analogico A0 e ajusta o valor lido ja que a saída do sensor é (1023)vcc/2 para corrente =0
    sensorValue += pow(sensorValue_aux,2); // somam os quadrados das leituras.
  }
 
  sensorValue = (sqrt(sensorValue/ 100)) * voltsporUnidade; // finaliza o calculo da méida quadratica e ajusta o valor lido para volts
  currentValue = (sensorValue/0.185); // calcula a corrente considerando a sensibilidade do sernsor (185 mV por amper)
  
  
// PWM (Servo & Potênciometro)
  
val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 
myservo.write(val); 

// Incrementa linha no Excel

ROW++; // incrementa a linha do excel para que a leitura pule de linha em linha

// Começa a printação

Serial.print("DATA,TIME,"); //inicia a impressão de dados, sempre iniciando 
Serial.print(val); 
Serial.print(",");
Serial.print(temperature); 
Serial.print(",");
Serial.print(humidity);
Serial.print(",");
Serial.print(dewpoint);
Serial.print(",");
Serial.print(voltage);
Serial.print(",");
Serial.println(currentValue,3);

sensorValue =0;
 
  delay(100);


if (ROW > 100) //laço para limitar a quantidade de dados
{
ROW = 0;
Serial.println("ROW,SET,2"); // alimentação das linhas com os dados sempre iniciando
}



}

I will straighten the code, and repost ASAP.

(and some comments are in Portuguese, i will fix it too.)

OK, Im also a novice, but the obvious questions:

  1. Why do you take 100 ADC conversions in every cycle when you seem to only store one value? What happens if you just do this once ? (lose the for loop)
for(int i=100; i>0; i--){
    sensorValue_aux = (analogRead(sensorPin) -511); // le o sensor na pino analogico A0 e ajusta o valor lido ja que a saída do sensor é (1023)vcc/2 para corrente =0
    sensorValue += pow(sensorValue_aux,2); // somam os quadrados das leituras.
  }

becomes:

sensorValue_aux = (analogRead(sensorPin) -511); // le o sensor na pino analogico A0 e ajusta o valor lido ja que a saída do sensor é (1023)vcc/2 para corrente =0
    sensorValue += pow(sensorValue_aux,2); // somam os quadrados das leituras.
  1. Do you really need a 100ms delay in the loop? I couldnt understand what benefit it gave you, but maybe thats just me :slight_smile:

delay(100);

  1. Your serial comms is set very slow, is there some reason not to use 115200?

There are other improvements you could make, but certainly the first two items should see a huge improvement.

Also, I got into the habit of making my main loop track a looptimer variable (looptimer=millis() at start and Serial.print(millis()-looptimer) at the end of the loop). Maybe that helps you find where your code is slowing you down.

scrumfled:

  1. Why do you take 100 ADC conversions in every cycle when you seem to only store one value? What happens if you just do this once ? (lose the for loop)
for(int i=100; i>0; i--){

sensorValue_aux = (analogRead(sensorPin) -511); // le o sensor na pino analogico A0 e ajusta o valor lido ja que a saída do sensor é (1023)vcc/2 para corrente =0
   sensorValue += pow(sensorValue_aux,2); // somam os quadrados das leituras.
 }




becomes:



sensorValue_aux = (analogRead(sensorPin) -511); // le o sensor na pino analogico A0 e ajusta o valor lido ja que a saída do sensor é (1023)vcc/2 para corrente =0
   sensorValue += pow(sensorValue_aux,2); // somam os quadrados das leituras.

Improve the accurate of reading.

sensorValue += pow(sensorValue_aux,2)

In addition to the silly delays, this is the slowest possible way to square a number.

elyancorrea:
Improve the accurate of reading.

As far as i know, the ADC is only inaccurate when its either first powered, or when it comes out of a sleep mode. The references I've seen refer to that inaccuracy only applying to the first reading.

Personally I'd take two readings (reduce the int value to 2 in the loop), if i was worried about it. the extra 98 seem overkill :wink:

Taking a hundred readings for reading a trim pot seems major overkill.