Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Arduino Protothreading Example v1.1
  3. by Drew Alden (@ReanimationXP) 1/12/2016
  4.  
  5. - Update: v1.1 - 8/18/17
  6.   Arduino 1.6.6+ prototyping changed, small fixes.
  7.   (create functions ahead of use, removed foreach and related library).
  8.  
  9.   Note that TimedAction is now out of date. Be sure to read notes about
  10.   TimedAction and WProgram.h / Arduino.h errors.
  11. */
  12.  
  13. //COMPONENTS
  14.  
  15. /*
  16. This code was made using the Sunfounder Arduino starter kit's blue LCD.
  17. It can be found at Amazon.com in a variety of kits.
  18. */
  19.  
  20. //THIRD-PARTY LIBRARIES
  21. //these must be manually added to your Arduino IDE installation
  22.  
  23. //TimedAction
  24. //allows us to set actions to perform on separate timed intervals
  25. //http://playground.arduino.cc/Code/TimedAction
  26. //http://wiring.uniandes.edu.co/source/trunk/wiring/firmware/libraries/TimedAction
  27.  
  28. #include <TimedAction.h>
  29. //NOTE: This library has an issue on newer versions of Arduino. After
  30. //      downloading the library you MUST go into the library directory and
  31. //      edit TimedAction.h. Within, overwrite WProgram.h with Arduino.h
  32.  
  33.  
  34. //NATIVE LIBRARIES
  35.  
  36. #include <LiquidCrystal.h>
  37.     /*
  38.       LiquidCrystal Library - Hello World
  39.    
  40.      Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  41.      library works with all LCD displays that are compatible with the
  42.      Hitachi HD44780 driver. There are many of them out there, and you
  43.      can usually tell them by the 16-pin interface.
  44.  
  45.      One example circuit:
  46.      * LCD RS pin to digital pin 12.
  47.      * LCD Enable/E/EN pin to digital pin 11
  48.      * LCD D4 pin to digital pin 5
  49.      * LCD D5 pin to digital pin 4
  50.      * LCD D6 pin to digital pin 3
  51.      * LCD D7 pin to digital pin 2
  52.      * LCD R/W pin to ground
  53.      * LCD VSS pin to ground
  54.      * LCD VCC/VDD pin to 5V
  55.      * 10K resistor:
  56.      * ends to +5V and ground
  57.      * wiper (middle) to LCD VO pin (pin 3)
  58.      *Backlit Displays:
  59.      * LCD K pin to ground (if present)
  60.      * LCD A pin to 220ohm (red red black black (brown)) resistor, then
  61.        resistor to pin 9
  62.    
  63.      This example code is in the public domain.
  64.    
  65.      http://www.arduino.cc/en/Tutorial/LiquidCrystal
  66.      */
  67.  
  68. //GLOBALS
  69. int backlightPin = 9;   // used for backlight fading
  70.  
  71. int timerCounter = 0;   // incrementing counter. will crash eventually.
  72. int stringNo = 0;       //which text string to show
  73. //                   "16 CHARACTER MAX"
  74. char* stringArray[]={"Check it out... ",
  75.                      "I have 3 threads",
  76.                      "going at once...",
  77.                      "Cool, huh?! :D  "};
  78.                      
  79. //INIT
  80.  
  81. // This should probably be done inside setup(), but whatever.
  82. // initialize the LCD library with the numbers of the interface pins
  83. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  84.  
  85. //FUNCTIONS
  86.  
  87. //this is our first task, print an incrementing number to the LCD
  88. void incrementNumber(){
  89.    // set the cursor to column 0, line 1
  90.   // (note: line 1 is the second row, since counting begins with 0):
  91.   lcd.setCursor(0, 1);
  92.   // add one to the counter, then display it.
  93.   timerCounter = timerCounter + 1;  
  94.   lcd.print(timerCounter);
  95. }
  96.  
  97. //our second task, fires every few seconds and rotates text strings
  98. void changeText(){  
  99.   // Print a message to the LCD.
  100.   lcd.setCursor(0, 0);
  101.   lcd.print(stringArray[stringNo]);
  102.  
  103.   //nasty hack to get number of Array elements
  104.   if (stringNo >= sizeof(stringArray)/sizeof(char *)){  
  105.     stringNo = 0;
  106.     changeText();
  107.   }
  108.   else{
  109.     stringNo = stringNo + 1;  
  110.   }
  111. }
  112.  
  113. //Create a couple timers that will fire repeatedly every x ms
  114.  
  115. //edit: these lines used to be in front of incrementNumber and changeText
  116. //      functions. this didn't work because the functions weren't defined yet!
  117. TimedAction numberThread = TimedAction(700,incrementNumber);
  118. TimedAction textThread = TimedAction(3000,changeText);
  119.  
  120. // where's our third task? well, it's the main loop itself :) the task
  121. // which repeats most often should be used as the loop. other
  122. // tasks are able to "interrupt" the fastest repeating task.
  123.  
  124.  
  125. void setup() {
  126.   //define the LCD's number of columns and rows:
  127.   lcd.begin(16, 2);
  128.   //fire changeText once to paint the initial string [0]
  129.   changeText();
  130. }
  131.  
  132.  
  133. void loop() {
  134.  
  135.   //check on our threads. based on how long the system has been
  136.   //running, do they need to fire and do work? if so, do it!
  137.   numberThread.check();
  138.   textThread.check();
  139.  
  140.   //third task, fade in backlight from min to max brightness
  141.   //in increments of 5 points:
  142.   digitalWrite(13, HIGH);
  143.   for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 10) {
  144.    
  145.     //wait a second, why am i checking on the threads here? because
  146.     //this is a for loop. you must check on your threads during ANY
  147.     //loops that occur, including the main one!
  148.     numberThread.check();
  149.     textThread.check();
  150.    
  151.     //sets the value (range from 0 to 255):
  152.     analogWrite(backlightPin, fadeValue);
  153.    
  154.     // wait for 20 milliseconds to see the dimming effect
  155.     // keep delays on the main loop SHORT. these WILL prevent
  156.     // other threads from firing on time.
  157.     delay(20);
  158.   }
  159.  
  160.   //fade out from max to min in increments of 5 points:
  161.   digitalWrite(13, LOW);
  162.   for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 10) {
  163.    
  164.     //check on our threads again
  165.     numberThread.check();
  166.     textThread.check();
  167.    
  168.     //sets the value (range from 0 to 255):
  169.     analogWrite(backlightPin, fadeValue);
  170.    
  171.     //wait for 20 milliseconds to see the dimming effect
  172.     delay(20);
  173.   }
  174.  
  175.   /*  
  176.  
  177.   For some scrolling message fun in the future...
  178.  
  179.   lcd.setCursor(15,0);  // set the cursor to column 15, line 0
  180.     for (int positionCounter1 = 0; positionCounter1 < 26; positionCounter1++)
  181.     {
  182.       lcd.scrollDisplayLeft();
  183.       //Scrolls the contents of the display one space to the left.
  184.       lcd.print(array1[positionCounter1]);
  185.       // Print a message to the LCD.
  186.       delay(tim);  //wait for 250 microseconds
  187.     }
  188.     lcd.clear();  
  189.     //Clears the LCD screen and positions the cursor in the upper-left corner.
  190.    
  191.     lcd.setCursor(15,1);  // set the cursor to column 15, line 1
  192.     for (int positionCounter = 0; positionCounter < 26; positionCounter++){
  193.       lcd.scrollDisplayLeft();
  194.       //Scrolls the contents of the display one space to the left.
  195.       lcd.print(array2[positionCounter]);  // Print a message to the LCD.
  196.       delay(tim);  //wait for 250 microseconds
  197.     }
  198.     lcd.clear();
  199.     //Clears the LCD screen and positions the cursor in the upper-left corner.
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement