Created
July 29, 2016 18:54
-
-
Save bangonkali/9adcc3a7ae4dbc5c3f5be59e866366ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino_FreeRTOS.h> | |
#include <semphr.h> // add the FreeRTOS functions for Semaphores (or Flags). | |
#include <LiquidCrystal.h> | |
#include "pitches.h" | |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | |
// Declare a mutex Semaphore Handle which we will use to manage the Serial Port. | |
// It will be used to ensure only only one Task is accessing this resource at any time. | |
SemaphoreHandle_t xSerialSemaphore; | |
// define two Tasks for DigitalRead & AnalogRead | |
void TaskDigitalRead( void *pvParameters ); | |
void TaskAnalogRead( void *pvParameters ); | |
int button1State; | |
int button2State; | |
int button3State; | |
int button4State; | |
int sensorValue; | |
int note=0; | |
// notes to play, corresponding to the 3 sensors: | |
const int notes[] = { | |
NOTE_A4, NOTE_B4, NOTE_C3, NOTE_E5 | |
}; | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
// initialize serial communication at 9600 bits per second: | |
// Serial.begin(9600); | |
// make the pushbutton's pin an input: | |
pinMode(6, INPUT); | |
pinMode(7, INPUT); | |
pinMode(8, INPUT); | |
pinMode(9, INPUT); | |
// Semaphores are useful to stop a Task proceeding, where it should be paused to wait, | |
// because it is sharing a resource, such as the Serial port. | |
// Semaphores should only be used whilst the scheduler is running, but we can set it up here. | |
if ( xSerialSemaphore == NULL ) // Check to confirm that the Serial Semaphore has not already been created. | |
{ | |
xSerialSemaphore = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use to manage the Serial Port | |
if ( ( xSerialSemaphore ) != NULL ) | |
xSemaphoreGive( ( xSerialSemaphore ) ); // Make the Serial Port available for use, by "Giving" the Semaphore. | |
} | |
// Now set up two Tasks to run independently. | |
xTaskCreate( | |
TaskLCD | |
, (const portCHAR *)"LCD" // A name just for humans | |
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater | |
, NULL | |
, 1 // Priority, with 1 being the highest, and 4 being the lowest. | |
, NULL ); | |
xTaskCreate( | |
TaskDigitalRead | |
, (const portCHAR *)"DigitalRead" // A name just for humans | |
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater | |
, NULL | |
, 1 // Priority, with 1 being the highest, and 4 being the lowest. | |
, NULL ); | |
xTaskCreate( | |
TaskAnalogRead | |
, (const portCHAR *) "AnalogRead" | |
, 128 // Stack size | |
, NULL | |
, 1 // Priority | |
, NULL ); | |
// Now the Task scheduler, which takes over control of scheduling individual Tasks, is automatically started. | |
} | |
void loop() | |
{ | |
// Empty. Things are done in Tasks. | |
} | |
/*--------------------------------------------------*/ | |
/*---------------------- Tasks ---------------------*/ | |
/*--------------------------------------------------*/ | |
void TaskLCD( void *pvParameters __attribute__((unused)) ) // This is a Task. | |
{ | |
// set up the LCD's number of columns and rows: | |
lcd.begin(16, 2); | |
lcd.print("State!"); | |
vTaskDelay(10); | |
for (;;) // A Task shall never return or exit. | |
{ | |
// lcd.noDisplay(); | |
// vTaskDelay(6); | |
// lcd.display(); | |
// lcd.clear(); | |
lcd.setCursor(0, 1); | |
// String strOut = sprintf("%04d", sensorValue); | |
// strOut += button2State + ","; | |
// strOut += button3State + ","; | |
// strOut += button4State + ","; | |
// strOut += sensorValue; | |
lcd.print(button1State); | |
lcd.print(","); | |
lcd.print(button2State); | |
lcd.print(","); | |
lcd.print(button3State); | |
lcd.print(","); | |
lcd.print(button4State); | |
if (sensorValue < 10) | |
lcd.print(",000"); | |
else if (sensorValue < 100) | |
lcd.print(",00"); | |
else if (sensorValue < 1000) | |
lcd.print(",0"); | |
else if (sensorValue < 10000) | |
lcd.print(","); | |
lcd.print(sensorValue); | |
vTaskDelay(3); | |
} | |
} | |
void TaskDigitalRead( void *pvParameters __attribute__((unused)) ) // This is a Task. | |
{ | |
/* | |
DigitalReadSerial | |
Reads a digital input on pin 2, prints the result to the serial monitor | |
This example code is in the public domain. | |
*/ | |
for (;;) // A Task shall never return or exit. | |
{ | |
// read the input pin: | |
button1State = digitalRead(6); | |
button2State = digitalRead(7); | |
button3State = digitalRead(8); | |
button4State = digitalRead(9); | |
if (button1State > 0 or button2State > 0 or button3State > 0 or button4State > 0){ | |
if (button1State) { | |
note=0; | |
} else if (button2State) { | |
note=1; | |
} else if (button3State) { | |
note=2; | |
} else if (button4State) { | |
note=3; | |
} | |
tone(10, notes[note], sensorValue); | |
} | |
// if (button2State){ | |
// noTone(10); | |
// tone(10, notes[1], sensorValue); | |
// } | |
// if (button3State){ | |
// noTone(10); | |
// tone(10, notes[2], sensorValue); | |
// } | |
// if (button4State){ | |
// noTone(10); | |
// tone(10, notes[3], sensorValue); | |
// } | |
// See if we can obtain or "Take" the Serial Semaphore. | |
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free. | |
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE ) | |
{ | |
// We were able to obtain or "Take" the semaphore and can now access the shared resource. | |
// We want to have the Serial Port for us alone, as it takes some time to print, | |
// so we don't want it getting stolen during the middle of a conversion. | |
// print out the state of the button: | |
// Serial.println(button1State); | |
// Serial.println(button2State); | |
// Serial.println(button3State); | |
// Serial.println(button4State); | |
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others. | |
} | |
vTaskDelay(1); // one tick delay (15ms) in between reads for stability | |
} | |
} | |
void TaskAnalogRead( void *pvParameters __attribute__((unused)) ) // This is a Task. | |
{ | |
for (;;) | |
{ | |
// See if we can obtain or "Take" the Serial Semaphore. | |
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free. | |
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE ) | |
{ | |
// read the input on analog pin 0: | |
sensorValue = analogRead(A1); | |
// We were able to obtain or "Take" the semaphore and can now access the shared resource. | |
// We want to have the Serial Port for us alone, as it takes some time to print, | |
// so we don't want it getting stolen during the middle of a conversion. | |
// print out the value you read: | |
// Serial.println(sensorValue); | |
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others. | |
} | |
vTaskDelay(1); // one tick delay (15ms) in between reads for stability | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment