POT reading converted to Percentage
//Simple Serial Printing Test with a Potentiometer
const int POT=0; //Pot on analog pin 0
void setup() {
Serial.begin(9600); //Start serial port with baud = 9600
} void loop() {
int val = analogRead(POT); //Read potentiometer
int per = map(val, 0, 1023, 0, 100); //Convert to percentage
Serial.print("Analog Reading: ");
Serial.print(val); //Print raw analog value
Serial.print(" Percentage: ");
Serial.print(per); //Print percentage analog value Serial.println("%"); //Print % sign and newline
delay(1000); //Wait 1 second, then repeat
}
Serial Printing a Potentiometer Data Table
//Tabular serial printing test with a potentiometer
const int POT=0; //Pot on analog pin 0
void setup()
{
Serial.begin(9600); //Start Serial Port with Baud = 9600
}
void loop() {
Serial.println("\nAnalog Pin\tRaw Value\tPercentage");
Serial.println("------------------------------------------");
for (int i = 0; i < 10; i++) {
int val = analogRead(POT); //Read potentiometer
int per = map(val, 0, 1023, 0, 100); //Convert to percentage Serial.print("A0\t\t");
Serial.print(val);
Serial.print("\t\t");
Serial.print(per); //Print percentage analog value Serial.println("%"); //Print % sign and newline
delay(1000); //Wait 1 second, then repeat
}
}
Echo Serial Inputs to the Serial Output Space
//Echo every character char data; //Holds incoming character
void setup() {
Serial.begin(9600); //Serial Port at 9600 baud
}
void loop() {
//Only print when data is received
if (Serial.available() > 0) {
data = Serial.read(); //Read byte of data
Serial.print(data); //Print byte of data
}
}
HOMEWORK:
Serial Input RGB values to light up RGB LED
//Sending Multiple Variables at Once
//Define LED pins
const int RED =11;
const int GREEN =10;
const int BLUE =9;
//Variables for RGB levels
int rval = 0;
int gval = 0;
int bval = 0;
void setup()
{
Serial.begin(9600); //Serial Port at 9600 baud
//Set pins as outputs
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop()
{
//Keep working as long as data is in the buffer
while (Serial.available() > 0)
{
rval = Serial.parseInt(); //First valid integer
gval = Serial.parseInt(); //Second valid integer
bval = Serial.parseInt(); //Third valid integer
if (Serial.read() == '\n') //Done transmitting
{
//set LED
analogWrite(RED, rval);
analogWrite(GREEN, gval);
analogWrite(BLUE, bval);
}
}
}
No comments:
Post a Comment