Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. #include <Charliplexing.h>
  2. #include <Figure.h>
  3. #include <Font.h>
  4.  
  5. int motorpin=30;
  6. int directiono=31;
  7.  
  8. int y=random(0,8);//ensures the first light turned on is actually on the shield
  9. int x=random(0,13);
  10. int timedelay=20;
  11. int xcounterpos=1;
  12. int xcounterneg=0;//initialise all variables
  13. int ycounterpos=1;
  14. int ycounterneg=0;
  15. void setup(){
  16. pinMode(directiono,OUTPUT);
  17. pinMode(motorpin,OUTPUT);
  18.  
  19. LedSign::Init(); //initializes screen
  20. LedSign::Clear();
  21. //x is rows ie going along with the ciseco writing
  22. //rows and columns start off at 0!!
  23. }
  24. void loop(){
  25.  
  26. smile();
  27. numberone();
  28. numberseven();
  29. for(int i=0;i<1500;i+=20){
  30. forward(20000);
  31. }
  32. //moves the motor forward and back
  33. for(int i=0;i<1300;i+=20){//different threshild here due to the
  34. back(20000); //fact that we didn't wat the motor against the back wall
  35.  
  36. }
  37. while (true){// this ensures the group number is only printed once
  38. ball();
  39. }
  40.  
  41. }
  42.  
  43. void ball(){
  44. x=xmovement(x);
  45. y=ymovement(y);
  46. LedSign::Set(x,y,7);
  47. delay(timedelay);//xcounterpos of one ball is effecting the o
  48.  
  49. //delay(timedelay);//calculates new position of each coordiinate and lights up new light
  50. //then after a delay turns off the light before turning on the next one
  51. }
  52.  
  53. int ymovement(int movey){
  54. int posit=movey;
  55.  
  56. if(posit==0){
  57. motordrive();
  58. posit++;
  59. //if it bounces off the left wall it sends it back right and adjusts counters
  60. ycounterpos=1;// counters are used here to see which wall the ball last hit
  61. ycounterneg=0;
  62. }
  63. else if(posit==8 ){
  64. motordrive();
  65. posit--;
  66. //ball has hit off left wall and goes back
  67. ycounterpos=0;
  68. ycounterneg=1;
  69. }
  70. else if(ycounterpos==1){// checks which vertical wall was last hit and decieds which way to send it
  71.  
  72. posit++;
  73. LedSign::Clear();// turns off the light after moving
  74. //this solved one of the issues we encountered
  75. //it stopped the light 'disappearing' when the floppy drive
  76. //funxtion was callled and just left the light on until it moved away from the wall
  77. }
  78.  
  79. else if(ycounterneg==1){
  80.  
  81. posit--;
  82. LedSign::Clear();
  83. }
  84. return posit;// returns y position
  85. }
  86.  
  87.  
  88.  
  89.  
  90. int xmovement(int movem){//does the same as the y movement except for the x direction
  91. int posit=movem;
  92.  
  93. if(posit==0){
  94. posit++;
  95. motordrive();
  96. xcounterpos=1;
  97. xcounterneg=0;
  98. }
  99. else if(posit==13 ){
  100. posit--;
  101. motordrive();
  102. xcounterpos=0;
  103. xcounterneg=1;
  104. }
  105. else if(xcounterpos==1){
  106. posit++;
  107. }
  108.  
  109. else if(xcounterneg==1){
  110. posit--;
  111. }
  112. return posit;
  113. }
  114.  
  115.  
  116.  
  117. void forward(int p){
  118. p-=1000;
  119. digitalWrite(motorpin,LOW);
  120. digitalWrite(directiono,LOW);
  121. //you must set the direction pin before the motor is turned on
  122. //delays were used because the motor could not handle the speed at which the direction changed
  123. delayMicrosecond(p);
  124. digitalWrite(motorpin,HIGH);
  125. delay(1);
  126. digitalWrite(motorpin,LOW);
  127. }
  128.  
  129. void back(int p){
  130. p-=1000;
  131. digitalWrite(motorpin,LOW);
  132. digitalWrite(directiono,HIGH);//you must set the direction pin before the motor is turned on
  133. //delays were used because the motor could not handle the speed at which the direction changed
  134. delayMicrosecond(p);
  135. digitalWrite(motorpin,HIGH);
  136. delay(1);
  137. digitalWrite(motorpin,LOW);
  138. }
  139.  
  140.  
  141.  
  142. void motordrive(){
  143. int p=pitch(x);
  144. //the pitch function calculates the delay needed to make the required tone.
  145. //250000 microseconds for 1/4sec, the buzz should sound for this long. 2p is the amount of time it take to go forward AND back.
  146. //So 250000/2p gives us the amount of cycles to run for to buzz for 1/4 of a sec. (rounded down to the nearest whole number, since it's an int)
  147. for(int i=0; i<250000/(2*p);i++){
  148. forward(p);
  149. back(p);
  150. }
  151. }
  152.  
  153. void pitch(int x){
  154. int q=x-1;
  155. double pp = 1000000/220; //base note of 110Hz
  156. pp = pp*(pow(2,q/12); //with a q ranging from 0 to 12, we get a range of 110Hz to 55Hz
  157. int p = pp;
  158. return p;
  159. }
  160.  
  161. void smile(){
  162. //turns on the lights for a smile one by one
  163. //looks better than all of them being turned on at once hence the delay
  164. LedSign::Set(9,5,7);
  165. delay(100);
  166. LedSign::Set(12,5,7);
  167. delay(100);
  168. LedSign::Set(9,3,7);
  169. delay(100);
  170. LedSign::Set(12,3,7);
  171. delay(100);
  172. LedSign::Set(11,2,7);
  173. delay(100);
  174. LedSign::Set(10,2,7);
  175. delay(100);
  176.  
  177. }
  178.  
  179. void numberone(){
  180. //turns on the lights for the number one, one by one
  181.  
  182. LedSign::Set(6,6,7);
  183. delay(100);
  184. LedSign::Set(6,5,7);
  185. delay(100);
  186. LedSign::Set(6,4,7);
  187. delay(100);
  188. LedSign::Set(6,3,7);
  189. delay(100);
  190. LedSign::Set(6,2,7);
  191. delay(100);
  192. LedSign::Set(6,1,7);
  193. delay(100);
  194. }
  195.  
  196. void numberseven(){
  197. //turns on the lights for the number one, one by one
  198. LedSign::Set(4,6,7);
  199. delay(100);
  200. LedSign::Set(3,6,7);
  201. delay(100);
  202. LedSign::Set(2,6,7);
  203. delay(100);
  204. LedSign::Set(2,5,7);
  205. delay(100);
  206. LedSign::Set(2,4,7);
  207. delay(100);
  208. LedSign::Set(2,3,7);
  209. delay(100);
  210. LedSign::Set(2,2,7);
  211. delay(100);
  212. LedSign::Set(2,1,7);
  213. delay(100);
  214. LedSign::Set(1,4,7);
  215. delay(100);
  216. LedSign::Set(3,4,7);
  217. delay(100);
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement