uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

freertos.ino (2913B)


      1 #include <Arduino_FreeRTOS.h>
      2 #include <LiquidCrystal.h>
      3 #include <Servo.h>
      4 #include <queue.h>
      5 
      6 #define PIN_BTN_LEFT 2
      7 #define PIN_BTN_RIGHT 3
      8 #define PIN_BTN_ALARM 13
      9 #define PIN_TURN_LEFT 4
     10 #define PIN_TURN_RIGHT 5
     11 #define PIN_SERVO 6
     12 #define PIN_POTENTIOMETER A0
     13 
     14 #define TURN_SIGNAL_DELAY 500
     15 
     16 QueueHandle_t queue;
     17 LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
     18 Servo servo;
     19 
     20 void
     21 setup()
     22 {
     23 	Serial.begin(9600);
     24 	pinMode(PIN_BTN_LEFT, INPUT_PULLUP);
     25 	pinMode(PIN_BTN_RIGHT, INPUT_PULLUP);
     26 	pinMode(PIN_BTN_ALARM, INPUT_PULLUP);
     27 	lcd.begin(16, 2);
     28 	servo.attach(PIN_SERVO);
     29 	queue = xQueueCreate(5, sizeof(int));
     30 	if (queue == NULL) {
     31 		Serial.println("queue cannot be created");
     32 		/* hang */
     33 		for (;;);
     34 	}
     35 	xTaskCreate(turn_signal_left, "Left turn singal", 128, NULL, 1, NULL);
     36 	xTaskCreate(turn_signal_right, "Right turn singal", 128, NULL, 1, NULL);
     37 	xTaskCreate(alarm, "Alarm", 128, NULL, 1, NULL);
     38 	xTaskCreate(rpm_count, "RPM count", 128, NULL, 1, NULL);
     39 	xTaskCreate(tachometer, "Tachometer", 128, NULL, 1, NULL);
     40 	vTaskStartScheduler();
     41 }
     42 
     43 void
     44 loop()
     45 {
     46 }
     47 
     48 int
     49 btn_pressed(int btn_pin)
     50 {
     51 	return (digitalRead(btn_pin) == LOW);
     52 }
     53 
     54 void
     55 turn_signal_left(void *pv_params)
     56 {
     57 	if (!btn_pressed(PIN_BTN_LEFT))
     58 		return;
     59 	pinMode(PIN_TURN_LEFT, OUTPUT);
     60 	for (;;) {
     61 		Serial.println("Left turn");
     62 		digitalWrite(PIN_TURN_LEFT, HIGH);
     63 		vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS);
     64 		digitalWrite(PIN_TURN_LEFT, LOW);
     65 		vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS);
     66 	}
     67 }
     68 
     69 void
     70 turn_signal_right(void *pv_params)
     71 {
     72 	if (!btn_pressed(PIN_BTN_RIGHT))
     73 		return;
     74 	pinMode(PIN_TURN_RIGHT, OUTPUT);
     75 	for (;;) {
     76 		Serial.println("Right turn");
     77 		digitalWrite(PIN_TURN_RIGHT, HIGH);
     78 		vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS);
     79 		digitalWrite(PIN_TURN_RIGHT, LOW);
     80 		vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS);
     81 	}
     82 }
     83 
     84 void
     85 alarm(void *pv_params)
     86 {
     87 	if (!btn_pressed(PIN_BTN_ALARM))
     88 		return;
     89 	pinMode(PIN_TURN_LEFT, OUTPUT);
     90 	pinMode(PIN_TURN_RIGHT, OUTPUT);
     91 	for (;;) {
     92 		Serial.println("Alarm");
     93 		digitalWrite(PIN_TURN_LEFT, HIGH);
     94 		digitalWrite(PIN_TURN_RIGHT, HIGH);
     95 		vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS);
     96 		digitalWrite(PIN_TURN_LEFT, LOW);
     97 		digitalWrite(PIN_TURN_RIGHT, LOW);
     98 		vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS);
     99 	}
    100 }
    101 
    102 void
    103 rpm_count(void *pv_params)
    104 {
    105 	int rpm, servo_angle;
    106 
    107 	for (;;) {
    108 		servo_angle = map(analogRead(PIN_POTENTIOMETER), 0, 1023, 0, 180);
    109 		servo.write(servo_angle);
    110 		rpm = map(servo_angle, 0, 180, 0, 8000);
    111 		Serial.print("RPM: ");
    112 		Serial.println(rpm);
    113 		xQueueSend(queue, &rpm, portMAX_DELAY);
    114 		vTaskDelay(1000 / portTICK_PERIOD_MS);
    115 	}
    116 }
    117 
    118 void
    119 tachometer(void *pv_params)
    120 {
    121 	int rpm;
    122 
    123 	for (;;) {
    124 		if (xQueueReceive(queue, &rpm, portMAX_DELAY) != pdPASS)
    125 			return;
    126 		Serial.print("Tachometer: ");
    127 		Serial.println(rpm);
    128 		lcd.clear();
    129 		lcd.setCursor(0, 0);
    130 		lcd.print("RPM: ");
    131 		lcd.setCursor(6, 0);
    132 		lcd.print(rpm);
    133 	}
    134 }