const int ledCount = 10; // the number of LEDs in the bar graph
int ledPins[] = {
2,4,7,7,8,8,12,12,13,
}; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
// Configure Timer 1 for PWM @ 25 kHz.
TCCR1A = 0; // undo the configuration done by...
TCCR1B = 0; // ...the Arduino core library
TCNT1 = 0; // reset timer
TCCR1A = _BV(COM1A1) // non-inverted PWM on ch. A
| _BV(COM1B1) // same on ch; B
| _BV(WGM11); // mode 10: ph. correct PWM, TOP = ICR1
TCCR1B = _BV(WGM13) // ditto
| _BV(CS10); // prescaler = 1
ICR1 = 320; // TOP = 320
// Configure Timer 0 for phase correct PWM @ 25 kHz.
TCCR0A = 0; // undo the configuration done by...
TCCR0B = 0; // ...the Arduino core library
TCNT0 = 0; // reset timer
TCCR0A = _BV(COM0B1) // non-inverted PWM on ch. B
| _BV(WGM00); // mode 5: ph. correct PWM, TOP = OCR0A
TCCR0B = _BV(WGM02) // ditto
| _BV(CS00); // prescaler = 1
OCR0A = 1000;
// OCR0B = 160; // TOP =
// Same for Timer 1.
// Same for Timer 2.
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
TCCR2B = TCCR2B & 0b11111000 | 0x01;
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(CS20);
OCR2A = 180;
OCR2B = 180;
// Set the PWM pins as output.
pinMode( 9, OUTPUT);
pinMode(10, OUTPUT);
pinMode( 3, OUTPUT);
pinMode( 5, OUTPUT);
pinMode( 6, OUTPUT);
pinMode(11, OUTPUT);
pinMode( 2, OUTPUT);
pinMode( 4, OUTPUT);
pinMode( 7, OUTPUT);
pinMode( 8, OUTPUT);
pinMode( 12, OUTPUT);
pinMode( 13, OUTPUT);
}
}
void loop() {
analogWrite( 9, map (analogRead (0),0,1000,0,140)); //100); // duty cycle = 1/16010);
analogWrite(10, map (analogRead (1),0,1000,0,140)); //100); // duty cycle = 1/160210);
analogWrite( 3, map (analogRead(5),0,1000,0,120)); //100); // duty cycle = 1/16010);
analogWrite( 5, map (analogRead (4),0,1000,0,150)); //100); // duty cycle = 1/160210);
analogWrite(11, map (analogRead (2),0,1000,0,120)); //100); // duty cycle = 1/160210);
//analog comparator to digital
digitalWrite( 2, map (analogRead (3),0,10000,0,180)); //100); // duty cycle = 1/16010);
digitalWrite( 4, map (analogRead (3),0,10000,0,100)); //100); // duty cycle = 1/16010);
digitalWrite( 6, map (analogRead (3),0,10000,0,80)); //100); // duty cycle = 1/16010);
digitalWrite( 7, map (analogRead (3),0,10000,0,60)); //100); // duty cycle = 1/16010);
digitalWrite( 8, map (analogRead (3),0,10000,0,40)); //100); // duty cycle = 1/16010);
digitalWrite( 12, map (analogRead (3),0,10000,0,20)); //100); // duty cycle = 1/16010);
digitalWrite( 13, map (analogRead (3),0,10000,0,10)); //100); // duty cycle = 1/16010);
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น