Код:
float RT = 10000;// the rated potentiometer value 10k
float R;// the unknown resistance value R
int potPin = A0; // potentiometer is connected to analog 0 pin
int potValue; // variable used to store the value coming from the sensor
int percent; // variable used to store the percentage value
void setup() {
Serial.begin(9600); // initialize the serial communication
}
void loop() {
potValue = analogRead(potPin); // get a reading from the potentiometer, assign the name potValue
percent = map(potValue, 0, 1023, 0, 100); // convert reading to a percentage
float voltage = potValue * (5.0 / 1023.0); //convert to voltage
R = ( voltage * RT)/5.0; // get value of unknown resistance
Serial.print("Analog Reading: "); // print out the analog value from A0
Serial.print(potValue);
Serial.print(", Voltage =");// prints the text "Voltage ="
Serial.print(voltage);
Serial.print("v, Resistance="); //prints the text "Resistance ="
Serial.print(R);
Serial.print(" ohms");
Serial.print(", Percentage: "); // print out the percentage
Serial.print(percent);
Serial.println("%");
delay(1000); // wait a second
}
int potPin = A0; // potentiometer is connected to analog 0 pin
int led1 = 8; // red LED connected to digital PIN 13
int led2 = 9; // red LED connected to digital PIN 12
int led3 = 10;
int led4 = 11;
int potValue; // variable used to store the value coming from the sensor
int percent; // variable used to store the percentage value
void setup() {
pinMode(led1, OUTPUT); // LED is declared as an output
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop() {
potValue = analogRead(potPin); // read the value from the potentiometer and assign the name potValue
percent = map(potValue, 0, 1023, 0, 100); // convert potentiometer reading to a percentage
if (percent <= 5) { //if the percentage is less than 5%...
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
else if(percent > 5 && percent <= 25){
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
else if(percent > 25 && percent <= 50){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
else if(percent > 50 && percent <=75){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
}else {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
}
}
No comments:
Post a Comment