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?
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.
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.
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.
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
}