Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<arduinoPlatform.h>
- #include<tasks.h>
- #include<interrupts.h>
- #include<stdio.h>
- #include<serial.h>
- #include <pwm.h>
- #include<data.h>
- extern serial Serial;
- /*----------- PRVA TAVCKA ------------*/
- int x1 = 0,
- x2 = 0;
- /*-----------------------------------*/
- /*=========== DRUGA TACKA ===========*/
- const int sw[] = {2, 7, 8, 35};
- int fun1(){
- int dec = 0;
- for(int i = 0; i < 4; ++i){
- dec += digitalRead(sw[i]) * pow(2, i);
- }
- return dec;
- }
- /*===================================*/
- /*~~~~~~~~~~~~ TRECA TACKA ~~~~~~~~~~*/
- const int BTN1 = 4,
- BTN2 = 34;
- int BTN1_old_state,
- BTN2_old_state;
- void print_x1_x2(){
- Serial.print("task1: x1=");
- Serial.print(x1);
- Serial.print(" x2=");
- Serial.println(x2);
- }
- void task1(int id, void* ptrt){
- if(BTN1_old_state < digitalRead(BTN1)){
- x1 = fun1();
- print_x1_x2();
- BTN1_old_state = digitalRead(BTN1);
- }else if(BTN1_old_state > digitalRead(BTN2)){
- BTN1_old_state = digitalRead(BTN1);
- }
- if(BTN2_old_state < digitalRead(BTN2)){
- x2 = fun1();
- print_x1_x2();
- BTN2_old_state = digitalRead(BTN2);
- }else if(BTN2_old_state > digitalRead(BTN2)){
- BTN2_old_state = digitalRead(BTN2);
- }
- }
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- /*=============== CETVRTA TACKA ==========*/
- typedef void interruptFunction();
- interruptFunction *sw1_rising_edge, *sw2_rising_edge, *sw3_rising_edge, *sw4_rising_edge,
- *sw1_falling_edge, *sw2_falling_edge, *sw3_falling_edge, *sw4_falling_edge;
- int sw1_old_state,
- sw2_old_state,
- sw3_old_state,
- sw4_old_state;
- void sw1_attach_interrupt(interruptFunction rise, interruptFunction fall){
- sw1_rising_edge = rise;
- sw1_falling_edge = fall;
- }
- void sw2_attach_interrupt(interruptFunction rise, interruptFunction fall){
- sw2_rising_edge = rise;
- sw2_falling_edge = fall;
- }
- void sw3_attach_interrupt(interruptFunction rise, interruptFunction fall){
- sw3_rising_edge = rise;
- sw3_falling_edge = fall;
- }
- void sw4_attach_interrupt(interruptFunction rise, interruptFunction fall){
- sw4_rising_edge = rise;
- sw4_falling_edge = fall;
- }
- void sw1_int_on(){
- digitalWrite(26, HIGH);
- }
- void sw2_int_on(){
- digitalWrite(27, HIGH);
- }
- void sw3_int_on(){
- digitalWrite(28, HIGH);
- }
- void sw4_int_on(){
- digitalWrite(29, HIGH);
- }
- void sw1_int_off(){
- digitalWrite(26, LOW);
- }
- void sw2_int_off(){
- digitalWrite(27, LOW);
- }
- void sw3_int_off(){
- digitalWrite(28, LOW);
- }
- void sw4_int_off(){
- digitalWrite(29, LOW);
- }
- void sw1_check(int id, void* ptrt){
- if(sw1_old_state < digitalRead(sw[0])){
- sw1_rising_edge();
- sw1_old_state = digitalRead(sw[0]);
- }else if(sw1_old_state > digitalRead(sw[0])){
- sw1_falling_edge();
- sw1_old_state = digitalRead(sw[0]);
- }
- }
- void sw2_check(int id, void* ptrt){
- if(sw2_old_state < digitalRead(sw[1])){
- sw2_rising_edge();
- sw2_old_state = digitalRead(sw[1]);
- }else if(sw2_old_state > digitalRead(sw[1])){
- sw2_falling_edge();
- sw2_old_state = digitalRead(sw[1]);
- }
- }
- void sw3_check(int id, void* ptrt){
- if(sw3_old_state < digitalRead(sw[2])){
- sw3_rising_edge();
- sw3_old_state = digitalRead(sw[2]);
- }else if(sw3_old_state > digitalRead(sw[2])){
- sw3_falling_edge();
- sw3_old_state = digitalRead(sw[2]);
- }
- }
- void sw4_check(int id, void* ptrt){
- if(sw4_old_state < digitalRead(sw[3])){
- sw4_rising_edge();
- sw4_old_state = digitalRead(sw[3]);
- }else if(sw4_old_state > digitalRead(sw[3])){
- sw4_falling_edge();
- sw4_old_state = digitalRead(sw[3]);
- }
- }
- /*=======================================*/
- /*-=-=-=-=-=-=- PETA TACKA =-=-=-=-=-=-=-=*/
- const int BTN3 = 36;
- int BTN3_old_state,
- BTN3_start_time;
- void task2(int id, void* ptrt){
- if(BTN3_old_state < digitalRead(BTN3)){
- BTN3_start_time = millis();
- BTN3_old_state = digitalRead(BTN3);
- }else if(BTN3_old_state > digitalRead(BTN3)){
- if(millis() - BTN3_start_time >= 2000){
- Serial.print("Proizvod brojeva x1 i x2 je: ");
- Serial.println(x1 * x2);
- }
- BTN3_old_state = digitalRead(BTN3);
- }
- }
- /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
- void setup()
- {
- /*----------- PRVA TAVCKA ------------*/
- Serial.begin(9600);
- for(int i = 26; i <= 33; ++i){
- pinMode(i, OUTPUT);
- digitalWrite(i, LOW);
- }
- Serial.println("Martin Muharemovic - PR130/2021 Grupa 7");
- /*-----------------------------------------*/
- /*~~~~~~~~~~~~ TRECA TACKA ~~~~~~~~~~*/
- BTN1_old_state = digitalRead(BTN1);
- BTN2_old_state = digitalRead(BTN2);
- createTask(task1, 25, TASK_ENABLE, NULL);
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- /*=============== CETVRTA TACKA ==========*/
- sw1_old_state = digitalRead(sw[0]);
- sw2_old_state = digitalRead(sw[1]);
- sw3_old_state = digitalRead(sw[2]);
- sw4_old_state = digitalRead(sw[3]);
- sw1_attach_interrupt(sw1_int_on,sw1_int_off);
- sw2_attach_interrupt(sw2_int_on,sw2_int_off);
- sw3_attach_interrupt(sw3_int_on,sw3_int_off);
- sw4_attach_interrupt(sw4_int_on,sw4_int_off);
- createTask(sw1_check, 50, TASK_ENABLE, NULL);
- createTask(sw2_check, 50, TASK_ENABLE, NULL);
- createTask(sw3_check, 50, TASK_ENABLE, NULL);
- createTask(sw4_check, 50, TASK_ENABLE, NULL);
- /*========================================*/
- /*-=-=-=-=-=-=- PETA TACKA =-=-=-=-=-=-=-=*/
- BTN3_old_state = digitalRead(BTN3);
- createTask(task2, 25, TASK_ENABLE, NULL);
- /*=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-*/
- }
- void loop()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment