/* blink_analogRead474.ino
*
* Lab 4 - Part A
* February 26, 2024
* EE/CSE 474
*
* Authors: Whitney Waldinger, Uma Nene
*
*
* Acknowledgements: Borrowed code from in class example. Updated
* to use the joystick given in our lab kit as the
* analog input. Because we used the joystick with
* two different outputs (x and y) we are printing
* both of them in TaskAnalogRead.
*/
#include <Arduino_FreeRTOS.h>
////////////////////////////////////////////////
// APPROVED FOR ECE 474 Spring 2021
//
// NOTE: modify analogRead() on line 113 according
// to your setup.
////////////////////////////////////////////////
#define VRX A1
#define VRY A0
// define two tasks for Blink & AnalogRead
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
int last_joystick_x;
int last_joystick_y;
int randNum;
int points = 0;
int i = 0;
int taskControl[4] = {0, 0, 0, 0};
// 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(19200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
}
// Now set up two tasks to run independently.
xTaskCreate(
TaskBlink
, "Blink" // A name just for humans
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 3 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL );
xTaskCreate(
TaskAnalogRead
, "AnalogRead"
, 128 // Stack size
, NULL
, 2 // Priority
, NULL );
xTaskCreate(
TaskController
, "task control"
, 128
, NULL
, 1
, NULL );
xTaskCreate(
TaskButtonRead
, "Button"
, 128
, NULL
, 2
, NULL);
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
// (note how the above comment is WRONG!!!)
vTaskStartScheduler();
}
void loop()
{
// Empty. Things are done in Tasks.
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskBlink(void *pvParameters) // This is a task.
{
// (void) pvParameters; // allocate stack space for params
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, LEONARDO, MEGA, and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN takes care
of use the correct LED pin whatever is the board used.
The MICRO does not have a LED_BUILTIN available. For the MICRO board please substitute
the LED_BUILTIN definition with either LED_BUILTIN_RX or LED_BUILTIN_TX.
e.g. pinMode(LED_BUILTIN_RX, OUTPUT); etc.
If you want to know what pin the on-board LED is connected to on your Arduino model, check
the Technical Specs of your board at https://www.arduino.cc/en/Main/Products
This example code is in the public domain.
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
*/
// initialize digital LED_BUILTIN on pin 13 as an output.
pinMode(LED_BUILTIN, OUTPUT);
for (;;) // A Task shall never return or exit.
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 100 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 200 / portTICK_PERIOD_MS ); // wait for one second
}
}
void TaskAnalogRead(void *pvParameters) // This is a task.
{
// (void) pvParameters;
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
int x;
int y;
for (;;)
{
x = analogRead(VRX); // reads analog input from X pin (A1)
y = analogRead(VRY); // reads analog input from Y pin (A0)
if(taskControl[0] & ((last_joystick_x != x) | (last_joystick_y != y))) {
points++;
taskControl[0] = 0;
}
last_joystick_x = x;
last_joystick_y = y;
// print out the values read:
Serial.print("pts: ");
Serial.println(points);
/*Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.println(y);*/
vTaskDelay(500/portTICK_PERIOD_MS); // 0.5 sec in between reads for stability
}
}
void TaskController(void *pvParameters)
{
for (;;)
{
randNum = random(0,4);
taskControl[randNum] = 1;
taskControl[(randNum + 1) % 4] = 0;
taskControl[(randNum + 2) % 4] = 0;
taskControl[(randNum + 3) % 4] = 0;
Serial.println(randNum);
i++;
vTaskDelay(4000/portTICK_PERIOD_MS);
}
}
void TaskButtonRead(void *pvParameters) {
pinMode(7, INPUT_PULLUP);
for(;;) {
if (taskControl[1] & digitalRead(7) == LOW) {
points++;
taskControl[1] = 0;
}
vTaskDelay(200/portTICK_PERIOD_MS);
}
}