Blink sketch random // solenoid

Hi I would like to have a random motor controlled sketch like the one below I've made but for a solenoid. So HIGH or LOW with different speeds.

int randNumber; // variable to store the random value
int motor = 0;



void setup() {
  
    TCCR0B |= CS01;
    TCCR0B &=  ~(CS02 | CS00);
} 
void loop()
{
randomSeed(millis()); // sets millis() as seed
randNumber = random(255); // random number from 0-255
analogWrite(motor, randNumber); // outputs PWM signal
delay(120000); 
}

with this

int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);               // wait for a second
}

So I think I have to randomize the delay but how do I do that? Create a new class?

randomSeed(millis()); // sets millis() as seed

You call randomSeed() ONCE, not on every pass through loop().

Calling it with millis() in setup() will be useless.

Where do you test that the solenoid is at its limit? All you are doing it randomly selecting a speed, every 2 minutes.

What is not working for you?

well indeed the millis doesn't work, because it's just on of.

So I just figured out that I'm only able to randomize the delay.

Can I do

int random

void loop{

random = random(delay) (50-1200);

}

Im new at whole these things so excuse me If i'm asking stupid questions.

beyondal: Im new at whole these things

We all are / were..... but my question has to be, have you worked through the examples in the IDE (File > examples > ...) or also available here. Do you read the Reference for coding help?

well indeed the millis doesn't work, because it's just on of.

Capital letters, punctuation, and proper spelling might make this string of letters less meaningless.

Can I do

int random

void loop{

random = random(delay) (50-1200);

}

Of course not, as the compiler would most certainly tell you. There are several problems here. First, statements end with semicolons. Second, you can't have variables and functions with the same name. Third, the variable does NOT need to be global. Fourth, what that lest statement is trying to do is not at all clear.

Try this:

// Get a value between 50 and 255
int speed = random(50, 256);

analogWrite(somePin, speed);

Pin 0 is NOT a PWM pin on ANY Arduino, so you first need to connect your solenoid to somePin that IS a PWM pin.

I did this but didn't work.

int snelheid= random(500,1000); //speed
int sol=13;  //pwm output

void setup(){
}

void loop(){
digitalWrite(sol, snelheid);
delay(1000);
}

I did this but didn't work.

It most certainly did.

That it did not achieve the results you expect is solely a problem with your expectations. If you need help aligning your expectations to reality, you need to define which Arduino you have, what your expectations are, and what the code actually does.

No more "it didn't work" shit is allowed.

beyondal: I did this but didn't work.

int snelheid= random(500,1000); //speed
int sol=13;  //pwm output

void setup(){ }

void loop(){ digitalWrite(sol, snelheid); delay(1000); }

What values does digitalWrite() normally take as its second parameter ? How many different states can a digital pin be in ? What is the default state of a digital pin when it has not been set by pinMode() ?

All of these questions are relevant to your code not working, at least in the way that you expect.

Firstly, to use PWM you need an *analog*Write not *digital*Write, and second those values need to be from 0 to 255, not 500 to 1000.

Thirdly, is your Arduino one that supports PWM on pin 13?

Well I did the sketch with the output 13 (led)

So my expectations are that it goes on and off at a different (random) speed for a (random) time.

So I thought if I give the variable of the speed 500-1000 it randomly goes between those two values. And if I then give it an delay of 1000 it would change the led to go randomly over a 1000milliseconds.

I use an Uno, and the values of 0-255 is for the pwm output? I want to change the speed but in the delay, that's what makes the solenoid blink? Or am I wrong?

So I think now I have to declare two variables that one ex.

int speed = random(100-1000) // speed that changes the solenoid's on/off
int spdelay = random(1000-8000) // random delay between given values

void setup(){
}

void loop(){

analogWrite(13, speed) // and the output of HIGH??
 delay = spdelay

}

You're right about the answer!

I'm trying like hell to figure it out.

cheers!

Pin 13's only PWM on a Mega.

that's what makes the solenoid blink? Or am I wrong?

You are. Solenoids don't blink.

PaulS:

that's what makes the solenoid blink? Or am I wrong?

You are. Solenoids don't blink.

Must say, PaulS, I've been wondering wth it means to "blink" a solenoid. Perhaps extend / retract?

Also what it means to PWM a solenoid is beyond me.... perhaps at low PWM average voltage, extend sloooooowly?