JPRoy

Simple elevator

Feb 25th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #define USE_OCTOWS2811
  2. #include<OctoWS2811.h>
  3. #include <FastLED.h>
  4.  
  5. #define NUM_STRIPS 8
  6. #define NUM_LEDS_PER_STRIP 64
  7. #define NUM_LEDS 512
  8. #define MAX_BRIGHTNESS 128
  9.  
  10. CRGB leds[NUM_LEDS];
  11.  
  12. #define elevator_size 4 // these are not changing during program execution
  13. #define top_floor 10 // but you can modify them each time you recompile
  14. #define floor_height 8 // to change the basic behaviour of your sketch
  15.  
  16. int current_floor = 0; // these will be modified actively during program execution
  17. int which_floor = 0;
  18. int elevator_position = 0;
  19.  
  20. void setup() {
  21.  
  22. Serial.begin(9600);
  23.  
  24. LEDS.addLeds<OCTOWS2811, RGB>(leds, NUM_LEDS_PER_STRIP);
  25. FastLED.setBrightness(MAX_BRIGHTNESS);
  26. fill_solid(leds, NUM_LEDS, CRGB::Black);
  27. for (int i = 0; i < elevator_size; i++){
  28. leds[i] = CRGB::Red;
  29. }
  30. FastLED.show();
  31. delay(2000);
  32.  
  33. }
  34. void loop(){
  35.  
  36. which_floor = random8(0, top_floor);
  37.  
  38. Serial.print("current_floor = ");
  39. Serial.print(current_floor);
  40. Serial.print(", which_floor = ");
  41. Serial.println(which_floor);
  42.  
  43. if (which_floor > current_floor) { // Going up
  44. for ( elevator_position = current_floor*floor_height;
  45. elevator_position <= which_floor*floor_height;
  46. elevator_position++) {
  47.  
  48. Serial.print("elevator_position = ");
  49. Serial.println(elevator_position);
  50.  
  51. fill_solid(leds, NUM_LEDS, CRGB::Black);
  52. for (int i = 0; i < elevator_size; i++) leds[elevator_position+i] = CRGB::Red;
  53. FastLED.show();
  54. delay(200);
  55. }
  56. }
  57.  
  58. else { // Going down or just staying there
  59. for ( elevator_position = current_floor*floor_height;
  60. elevator_position >= which_floor*floor_height;
  61. elevator_position--) {
  62.  
  63. Serial.print("elevator_position = ");
  64. Serial.println(elevator_position);
  65.  
  66. fill_solid(leds, NUM_LEDS, CRGB::Black);
  67. for (int i = 0; i < elevator_size; i++) leds[elevator_position+i] = CRGB::Red;
  68. FastLED.show();
  69. delay(200);
  70. }
  71. }
  72.  
  73. current_floor = which_floor;
  74.  
  75. delay(2000); // or any value you want or replace 2000 with some random value !?
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment