Go Down

Topic: relay output speed control problem, HELP! (Read 194 times) previous topic - next topic

James121212

May 09, 2019, 09:18 pm Last Edit: May 09, 2019, 09:22 pm by James121212
doing a project.
Need the relays to turn on individually with a mapped delay time that is adjusted by a potentiometer signal pin. I have managed to get all the relays connected to the potentiometer but its changing the speed of all of them at the same time, as opposed to maintaining a delay time between each relay and them individually switching on. Any help would be greatful, i need this make this project work correctly and its very urgent, Thank you!!!
I need the relays to turn on one at a time, but change the overall speed.
Code: [Select]
int LED1 = 7;   //This assigns names to pins
int LED2 = 8;
int LED3 = 9;
int LED4 = 10;
int LED5 = 13;
int LED6 = 1;

int pot = A0; // assigns analog input A0 to variable pot
int val;

int lastChange; // the time of changing pin state
int state = HIGH;
void setup()
{


  pinMode(LED1, OUTPUT); //Set the pins to led as outputs
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT); //Set the pins to led as outputs
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(A0, INPUT);

  lastChange = millis();  // ledpin state changed NOW
  digitalWrite(LED1, state);
  digitalWrite(LED2, state);
  digitalWrite(LED3, state);
  digitalWrite(LED4, state);
  digitalWrite(LED5, state);
  digitalWrite(LED6, state);
}


void loop()

{

  val = map(analogRead(pot), 0, 1023, 500, 5);  //this reads the value every time
  if (lastChange + val <= millis())
  {
    state = !state; // invert led's state
    lastChange = millis();  //
    digitalWrite(LED1, state);
    delay(500);
    digitalWrite(LED2, state);
    delay(500);
    digitalWrite(LED3, state);
    delay(500);
    digitalWrite(LED4, state);
    delay(500);
    digitalWrite(LED5, state);
    delay(500);
    digitalWrite(LED6, state);
    delay(500);
  }

}

PerryBebbington

#1
May 09, 2019, 09:56 pm Last Edit: May 09, 2019, 09:58 pm by PerryBebbington
Hello James,

I've read your code 3 times and cannot see anything about relays...

Unfortunately I've also read your description of what you are trying to achieve and I can't make much sense of it either.

A circuit diagram would help, hand drawn and photographed is fine.

Ditch the delays, see 'blink without delay'

I also suspect you need a state machine, but as I don't really understand what you are doing I can't be sure.

James121212

The intLEDs are signals to the INpins on the relays, i have a 8 relay module but am only using 6 relays.

Sorry for not explaining

Okay ive attached a horrific circuit diagram.
At the bottom you see the ON, OFF. as i want to turn each pin individually on and off before the next pin is powered. but i need the speed of this one and off to be able be changed with potentiometer.




slipstick

It's close to impossible to mix timing using delay() and using millis(). You need to do one thing or the other. With all those delays in there you're only reading the pot once every few seconds, probably not what you intended.

Steve

PerryBebbington



Quote
I've attached a horrific circuit diagram.
Far better than no diagram!



larryd

#5
May 10, 2019, 02:23 am Last Edit: May 10, 2019, 02:31 am by larryd
  val = map(analogRead(pot), 0, 1023, 500, 5);

  if (lastChange + val <= millis())
. . .
  lastChange = millis();  <----<<< because of line placement and all the delays your if() will always evaluate
. . .
   delay(500);
. . .
   delay(500);
. . .




No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Go Up