Är ganska ny på det här med arduino så behöver lite hjälp.
Håller på att göra en RGB ljusslinga som jag vill ställa in blinkhastigheten med en potentiometer och färgen med en annan potentiometer.
har fått var för sig att fungera men när jag kombinerar så försvinner färgvalet och den blinkar alla samtidigt.
Någon som har ett bra förslag hur jag ska lösa det? ![]()
bifogar koden:
int redPin = 9;
int greenPin = 11;
int bluePin = 10;
int potPin = A0; //color change pot
int val = 0;
int sensorPin = A1; // Blink speed pot
int sensorValue = 0; // variable to store the value coming from the sensor
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
//Read the potentiometer (color wheel).
val = analogRead(potPin);
Serial.println(val);
//Red-orange-yellow-green spectrum
if ( val <= 341 )
{
//We now know the val is between 0 and 341.
//Map the val to 0-255 for the LED.
val = map(val, 0, 341, 0, 255);
//Higher val turns red down.
//Lower val turns red up (common anode).
analogWrite(redPin, val);
//Write the complimentary amount to the green pin.
//The mixed color values add up to 255.
//This keeps the LED at constant brightness.
analogWrite(greenPin, 255-val);
//Turn blue pin off.
analogWrite(bluePin, 255);
}
//Green-teal-blue spectrum
else if ( val <= 683 )
{
//We now know the val is between 341 and 683.
//Map the val to 0-255 for the LED.
val = map(val, 341, 683, 0, 255);
//Higher val turns green down.
//Lower val turns green up (common anode).
analogWrite(greenPin, val);
//Write the complimentary amount to the blue pin.
//The mixed color values add up to 255.
//This keeps the LED at constant brightness.
analogWrite(bluePin, 255-val);
//Turn red pin off.
analogWrite(redPin, 255);
}
//Blue-indigo-purple-red spectrum.
else
{
//We now know the val is between 683 and 1023.
//Map the val to 0-255 for the LED.
val = map(val, 683, 1023, 0, 255);
//Higher val turns blue down.
//Lower val turns blue up (common anode).
analogWrite(bluePin, val);
//Write the complimentary amount to the red pin.
//The mixed color values add up to 255.
//This keeps the LED at constant brightness.
analogWrite(redPin, 255-val);
//Turn green pin off.
analogWrite(greenPin, 255);
}
//Sampling rate.
delay(20);
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
}
Mvh Dan