I'm trying to make a pseudo granular on/off tremolo effect for some audio signal.
At the moment I'm using a NPN transistor as a switch, and an LED to indicate status.
I should mention that I'm very new to Arduino-ing, so this is just using some example code and trying to mess about with things.
I managed to get a pot control the rate of on/off and managed to scale the values to useful range, but I'm stuck on how to implement the other features I want.
I'd like to be able to control the PWM(duty cycle?) of the LED. So if the analogread is 1023 the LED is always on. And at 0, it would flash a tiny tiny blip at the rate set by the.... rate.
Not sure if PWM is the right term in this application.
What kind of changes would I have to make to the code below to implement that? Do I define a new variable, then make the on/off times dependent on it?
Eventually I want to implement a random control, where it doesn't turn on/off regularly, but rather, based on the value read from a pot, it either:
max clockwise : LED always on
middle : on/off roughly 50% of the time
counterclock : LED always off.
But based on random on/off. So a 'weight' type value. The higher the 'weight' the more the LED will be on (though still random), and the lower the weight, the less the LED will be on.
I know these are a lot of questions, and very specific/particular ones. I don't want someone to just give me code that will work, as I enjoy figuring things out. But I'm stuck as to how such things would be done at all in code.
int sensorPin = 2;
int ledPin = 0;
int sensorValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, 0, 1023, 20, 500);
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
}