- Joined
- Nov 25, 2010
- Messages
- 1,211
- Motherboard
- AsRock X570M Pro4
- CPU
- Ryzen 3700x
- Graphics
- RX 580
Excellent Kiwi, this is ramping up into something great!
Cheers!
Cheers!
Welcome to the A club, would be good to get some feedback once you have finished.Kiwi,
Thanks for your hints. I've ordered an Arduino Leonardo and will do some tests. ...
MacTester
void newMethod() {
// setup a timer to press the button
timer.setVariableTimer(powerButtonCallback);
}
long powerButtonCallback(int callNumber) {
if (callNumber ==0) {
// delay 1000ms
return 1000;
} else if (callNumber ==1) {
// output high value for 200ms
digitalWrite(NUC_POWER_SWITCH_POUT,HIGH);
return 200;
} else if (callNumber ==2) {
// reset output to low, and finish the timer
digitalWrite(NUC_POWER_SWITCH_POUT,LOW);
return 0;
}
}
void oldWayOfDoingIt() {
delay(1000);
digitalWrite(NUC_POWER_SWITCH_POUT,HIGH);
delay(200);
digitalWrite(NUC_POWER_SWITCH_POUT,LOW);
}
// Include the following Libraries-------------------------------------
#include <SimpleTimer.h> //SimpleTimer
// A general purpose timer, and some misc constants
SimpleTimer timer = SimpleTimer();
// Pin Assignments (constants)-----------------------------------------
const int LED_1 = 3;
const int LED_2 = 5;
const int LED_3 = 6;
// Define Variables----------------------------------------------------
//float currentPWM = 150; // Roughly equal to 3.3v
//float targetVOLT = 3.3; // current target voltage to match
void setup ( )
{
//This Commands are only executed during startup or reset--------------
// Declare Output Pins
pinMode(LED_1,OUTPUT);
pinMode(LED_2,OUTPUT);
pinMode(LED_3,OUTPUT);
}
// This is the Main Loop-----------------------------------------------
void loop ( ) {
timer.setVariableTimer(LED_1_Timer);
timer.setVariableTimer(LED_2_Timer);
timer.setVariableTimer(LED_3_Timer);
timer.run();
}
//LED 1 Timer----
long LED_1_Timer(int callNumber) {
if (callNumber ==0) {
// delay 1000ms
return 1000;
} else if (callNumber ==1) {
// output high value for 200ms
digitalWrite(LED_1,HIGH);
return 200;
} else if (callNumber ==2) {
// reset output to low, and finish the timer
digitalWrite(LED_1,LOW);
return 0;
}
}
//LED 2 Timer----
long LED_2_Timer(int callNumber) {
if (callNumber ==0) {
// delay 1000ms
return 1000;
} else if (callNumber ==1) {
// output high value for 100ms
digitalWrite(LED_2,HIGH);
return 100;
} else if (callNumber ==2) {
// reset output to low, and finish the timer
digitalWrite(LED_2,LOW);
return 0;
}
}
//LED 3 Timer----
long LED_3_Timer(int callNumber) {
if (callNumber ==0) {
// delay 1000ms
return 1000;
} else if (callNumber ==1) {
// output high value for 50ms
digitalWrite(LED_3,HIGH);
return 50;
} else if (callNumber ==2) {
// reset output to low, and finish the timer
digitalWrite(LED_3,LOW);
return 0;
}
}
// Include the following Libraries-------------------------------------
#include <SimpleTimer.h> //SimpleTimer
// A general purpose timer, and some misc constants
SimpleTimer timer_1 = SimpleTimer();
SimpleTimer timer_2 = SimpleTimer();
SimpleTimer timer_3 = SimpleTimer();
// Pin Assignments (constants)-----------------------------------------
const int LED_1 = 3;
const int LED_2 = 5;
const int LED_3 = 6;
// Define Variables----------------------------------------------------
//float currentPWM = 150; // Roughly equal to 3.3v
//float targetVOLT = 3.3; // current target voltage to match
//This Commands are only executed during startup or reset--------------
void setup ( ) {
// Declare Output Pins
pinMode(LED_1,OUTPUT);
pinMode(LED_2,OUTPUT);
pinMode(LED_3,OUTPUT);
}
// This is the Main Loop-----------------------------------------------
void loop ( ) {
timer_1.setVariableTimer(LED_1_Timer);
timer_2.setVariableTimer(LED_2_Timer);
timer_3.setVariableTimer(LED_3_Timer);
timer_1.run();
timer_2.run();
timer_3.run();
}
//LED 1 Timer----
long LED_1_Timer(int callNumber) {
if (callNumber ==0) {
// delay 900ms
return 900;
} else if (callNumber ==1) {
// output high value for 100ms
digitalWrite(LED_1,HIGH);
return 100;
} else if (callNumber ==2) {
// reset output to low, and finish the timer
digitalWrite(LED_1,LOW);
return 0;
}
}
//LED 2 Timer----
long LED_2_Timer(int callNumber) {
if (callNumber ==0) {
// delay 1900ms
return 1900;
} else if (callNumber ==1) {
// output high value for 100ms
digitalWrite(LED_2,HIGH);
return 100;
} else if (callNumber ==2) {
// reset output to low, and finish the timer
digitalWrite(LED_2,LOW);
return 0;
}
}
//LED 3 Timer----
long LED_3_Timer(int callNumber) {
if (callNumber ==0) {
// delay 3900ms
return 3900;
} else if (callNumber ==1) {
// output high value for 100ms
digitalWrite(LED_3,HIGH);
return 100;
} else if (callNumber ==2) {
// reset output to low, and finish the timer
digitalWrite(LED_3,LOW);
return 0;
}
}
Looking at you code the main issue is that you call setVariableTimer() every run through the main loop() function. This will create multiple "threads" invoking the same callback function, probably not what you want. Secondly SimpleTimer can handle 10 (can be increased) "threads", so you only need one of these to run the three timers in the example.Kiwi,
Thanks again for your help. I've tried your modified SimpleTimer library. It works as long as the time values are the same for all 3 LED. But it's freaking out, if every LED has different time values. Any thoughts?
EDIT: the following code works now.
// Include the following Libraries-------------------------------------
#include <SimpleTimer.h> //SimpleTimer
// A general purpose timer, and some misc constants
SimpleTimer timer = SimpleTimer();
// Pin Assignments (constants)-----------------------------------------
const int LED_1 = 3;
const int LED_2 = 5;
const int LED_3 = 6;
//This Commands are only executed during startup or reset--------------
void setup ( ) {
// Declare Output Pins
pinMode(LED_1,OUTPUT);
pinMode(LED_2,OUTPUT);
pinMode(LED_3,OUTPUT);
// Setup the BACKGROUND Timers (only needs to be done once)
timer.setVariableTimer(LED_1_Timer);
timer.setVariableTimer(LED_2_Timer);
}
// This is the Main Loop-----------------------------------------------
void loop ( ) {
if ( buttonWasPressed() ) {
// when a button is pressed flash LED 3
timer.setVariableTimer(LED_3_Timer);
}
timer.run();
}
//LED 1 Timer --- Shows How we can stop a timer at an arbitrary point in time
long LED_1_Timer(int callNumber) {
// do something useful every 1000ms
// unless some external condition is met then stop the timer.
if (somethingIsTrue()) return 0;
return 1000;
}
//LED 2 Timer --- Shows how the frequency of the timer can can change.
long LED_2_Timer(int callNumber) {
// do something useful every 100 to 300 milliseconds
// The period is variable and based on a sin function that
// cycles every 62.830 (pi*2*10) repetitions of the timer
return sin(callNumber/10)*100+200
}
//LED 3 Timer --- An example of a one shot, where we pulse an output pin ONCE
long LED_3_Timer(int callNumber) {
if (callNumber ==0) {
// delay 3900ms
return 3900;
} else if (callNumber ==1) {
// output high value for 100ms
digitalWrite(LED_3,HIGH);
return 100;
} else if (callNumber ==2) {
// reset output to low, and finish the timer
digitalWrite(LED_3,LOW);
return 0;
}
}
I have not had any reason (yet) to change PWM frequency. There is quite a bit on the topic though, including some simple functions, and complete libraries. The Leonardo is a different chip to the main Arduino UNO, so some of the information is not applicable. Here are some links.Kiwi,Another question: do you know, how the PWM frequency can be adjusted with the Leonardo controller? The PWM Frequency Library is not compatible with the ATmega32u4.
Thanks in advance
MacTester
Thanks for your positive comments, be aware this may be slow going for a while, I am in process of moving, so workshop and tools have been packed up.Hello, I'm a noob here but I used to build hackintoshes in the past before finally buying an Intel iMac. Lately I've found myself with one (maybe two) dead 17" iMac G5's and because of that I'm totally looking forward to this project!!! Thanks for sharing all of this information and keep it up, Kiwi!
If you are unable to get the audio working on your NUC you might want to consider adding one of those OSX compatible USB Audio dongles into your build, I used one of those with my old Core 2 Duo Hackintosh as every time they did a new update for snow leopard it would always break my system. After turning off my onboard audio and removing the audio kext files it worked perfectly until it was retired using the very inexpensive USB Audio dongle for sound.