/*
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.
The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 9 to ground
*/
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPinPWM = 0;
const int analogOutPin13 = 13;
const int analogOutPin12 = 12;
const int analogOutPin11 = 11;
const int analogOutPin10 = 10;
const int analogOutPin9 = 9;
const int analogOutPin8 = 8;
const int analogOutPin7 = 7;
const int analogOutPin6 = 6;
const int analogOutPin5 = 5;
const int analogOutPin4 = 4;
const int analogOutPin3 = 3;
const int analogOutPin2 = 2;
const int analogOutPin1 = 1;// Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // number of 7135 chips "on"
int pwmValue =0; // percent pwm of the 1 7135 that is pwmed, 0 to 255
int pwmRateConverter(int x, int y){
//method to determine pwm value for all cases besides 0; ---------> important ------->this relies on t map() truncating any value from the conversion, instead of roudning
int result;
result = x-map(outputValue, 0, 14, 0, 255);
return result;
}
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
//add method set all low
//method to determine pwm value for all cases besides 0; ---------> important ------->this relies on t map() truncating any value from the conversion, instead of roudning
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 14);
// change the analog out value:
switch (outputValue) {
case 0:
pwmValue = map(sensorValue, 0, 1023, 0, 255)
analogWrite(analogOutPinPWM, pwmValue);
//add method set all pins except pwmValue to low
break;
case 1:
digitalWrite(analogOutPin1, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 2:
digitalWrite(analogOutPin2, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 3:
digitalWrite(analogOutPin3, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 4:
digitalWrite(analogOutPin4, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 5:
digitalWrite(analogOutPin5, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 6:
digitalWrite(analogOutPin6, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 7:
digitalWrite(analogOutPin7, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 8:
digitalWrite(analogOutPin8, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 9:
digitalWrite(analogOutPin9, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 10:
digitalWrite(analogOutPin10, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 11:
digitalWrite(analogOutPin11, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 12:
digitalWrite(analogOutPin12, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 13:
digitalWrite(analogOutPin13, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.
The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 9 to ground
*/
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPinPWM = 0;
const int analogOutPin13 = 13;
const int analogOutPin12 = 12;
const int analogOutPin11 = 11;
const int analogOutPin10 = 10;
const int analogOutPin9 = 9;
const int analogOutPin8 = 8;
const int analogOutPin7 = 7;
const int analogOutPin6 = 6;
const int analogOutPin5 = 5;
const int analogOutPin4 = 4;
const int analogOutPin3 = 3;
const int analogOutPin2 = 2;
const int analogOutPin1 = 1;// Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // number of 7135 chips "on"
int pwmValue =0; // percent pwm of the 1 7135 that is pwmed, 0 to 255
int pwmRateConverter(int x, int y){
//method to determine pwm value for all cases besides 0; ---------> important ------->this relies on t map() truncating any value from the conversion, instead of roudning
int result;
result = x-map(outputValue, 0, 14, 0, 255);
return result;
}
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
//add method set all low
//method to determine pwm value for all cases besides 0; ---------> important ------->this relies on t map() truncating any value from the conversion, instead of roudning
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 14);
// change the analog out value:
switch (outputValue) {
case 0:
pwmValue = map(sensorValue, 0, 1023, 0, 255)
analogWrite(analogOutPinPWM, pwmValue);
//add method set all pins except pwmValue to low
break;
case 1:
digitalWrite(analogOutPin1, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 2:
digitalWrite(analogOutPin2, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 3:
digitalWrite(analogOutPin3, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 4:
digitalWrite(analogOutPin4, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 5:
digitalWrite(analogOutPin5, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 6:
digitalWrite(analogOutPin6, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 7:
digitalWrite(analogOutPin7, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 8:
digitalWrite(analogOutPin8, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 9:
digitalWrite(analogOutPin9, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 10:
digitalWrite(analogOutPin10, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 11:
digitalWrite(analogOutPin11, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 12:
digitalWrite(analogOutPin12, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
case 13:
digitalWrite(analogOutPin13, HIGH);
pwmValue =pwmRateConverter(pwmValue, outputValue);
analogWrite(analogOutPinPWM, pwmValue);
break;
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}