control delay

hi everyone.

i want to be able to slow down and increase the speed at which the loop repeats, ive been able to do this using the delay() command, but im trying to find out if its possible to control the delay command with a potentiometer???.

Yes it is. Read the pot and use the reading you get in the delay statement. If you want a greater range of delays then multiply the reading by something before using it in the delay.

i quite new to arduino and i just cant figure this out, been trying for a while now and can not get it to work. would yu by chance have any examples you can point me too

(deleted)

#include <Wire.h>

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;

unsigned int frequencyB;
double frequency = 0;

int scan = 9;
int level = 1;
int jump = 1;
int sweep = 8;

void setup()
{
pinMode(scan, OUTPUT);
Wire.begin();
frequency = 0; //starting frequency
setFrequency();
}

void loop()
{
Serial.begin(9600);

// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1024.0);
// print out the value you read:
Serial.println(voltage);

// set the level of pin 9:
analogWrite(scan, level);

// change the level for next time through the loop:
level = level + jump;

// reverse the direction of the jump at the ends of the scan:
if (level <= 0 || level >= 255) {
jump = -jump;
}

// read the input on analog pin 02:
int delay = analogRead(A2);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float = delay * (5.0 / 1024.0);
// print out the value you read:
Serial.println(voltage);

// set the level of pin 8:
analogWrite(delay, level);

// wait for ? milliseconds to see the jump
delay();

int reading = analogRead(0);
//frequency = map((float)reading, 0.1, 1024.0, 87.5, 108);

frequency = ((double)reading * (108.0 - 87.5)) / 1024.0 + 87.5;
frequency = ((int)(frequency * 10)) / 10.0;

setFrequency();
Serial.println(frequency);
}

void setFrequency()
{
frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
frequencyH = frequencyB >> 8;
frequencyL = frequencyB & 0XFF;
delay(100);
Wire.beginTransmission(0x60);
Wire.write(frequencyH);
Wire.write(frequencyL);
Wire.write(0xB0);
Wire.write(0x10);
Wire.write((byte)0x00);
Wire.endTransmission();
delay(100);
}

 float = delay * (5.0 / 1024.0);

Will that compile? Both float and delay are reserved words?

What is the point of this?

  frequency = ((double)reading * (108.0 - 87.5)) / 1024.0 + 87.5;
  frequency = ((int)(frequency * 10)) / 10.0;

You set a variable called frequency and then before you can use it you overwrite it with another value. Why?

// set the level of pin 8:
  analogWrite(delay, level);

despite what the comments say that line of code will set the pin who's number is given by the variable delay ( which you can't have anyway ) to a PWM duty cycle given by the variable level. Also you can only use analogWrite on certian pins depending on what Arduino you have. I don't think pin 8 is an option on any Arduino.

// wait for ? milliseconds to see the jump
  delay();

You need a number inside the brackets of delay to make it do anything. https://www.arduino.cc/en/reference/delay

What pot number value do you want to control the speed of the loop with?

Please read this:- How to use this forum Because your post is breaking the rules about posting code.

like i said i am fairly new to arduino, the code works fine for me except the delay. i need to figure out how to control delay with a pot

this is the part im struggling with

// read the input on analog pin 02: int delay = analogRead(A2); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float = delay * (5.0 / 1024.0); // print out the value you read: Serial.println(voltage);

// set the level of pin 8: analogWrite(delay, level);

// wait for ? milliseconds to see the jump delay();

@ grumpy mike no "float = delay * (5.0 / 1024.0);" does not compile

 // read the input on analog pin 02:
  int var_delay = analogRead(A2);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = var_delay * (5.0 / 1024.0);
  // print out the value you read:
  Serial.println(voltage);

  // set the level of pin 8:
  analogWrite(8, level); // this will not work on some Arduinos because that pin is not capable of PWM

  // wait for ? milliseconds to see the jump
  delay(var_delay);

thanks grumpy mike, thats been a lot of help.

i have removed this section as i can just use the 5v power rail to supply the pot

// set the level of pin 8: analogWrite(8, level);

when i put "float voltage = var_delay * (5.0 / 1024.0);" i get this error compiling

exit status 1 redeclaration of 'float voltage'

if i dont use that line the pot still adjusts the delay but it does not go below 24.

redeclaration of ‘float voltage’

Means you have more than one line that declares the variable voltage, of the type float.
Miss the “float” off that line.

if i dont use that line the pot still adjusts the delay but it does not go below 24.

You should just be putting the raw reading from the pot into delay, don’t mess with it because you don’t know what you are doing. Beginners often make the mistake of trying to convert analogue readings into a number, but 99% of the time this is unnecessary.

and again thankyou grumpy_mike, removed float and that fixed compiling error, it also appears to of made the delay drop lower as its now going down to 14

it also appears to of made the delay drop lower as its now going down to 14

Nothing to do with the delay. If the pot reading is not going the full range 0 to 1023 then you have wired up the pot wrong.