need help here -- simple flip flop with potentiometer to adjust the timer

Hello,

I am very newbie here,

I am trying to make an adjustable dual timer with a flip flop sketch

my need is : able to adjust the ON time (only ON time) for both OUTPUT , ex. from 1 minute to 10 minute

any step by step to modify the sketch for hookup the potentiometer PIN ?

any help will be appreciate

my current sketch flip flop is bellow :

int forward = 5;
int reverse = 4;

// the setup function runs once when you press reset or power the board
void setup()
{
pinMode(forward, OUTPUT);
pinMode(reverse, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(forward, HIGH);
delay(120000);
digitalWrite(forward, LOW);
delay(5000);
digitalWrite(reverse, HIGH);
delay(120000);
digitalWrite(reverse, LOW);
delay(5000);
}

Hi,

First off, a little more context is needed. When you refer to flip-flop I assume youre not referring to a logic flip flop (i.e JK / SR)

Secondly please put your code in the code brackets as its much easier to read

This is my best attempt at something to help you in the right direction. But really a little more context is needed i.e. what is this for, why the two different times.

A schematic always helps! :slight_smile:

Andy

#define forward 5;
#define reverse 4;
#define pot_pin 0;

void setup(void);
void loop(void);
void read_pot(void);

int time_1 = 120000;
int time_2 = 5000;

// the setup function runs once when you press reset or power the board
void setup()
{
  pinMode(forward, OUTPUT);
  pinMode(reverse, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  read_pot();
  digitalWrite(forward, HIGH);      
  delay(time_1);                       
  digitalWrite(forward, LOW);    
  delay(time_2);   
  digitalWrite(reverse, HIGH);      
  delay(time_1);                       
  digitalWrite(reverse, LOW);    
  delay(time_2); 
}

void read_pot()
{

float reading = (float) analogRead(pot_pin); //number between 0 and 1023;

reading/=1023;
reading*=120000; //scale up to 0 - 120000;

time_1 = (int)reading;
return;


}

Seems like a good place to use the map() function:

const unsigned long OneMinute = (1UL*60UL)*1000UL;  
const unsigned long TenMinutes = (10UL*60UL)*1000UL;
const byte PotPin = A0;


    // Wait for 1 to 10 minutes depending on the position of the potentiometer
    delay(map(analogRead(PotPin), 0, 1023, OneMinute, TenMinutes));

andyowenwest:
Hi,

First off, a little more context is needed. When you refer to flip-flop I assume youre not referring to a logic flip flop (i.e JK / SR)

Secondly please put your code in the code brackets as its much easier to read

This is my best attempt at something to help you in the right direction. But really a little more context is needed i.e. what is this for, why the two different times.

A schematic always helps! :slight_smile:

Andy

#define forward 5;

#define reverse 4;
#define pot_pin 0;

void setup(void);
void loop(void);
void read_pot(void);

int time_1 = 120000;
int time_2 = 5000;

// the setup function runs once when you press reset or power the board
void setup()
{
 pinMode(forward, OUTPUT);
 pinMode(reverse, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
 read_pot();
 digitalWrite(forward, HIGH);      
 delay(time_1);                      
 digitalWrite(forward, LOW);    
 delay(time_2);  
 digitalWrite(reverse, HIGH);      
 delay(time_1);                      
 digitalWrite(reverse, LOW);    
 delay(time_2);
}

void read_pot()
{

float reading = (float) analogRead(pot_pin); //number between 0 and 1023;

reading/=1023;
reading*=120000; //scale up to 0 - 120000;

time_1 = (int)reading;
return;

}

hi thanks for the replay, and sorry about the code not in the correct place :).

the purpose is to control a single phase AC motor rotation (fw and rev) with a delay (5sec) each fw/rev action to protect the motor, motor need a break delay for a mechanical thing (release the contact of the starting capacitor).

each pin (fw and rev) will be control a relay, and each relay will be correspondence to a motor direction and it's work alternately . for a moment I have only one FIX timing which is 1 minute. I need to be able to make it more flexible with an pot to adjust the timer to suit with my need.

I hope my above explanation is represent the situation.

also try to compile above code and get as bellow

exit status 1
expected ')' before ';' token

any advice, really new to these and only have a few soldering skill :frowning:

The semicolons on the end of the #define lines should not be there

UKHeliBob:
The semicolons on the end of the #define lines should not be there

working now :slight_smile: and that's what iam looking for

Thank you for all help

all credit goes to andyowenwest

and these the copy of the code using standard arduino board

/*  use with regular Arduino board
 *  
*/

#define forward 4;    // digital pin 4 on Arduino Board (relay forward)
#define reverse 5;    // digital pin 5 on Arduino Board (relay reverse)
#define pot_pin 0;    // analog pin 0 (A0) on Arduino Board (potentiometer 1-100K Ohm)

void setup(void);
void loop(void);
void read_pot(void);

int time_1 = 600000;    // 10 minute = 600000 milisecond
int time_2 = 5000;      // 5 second = 5000 milisecond

// the setup function runs once when you press reset or power the board
void setup()
{
  pinMode(forward, OUTPUT);
  pinMode(reverse, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  read_pot();
  digitalWrite(forward, HIGH);      
  delay(time_1);                       
  digitalWrite(forward, LOW);    
  delay(time_2);   
  digitalWrite(reverse, HIGH);      
  delay(time_1);                       
  digitalWrite(reverse, LOW);    
  delay(time_2); 
}

void read_pot()
{

float reading = (float) analogRead(pot_pin); 

reading/=1023;        //number between 0 and 1023;
reading*=600000;      //scale up to 0 - 600000;

time_1 = (int)reading;
return;


}

and these one using ultra small digispark board (* not tested yet, need to get one of these first)

/*  use with attiny85 digispark board
 *  basic use : http://digistump.com/wiki/digispark/tutorials/basics 
*/

#define forward 0        //0 is P0, 1 is P1, 2 is P2, etc. 
#define reverse 1        //0 is P0, 1 is P1, 2 is P2, etc. 
#define pot_pin 2        //THIS IS P2, P2 is analog input 1, so when you are using analog read, you refer to it as 1.

void setup(void);
void loop(void);
void read_pot(void);

int time_1 = 600000;
int time_2 = 5000;

// the setup function runs once when you press reset or power the board
void setup()
{
  pinMode(forward, OUTPUT); // relay for forward on P0 attiny85 digispark board
  pinMode(reverse, OUTPUT); // relay for reverse on P1 attiny85 digispark board
  pinMode (pot_pin, INPUT); // POTENTIO on P2 attiny85 digispark board
}

// the loop function runs over and over again forever
void loop()
{
  read_pot();
  digitalWrite(forward, HIGH);      
  delay(time_1);                       
  digitalWrite(forward, LOW);    
  delay(time_2);   
  digitalWrite(reverse, HIGH);      
  delay(time_1);                       
  digitalWrite(reverse, LOW);    
  delay(time_2); 
}

void read_pot()
{
 
float reading = (float) analogRead(1);      // 1 means P2 on digispark board
reading/=1023;                                     //number between 0 and 1023;
reading*=600000;                                //scale up to 0 - 600000;
  
time_1 = (int)reading;
return;

}