A place for all the arduino code I write.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HullPixelBotDance.ino 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // This example drives the stepper motors directly
  2. // It makes the robot perform the "HullPixelBot Dance"
  3. // See if you can change the moves.
  4. // www.robmiles.com/hullpixelbot
  5. ////////////////////////////////////////////////
  6. //declare variables for the motor pins
  7. int rmotorPin1 = 16; // Purple - 28BYJ48 pin 1
  8. int rmotorPin2 = 5; // Grey - 28BYJ48 pin 2
  9. int rmotorPin3 = 4; // White - 28BYJ48 pin 3
  10. int rmotorPin4 = 0; // Black - 28BYJ48 pin 4
  11. int lmotorPin1 = 2; // Yellow - 28BYJ48 pin 1
  12. int lmotorPin2 = 14; // Orange - 28BYJ48 pin 2
  13. int lmotorPin3 = 12; // Red - 28BYJ48 pin 3
  14. int lmotorPin4 = 13; // Brown - 28BYJ48 pin 4
  15. int motorSpeed = 1200; //variable to set stepper speed
  16. int count = 0; // count of steps made
  17. int countsperrev = 512; // number of steps per full revolution
  18. int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
  19. //////////////////////////////////////////////////////////////////////////////
  20. void setup() {
  21. //declare the motor pins as outputs
  22. pinMode(lmotorPin1, OUTPUT);
  23. pinMode(lmotorPin2, OUTPUT);
  24. pinMode(lmotorPin3, OUTPUT);
  25. pinMode(lmotorPin4, OUTPUT);
  26. pinMode(rmotorPin1, OUTPUT);
  27. pinMode(rmotorPin2, OUTPUT);
  28. pinMode(rmotorPin3, OUTPUT);
  29. pinMode(rmotorPin4, OUTPUT);
  30. Serial.begin(9600);
  31. }
  32. const int STOP = 0;
  33. const int FORWARD = 1;
  34. const int BACK = 2;
  35. const int LEFT = 3;
  36. const int RIGHT = 4;
  37. int moveState = FORWARD;
  38. void loop(){
  39. if(count < countsperrev ) {
  40. moveStep();
  41. }
  42. else
  43. {
  44. moveState++;
  45. if(moveState > RIGHT)
  46. moveState = FORWARD;
  47. count=0;
  48. }
  49. count++;
  50. }
  51. void moveStep()
  52. {
  53. for(int i = 0; i < 8; i++)
  54. {
  55. switch(moveState)
  56. {
  57. case STOP:
  58. return;
  59. case FORWARD:
  60. setOutputDir(i,7-i);
  61. break;
  62. case BACK:
  63. setOutputDir(7-i,i);
  64. break;
  65. case LEFT:
  66. setOutputDir(7-i,7-i);
  67. break;
  68. case RIGHT:
  69. setOutputDir(i,i);
  70. break;
  71. }
  72. delayMicroseconds(motorSpeed);
  73. }
  74. }
  75. void setOutputDir(int leftOut, int rightOut)
  76. {
  77. digitalWrite(lmotorPin1, bitRead(lookup[leftOut], 0));
  78. digitalWrite(lmotorPin2, bitRead(lookup[leftOut], 1));
  79. digitalWrite(lmotorPin3, bitRead(lookup[leftOut], 2));
  80. digitalWrite(lmotorPin4, bitRead(lookup[leftOut], 3));
  81. digitalWrite(rmotorPin1, bitRead(lookup[rightOut], 0));
  82. digitalWrite(rmotorPin2, bitRead(lookup[rightOut], 1));
  83. digitalWrite(rmotorPin3, bitRead(lookup[rightOut], 2));
  84. digitalWrite(rmotorPin4, bitRead(lookup[rightOut], 3));
  85. }