my first code and project 4 led sequential fader

just a pot controlled 4 sequential LED fader with a full warning (LED or buzzer)

im sure the code SUCKS! (its my first code i have only had the board for a day, it took me about an hour to get it working) so the many pointers to improve would be great :slight_smile:

 */

 // These constants won't change.  They're used to give names
 // to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin1 = 9; // Analog output pin that the LED1 is attached to
const int analogOutPin2 = 8; // Analog output pin that the LED2 is attached to
const int analogOutPin3 = 7; // Analog output pin that the LED3 is attached to
const int analogOutPin4 = 6; // Analog output pin that the LED4 is attached to
const int buzzerPin = 24; // Analog output pin that the peizo is attached to

int sensorValue1 = 0;        // value read from the pot
int sensorValue2 = 0;        // value read from the pot
int sensorValue3 = 0;        // value read from the pot
int sensorValue4 = 0;        // value read from the pot
int outputValue1 = 0;        // value output to the PWM (analog out)
int outputValue2 = 0;        // value output to the PWM (analog out)
int outputValue3 = 0;        // value output to the PWM (analog out)
int outputValue4 = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // make the buzzerpin an output:
  pinMode(buzzerPin, OUTPUT); 
   // read the analog in value:
  sensorValue1 = analogRead(analogInPin);       
  sensorValue2 = analogRead(analogInPin); 
  sensorValue3 = analogRead(analogInPin);
  sensorValue4 = analogRead(analogInPin);  
  // map it to the range of the analog out:
  outputValue1 = map(sensorValue1, 0, 255, 0, 255);  
  
  outputValue2 = map(sensorValue2, 255, 510, 0, 255); 
 
  outputValue3 = map(sensorValue3, 510, 765, 0, 255);  
 
  outputValue4 = map(sensorValue4, 765, 1020, 0, 255);   
 
  // change the analog out value:
  analogWrite(analogOutPin1, outputValue1); 
  // lock the led1 on
  if (outputValue1 > 255)
  {
  analogWrite(analogOutPin1, 255);
  }
  
  // keep led2 off untill value high enough
  if (sensorValue1 < 255)
  {
  analogWrite(analogOutPin2, 0);
  }  
  //led2 PWM
  else 
  {
  analogWrite(analogOutPin2, outputValue2);  
  }
  // lock led2 on when value high enough
 if (sensorValue1 > 510)
  {
  analogWrite(analogOutPin2, 255);
  }  
 
 //see above
  if (sensorValue1 < 510)
  {
  analogWrite(analogOutPin3, 0);
  }  
  else 
  {
  analogWrite(analogOutPin3, outputValue2);  
  }
 if (sensorValue1 > 765)
  {
  analogWrite(analogOutPin3, 255);
  }       
  //see above
  if (sensorValue1 < 765)
  {
  analogWrite(analogOutPin4, 0);
  }  
  else 
  {
  analogWrite(analogOutPin4, outputValue2);  
  }
 if (sensorValue1 > 1015)
  {
  analogWrite(analogOutPin4, 255);
  }               

 
// turns buzzer on when pot is turned up full
  if (sensorValue1 > 1015)
  {
  digitalWrite(buzzerPin, HIGH);  
  }
// turns buzzer off when pot is less than full  
  if (sensorValue1 < 1010)
  {
  digitalWrite(buzzerPin, LOW);   
  }
  
  // print the results to the serial monitor:
  Serial.print("sensor1 = " );                       
  Serial.print(sensorValue1);      
  Serial.print("\t output1 = ");      
  Serial.print(outputValue1);   
  Serial.print("\t output2 = ");      
  Serial.print(outputValue2);   
  Serial.print("\t output3 = ");      
  Serial.print(outputValue3);   
  Serial.print("\t output4 = ");      
  Serial.println(outputValue4);  
   

  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);                     
}

That is a good start! I'm not sure if I totally understand how your code works together in deciding which light turns on. Since you're using map(), why are you still using "if"?

im sure there is lots of mess. but im maping the same input at diferent input ranges to output the same range on a different pin:

outputValue1 = map(sensorValue1, 0, 255, 0, 255);  //first 1/4 turn
  
  outputValue2 = map(sensorValue2, 255, 510, 0, 255); //second 1/4 turn

  outputValue3 = map(sensorValue3, 510, 765, 0, 255);  //3rd 1/4 turn

  outputValue4 = map(sensorValue4, 765, 1020, 0, 255);  //last 1/4 turn

then as this didnt quite work as i expected lol (all the lights were fading on and of 4 time during one turn)

i used ‚Äúif‚ÄĚ and ‚Äúelse‚ÄĚ to lock the led on or of untill the sensot input was in its range

  // lock the led1 on
  if (outputValue1 > 255)
  {
  analogWrite(analogOutPin1, 255);
  }
  
  // keep led2 off untill value high enough
  if (sensorValue1 < 255)
  {
  analogWrite(analogOutPin2, 0);
  }  
  //led2 PWM
  else
  {
  analogWrite(analogOutPin2, outputValue2);  
  }