SHARE
TWEET

Zoetrope Code

a guest Feb 23rd, 2020 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. #include <boarddefs.h>
  3. #include <IRremote.h>
  4. #include <IRremoteInt.h>
  5. #include <ir_Lego_PF_BitStreamEncoder.h>
  6. #define first_key  48703
  7. #define second_key  58359
  8.  
  9. int receiver_pin = 12;  
  10. int first_led_pin = 10;
  11. int second_led_pin = 11;
  12. int led[] = {0,0,0,0};
  13. IRrecv receiver(receiver_pin);
  14. decode_results output;
  15. const int controlPin1 = 2; // connected to pin 7 on the H-bridge
  16. const int controlPin2 = 3; // connected to pin 2 on the H-bridge
  17. const int enablePin = 9;   // connected to pin 1 on the H-bridge
  18. const int directionSwitchPin = 4;  // connected to the switch for direction
  19. const int onOffSwitchStateSwitchPin = 5; // connected to the switch for turning the motor on and off
  20. const int potPin = A0;  // connected to the potentiometer's output
  21. // create some variables to hold values from your inputs
  22. int onOffSwitchState = 0;  // current state of the On/Off switch
  23. int previousOnOffSwitchState = 0; // previous position of the on/off switch
  24. int directionSwitchState = 0;  // current state of the direction switch
  25. int previousDirectionSwitchState = 0;  // previous state of the direction switch
  26. int motorEnabled = 0; // Turns the motor on/off
  27. int motorSpeed = 0; // speed of the motor
  28. int motorDirection = 1; // current direction of the motor
  29. LiquidCrystal lcd (A4, A5)
  30.  
  31. void setup(){
  32.  
  33. Serial.begin(9600);
  34. receiver.enableIRIn();  
  35. pinMode(first_led_pin, OUTPUT);
  36. pinMode(second_led_pin, OUTPUT);
  37.  
  38. // intialize the inputs and outputs
  39. pinMode(directionSwitchPin, INPUT);
  40. pinMode(onOffSwitchStateSwitchPin, INPUT);
  41. pinMode(controlPin1, OUTPUT);
  42. pinMode(controlPin2, OUTPUT);
  43. pinMode(enablePin, OUTPUT);
  44. // pull the enable pin LOW to start
  45. digitalWrite(enablePin, LOW);
  46. //lcd
  47. lcd.begin (16,2);
  48. lcd.setCursor (5,0); //first row 5th column
  49. lcd.print ("my name:");
  50. lcd.setCursor (5,1); //second row 5th column
  51. lcd.print ("alondra");
  52. }
  53.  
  54. void loop(){
  55.  
  56. if (receiver.decode(&output)) {
  57. unsigned int value = output.value;
  58. switch(value) {
  59. case first_key:    
  60. if(led[1] == 1) {      
  61. digitalWrite(first_led_pin, LOW);
  62. led[1] = 0;        
  63. } else {                      
  64. digitalWrite(first_led_pin, HIGH);
  65. led[1] = 1;          
  66. }
  67. break;
  68. case second_key:
  69. if(led[2] == 1) {
  70. digitalWrite(second_led_pin, LOW);
  71. led[2] = 0;
  72. } else {
  73. digitalWrite(second_led_pin, HIGH);
  74. led[2] = 1;
  75. }
  76. }
  77. Serial.println(value);
  78. receiver.resume();
  79. }
  80. // read the value of the on/off switch
  81. onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  82. delay(1);
  83.  
  84. // read the value of the direction switch
  85. directionSwitchState = digitalRead(directionSwitchPin);
  86. // read the value of the pot and divide by 4 to get
  87. // a value that can be used for PWM
  88. motorSpeed = analogRead(potPin)/4;
  89.  
  90. // if the on/off button changed state since the last loop()
  91. if(onOffSwitchState != previousOnOffSwitchState){
  92. // change the value of motorEnabled if pressed
  93. if(onOffSwitchState == HIGH){
  94. motorEnabled = !motorEnabled;
  95. }
  96. }
  97.  
  98. // if the direction button changed state since the last loop()
  99. if (directionSwitchState != previousDirectionSwitchState) {
  100. // change the value of motorDirection if pressed
  101. if (directionSwitchState == HIGH) {
  102. motorDirection = !motorDirection;
  103. }
  104. }  
  105. // change the direction the motor spins by talking
  106. // to the control pins on the H-Bridge
  107. if (motorDirection == 1) {
  108. digitalWrite(controlPin1, HIGH);
  109. digitalWrite(controlPin2, LOW);
  110. }
  111. else{
  112. digitalWrite(controlPin1, LOW);
  113. digitalWrite(controlPin2, HIGH);
  114. }  
  115. // if the motor is supposed to be on
  116. if (motorEnabled == 1) {
  117. // PWM the enable pin to vary the speed
  118. analogWrite(enablePin, motorSpeed);
  119. }else {
  120. //if the motor is not supposed to be on
  121. //turn the motor off
  122. analogWrite(enablePin, 0);
  123. }
  124. // save the current On/Offswitch state as the previous
  125. previousDirectionSwitchState = directionSwitchState;
  126. // save the current switch state as the previous
  127. previousOnOffSwitchState = onOffSwitchState;
  128. }
  129. lcd.noBlink();
  130. delay(3000);
  131. lcd.blink();
  132. delay(3000);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top