Ok the code:
/**************************************************************
* Calculation *
***************************************************************
50Hz period in µsec is 1000000/50 = 20000 µsec
60Hz period in µsec is 1000000/60 = 16666 µsec
120Hz period in µsec is 1000000/120 = 8333 µsec
150Hz period in µsec is 1000000/150 = 6666 µsec
300Hz period in µsec is 1000000/300 = 3333 µsec
50Hz on 20000/62 = 322 µsec
50Hz off 20000-322 = 19678 µsec
60Hz on 16666/62 = 268 µsec
60Hz off 16666-268 = 16398 µsec
120Hz on 8333/62 = 134 µsec
120Hz off 8333-134 = 8199 µsec
150Hz on 6666/62 = 107 µsec
150Hz off 6666-107 = 6559 µsec
300Hz on 3333/62 = 54 µsec
300Hz off 3333-54 = 3279 µsec
**************************************************************/
// On and Off Times (as int, max=32secs)
const unsigned int onTime50 = 322;
const unsigned int offTime50 = 19678;
const unsigned int onTime60 = 268;
const unsigned int offTime60 = 16398;
const unsigned int onTime120 = 134;
const unsigned int offTime120 = 8199;
const unsigned int onTime150 = 107;
const unsigned int offTime150 = 6559;
const unsigned int onTime300 = 54;
const unsigned int offTime300 = 3279;
/**************************************************************
* MISC SETUP *
**************************************************************/
int interval50 = onTime50; // Interval is how long we wait
int interval60 = onTime60; // Interval is how long we wait
int interval120 = onTime120; // Interval is how long we wait
int interval150 = onTime150; // Interval is how long we wait
int interval300 = onTime300; // Interval is how long we wait
unsigned long previousMicros = 0; // Tracks the last time event fired
boolean LEDstate = true; // Used to track if LED should be on or off
// Define unused pins
byte pin[] = {10, 11, 12, 13}; // Array of unused digital pins
byte pinCount = sizeof(pin) / sizeof(pin[0]); // Count unused digital pins
byte pinA[] = {A0, A1, A2, A3, A4, A5, A6, A7}; // Array of unused analog pins
byte pinACount = sizeof(pinA) / sizeof(pinA[0]); // Count unused analog pins
// Define button pins
byte pinU[] = {3, 4, 5, 6, 7}; // Array of used digital pins (buttons)
byte pinUCount = sizeof(pinU) / sizeof(pinU[0]); // Count used digital pins
// Usual Setup Stuff
void setup() {
for (byte i = 0; i < pinCount; i++) {
pinMode(pin[i], OUTPUT); // Set unused digital pins as output
digitalWrite(pin[i], LOW); // Set unused digital pins state to low
}
for (byte i = 0; i < pinACount; i++) {
pinMode(pinA[i], OUTPUT); // Set unused analog pins as output
digitalWrite(pinA[i], LOW); // Set unused analog pins state to low
}
for (byte i = 0; i < pinUCount; i++) {
pinMode(pinU[i], INPUT); // Set button pins as an input
digitalWrite(pinU[i], HIGH); // Enable internal pull-up resistor
}
pinMode(2, OUTPUT); // Set LED pin as output
}
void Hz50() {
// Set Pin 2 to state of LEDstate each time through the loop
// If LEDstate hasn't changed, neither will the pin
digitalWrite(2, LEDstate);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statemen
unsigned long currentMicros = micros();
if ((unsigned long)(currentMicros - previousMicros) >= interval50) {
if (LEDstate) { // Change wait interval, based on current LED state
interval50 = offTime50; // LED is currently on, set time to stay off
} else {
interval50 = onTime50; // LED is currently off, set time to stay on
}
LEDstate = !(LEDstate); // Toggle the LED's state
previousMicros = currentMicros; // Save the current time to compare "later"
}
}
void Hz60() {
// Set Pin 2 to state of LEDstate each time through the loop
// If LEDstate hasn't changed, neither will the pin
digitalWrite(2, LEDstate);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statemen
unsigned long currentMicros = micros();
if ((unsigned long)(currentMicros - previousMicros) >= interval60) {
if (LEDstate) { // Change wait interval, based on current LED state
interval60 = offTime60; // LED is currently on, set time to stay off
} else {
interval60 = onTime60; // LED is currently off, set time to stay on
}
LEDstate = !(LEDstate); // Toggle the LED's state
previousMicros = currentMicros; // Save the current time to compare "later"
}
}
void Hz120() {
// Set Pin 2 to state of LEDstate each time through the loop
// If LEDstate hasn't changed, neither will the pin
digitalWrite(2, LEDstate);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statemen
unsigned long currentMicros = micros();
if ((unsigned long)(currentMicros - previousMicros) >= interval120) {
if (LEDstate) { // Change wait interval, based on current LED state
interval120 = offTime120; // LED is currently on, set time to stay off
} else {
interval120 = onTime120; // LED is currently off, set time to stay on
}
LEDstate = !(LEDstate); // Toggle the LED's state
previousMicros = currentMicros; // Save the current time to compare "later"
}
}
void Hz150() {
// Set Pin 2 to state of LEDstate each time through the loop
// If LEDstate hasn't changed, neither will the pin
digitalWrite(2, LEDstate);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statemen
unsigned long currentMicros = micros();
if ((unsigned long)(currentMicros - previousMicros) >= interval150) {
if (LEDstate) { // Change wait interval, based on current LED state
interval150 = offTime150; // LED is currently on, set time to stay off
} else {
interval150 = onTime150; // LED is currently off, set time to stay on
}
LEDstate = !(LEDstate); // Toggle the LED's state
previousMicros = currentMicros; // Save the current time to compare "later"
}
}
void Hz300() {
// Set Pin 12 to state of LEDstate each time through the loop
// If LEDstate hasn't changed, neither will the pin
digitalWrite(2, LEDstate);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statemen
unsigned long currentMicros = micros();
if ((unsigned long)(currentMicros - previousMicros) >= interval300) {
if (LEDstate) { // Change wait interval, based on current LED state
interval300 = offTime300; // LED is currently on, set time to stay off
} else {
interval300 = onTime300; // LED is currently off, set time to stay on
}
LEDstate = !(LEDstate); // Toggle the LED's state
previousMicros = currentMicros; // Save the current time to compare "later"
}
}
void loop() {
int button50 = digitalRead(3); //read the button value
int button60 = digitalRead(4); //read the button value
int button120 = digitalRead(5); //read the button value
int button150 = digitalRead(6); //read the button value
int button300 = digitalRead(7); //read the button value
if(button50 == LOW) { // If button on pin 2 is low
Hz50(); // run 50Hz code
} else if (button60 == LOW) {
Hz60(); // run 60Hz code
} else if (button120 == LOW) {
Hz120(); // run 120Hz code
} else if (button150 == LOW) {
Hz150(); // run 150Hz code
} else if (button300 == LOW) {
Hz300(); // run 300Hz code
} else {
digitalWrite(2, LOW); // LED off
}
}
Somebody out there who can measure it to confirm its right?
For fun i want to switch the color and want to use this LED:
So i calculated:
red 47ohm = 62.77 mA
blue 22ohm = 63.64 mA
green 22ohm = 63.64 mA
I always have problems reading the hFE graphs so i guess 1k is ok for a BC337-40 base resistor?
BC337 fast enough?