Skip to content

Commit

Permalink
Created new example: SensorThread
Browse files Browse the repository at this point in the history
Shows how to create a new class extending Thread.
Minor changes in ControllerWithTimer
  • Loading branch information
ivanseidel committed May 11, 2013
1 parent a41cf82 commit 020c0c0
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 19 deletions.
63 changes: 44 additions & 19 deletions examples/ControllerWithTimer/ControllerWithTimer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@
// ThreadController that will controll all threads
ThreadController controll = ThreadController();

//My Thread (as a pointer)
Thread* myThread = new Thread();
//His Thread (not pointer)
//My Thread
Thread myThread = Thread();
//His Thread
Thread hisThread = Thread();

// callback for myThread
void niceCallback(){
Serial.print("COOL! I'm running on: ");
Serial.println(millis());
void myThreadCallback(){
Serial.println("myThread\t\tcallback");
}

// callback for hisThread
void boringCallback(){
Serial.println("BORING...");
void hisThreadCallback(){
Serial.println("\thisThread\tcallback");
}

// This is the callback for the Timer
Expand All @@ -41,21 +40,21 @@ void setup(){
Serial.begin(9600);

// Configure myThread
myThread->onRun(niceCallback);
myThread->setInterval(500);
myThread.onRun(myThreadCallback);
myThread.setInterval(500);

// Configure myThread
hisThread.onRun(boringCallback);
hisThread.setInterval(250);
hisThread.onRun(hisThreadCallback);
hisThread.setInterval(200);

// Adds both threads to the controller
controll.add(myThread);
controll.add(&hisThread); // & to pass the pointer to it
controll.add(&myThread); // & to pass the pointer to it
controll.add(&hisThread);

/*
If using DueTimer...
*/
// Timer1.attachInterrupt(timerCallback).start(10000);
// Timer1.attachInterrupt(timerCallback).start(20000);

/*
If using TimerOne...
Expand All @@ -65,11 +64,37 @@ void setup(){
// Timer1.start();
}

void waitSerial(){
while (!Serial.available());
delay(10);
while (Serial.available() && Serial.read());
}

void loop(){
while(1){
float h = 3.1415;
h/=2;
Serial.println("Help me! I'm stuck here...");
delay(1000);
noInterrupts(); // Call to disable interrupts
Serial.println("Type anyting to stop myThread!");
interrupts(); // Call to enable interrupts
waitSerial();
myThread.enabled = false;

noInterrupts();
Serial.println("Type anyting to stop hisThread!");
interrupts();
waitSerial();
hisThread.enabled = false;

noInterrupts();
Serial.println("Type anyting to enable myThread!");
interrupts();
waitSerial();
myThread.enabled = true;

noInterrupts();
Serial.println("Type anyting to enable hisThread!");
interrupts();
waitSerial();
hisThread.enabled = true;

}
}
104 changes: 104 additions & 0 deletions examples/SensorThread/SensorThread.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "Thread.h"
#include "ThreadController.h"
/*
This is a more "complex" for of using Threads.
You can also inherit from Thread, and do your entire code on the class.
This allows you, to create for example:
Sensor Readings (aquire, filter, and save localy values)
Custom Blinks, Beeps...
Anything you can imagine.
Threads are more "usefull" when used within Timer interrupts
This way of coding is more "reusable", and "correct" (Object Oriented)
*/


/*
This example, requires a Timer Interrupt Library.
If you are using Arduino NANO, UNO... (with ATmega168/328)
Please go to: http://playground.arduino.cc/code/timer1
If you are using Arduino DUE,
Please go to: https://github.com/ivanseidel/DueTimer
Include the library corresponding to your Arduino.
*/
#include <DueTimer.h>
// #include <TimerOne.h>

// Create a new Class, called SensorThread, that inherits from Thread
class SensorThread: public Thread
{
public:
int value;
int pin;

// No, "run" cannot be anything...
// Because Thread uses the method "run" to run threads,
// we MUST overload this method here. using anything other
// than "run" will not work properly...
void run(){
// Reads the analog pin, and saves it localy
value = map(analogRead(pin), 0,1023,0,255);
}
};

// Now, let's use our new class of Thread
SensorThread analog1 = SensorThread();
SensorThread analog2 = SensorThread();

// Instantiate a new ThreadController
ThreadController controller = ThreadController();

// This is the callback for the Timer
void timerCallback(){
controller.run();
}

void setup(){

Serial.begin(9600);

// Configures Thread analog1
analog1.pin = A1;
analog1.setInterval(100);

// Configures Thread analog2
analog2.pin = A2;
analog2.setInterval(100);

// Add the Threads to our ThreadController
controller.add(&analog1);
controller.add(&analog2);

/*
If using DueTimer...
*/
Timer1.attachInterrupt(timerCallback).start(10000);

/*
If using TimerOne...
*/
// Timer1.initialize(20000);
// Timer1.attachInterrupt(timerCallback);
// Timer1.start();

}

void loop(){
// Do complex-crazy-timeconsuming-tasks here
delay(1000);

// Get the fresh readings
Serial.print("Analog1 Thread: ");
Serial.println(analog1.value);

Serial.print("Analog2 Thread: ");
Serial.println(analog2.value);

// Do more complex-crazy-timeconsuming-tasks here
delay(1000);

}

0 comments on commit 020c0c0

Please sign in to comment.