Push buttons instead of potentiometar to incriese and decriese time delay

Hi guys,
I wrote this basic code to control electromagnet movement left and right.
To be sure that on side is switched of i added time delay.
Time delay is variable and controlled with potentiometer.
Problem with potentiometer is that it breaks easily when used a lot so i would like to change it with 2 push buttons.
Frequency of change must not be more than 30 times per second.
I would really appreciate some suggestions how to do that.

Thanks.

int coil1 = 13;
int coil2 = 12;
int potPin = A1;
int val = 0;



void setup() {

  pinMode(coil1, OUTPUT);
  pinMode(coil2, OUTPUT);
  pinMode(potPin, INPUT);
   
}


void loop() 
{

  val = analogRead(potPin);
  val = map(potPin, 10, 1000, 10, 10000);
  int val  = analogRead(potPin);

{
  digitalWrite(coil1, HIGH);
  delay(val);
  digitalWrite(coil1, LOW);
  delay(val);
  digitalWrite(coil2, HIGH);
  delay(val);
  digitalWrite(coil2, LOW);
  delay(val);





}
}

arduino_tresilica.ino (471 Bytes)

The potentiometer has the advantage that it determines the value before the Arduino starts. With push buttons you will either have to set the value every time you start the Arduino or store the value in the EEPROM memory.

Why not just get a good quality potentiometer?

if you really do want to use buttons then I suggest you arrange things so that one button increments a variable by (say) 5 for every press and the other decrements it. if you keep the values between 0 and 1023 the value in the variable will be a direct equivalent of the value from the potentiometer.

...R

I agree with you about advantages, this is meant to be something in the field (tree shaker) and life expectancy would be really really poor.
Btw, value must be very high each time arduino is powered. (say 1 Hz)

I am afraid that my programming knowledge is just to poor to make code you suggested, can you pls point to me some examples?

websonic:
I am afraid that my programming knowledge is just to poor to make code you suggested, can you pls point to me some examples?

Most of what you need should be in the examples that come with the Arduino IDE.

...R

I just can not wrap my head around it.
What to declare, how ro declare....
2 weeks already I am searching on the net
Some pointers would be really appreciated....

Which of the Arduino IDE examples are you having trouble with?

You can't have done much studying in the 5 minutes between Replies #3 and #4

...R

Dude couple of weeks I am trying to solve this.

I suggest you start with ALL of the examples in the 02. Digital group in your Arduino IDE, except for the tone___ ones. You will need a button (or two pieces of wire to touch together) for most of those examples.

StateChangeDetection is going to be the most important lesson to learn. Followed by Debounce and then the advanced stuff is BlinkWithoutDelay. If you're really starting from basics, start with Button and DigitalInputPullup.

wooooow,

That was helpful,
Thanks a lot mate :smiley:

I am getting this impression that most of the people "helping" on this foruma are jerks whorather enjoy their own farts.
That explains why so many people started avoiding this website.

So thanks for nothing and [deleted] you very much

Right back at ya, champ - enjoy the ban.