Advertisement
tiodocomputador

Motor de Passo, Joystick e Processing

Jun 26th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.47 KB | None | 0 0
  1. /*Codigo para girar um motor de passo Mitsumi de 5 fios
  2.  usando joystick ou potenciometro
  3.  Desenvolvido por https://cyberohm.com
  4.  Apoio: labdegaragem.com
  5.  Copie, modifique, compartilhe!
  6.  */
  7.  
  8. import processing.serial.*; //importa a biblioteca serial do processing
  9. import cc.arduino.*; // importa a biblioteca do arduino para o processing
  10. import processing.net.*;
  11. Arduino arduino; //define o objeto (?) Arduino
  12.  
  13. color on = color(84, 145, 158); //define a cor quando background está em ON
  14. color off = color(255); //define a cor quando background está em OFF
  15. int a=1200; // largura da tela
  16. int b=600; // altura da tela
  17.  
  18. int [][] matriz=
  19. {
  20.   {
  21.     0, 0, 0, 1
  22.   }
  23.   ,
  24.   {
  25.     0, 0, 1, 1
  26.   }
  27.   ,
  28.   {
  29.     0, 0, 1, 0
  30.   }
  31.   ,
  32.   {
  33.     0, 1, 1, 0
  34.   }
  35.   ,
  36.   {
  37.     0, 1, 0, 0
  38.   }
  39.   ,
  40.   {
  41.     1, 1, 0, 0
  42.   }
  43.   ,
  44.   {
  45.     1, 0, 0, 0
  46.   }
  47.   ,
  48.   {
  49.     1, 0, 0, 1
  50.   }
  51. };
  52.  
  53. int joystick = 1;  // Analog input pin that the potentiometer is attached to
  54. int verde = 6;
  55. int amarelo = 7;
  56. int vermelho = 8;
  57. int button = 9;
  58. int t=5;
  59. int x;
  60. int y;
  61. int count;
  62. int motorState;
  63.  
  64. int diametro = a/6;
  65.  
  66. //******************************************** SETUP() ********************************************
  67. void setup()
  68. {
  69.   size(a, b);
  70.   //As duas próximas linhas deram trabalho, mas a vitória pertence aos bravos!
  71.   //Louvado seja ROOT!
  72.   //
  73.   //You can't hack if you don't Slack!
  74.   arduino = new Arduino(this, "/dev/ttyUSB0", 57600);
  75.   arduino.pinMode(joystick, Arduino.INPUT);
  76.   arduino.pinMode(button, Arduino.INPUT);
  77.   for (int pino=2; pino<9; pino++)
  78.   {
  79.     arduino.pinMode(pino, Arduino.OUTPUT);
  80.   }
  81.  
  82.   arduino.pinMode(button, Arduino.INPUT);
  83. }
  84.  
  85.  
  86. //******************************************** DRAW() ********************************************
  87.  
  88. void draw()
  89. {
  90.   float sensor = map(arduino.analogRead(joystick), 0, 1023, 0, 100);
  91.   int angle = int(36*sensor/10);
  92.   int begin = arduino.digitalRead(button);
  93.  
  94.  
  95.   background(off); //nem precisava comentar.
  96.   stroke(on); // linha de contorno
  97.   stroke(255); // linha de contorno branca.
  98.   strokeWeight(2); // expessura de 3 pixels.
  99.   strokeCap(SQUARE); // quinas quadradas.
  100.   fill(on); //cor de preencimento.
  101.  
  102.   //LOGOTIPO
  103.  
  104.   PFont font;
  105.   font = loadFont("Borg9-48.vlw");
  106.   PFont Arial;
  107.   Arial = loadFont("Arial.vlw");
  108.   textFont(font, 90);
  109.   textAlign(CENTER);
  110.   fill(0);
  111.   text("C Y B E R O H M", a/2+13, b/4+3);
  112.   fill(255, 0, 0);
  113.   text("C Y B E R O H M", a/2+10, b/4);
  114.   textFont(Arial, 28);
  115.   textAlign(RIGHT);
  116.   text("R o b o t i c s", 1070, b/4+50);
  117.   textFont(Arial, 42);
  118.   textAlign(CENTER);
  119.  
  120.   /*
  121.   PImage img;
  122.    // Image must be in the sketch's "data" folder
  123.    img = loadImage("cyberohm.png");
  124.    image(img, 280, b/16, 800,160);
  125.    */
  126.  
  127.   fill(255);
  128.   stroke(0);
  129.  
  130.   translate(a/2, b/2+100);
  131.   stroke(255, 0, 0);
  132.   strokeWeight(5);
  133.   ellipse(0, 0, diametro, diametro);
  134.   stroke(0);
  135.   strokeWeight(2);
  136.   line(-3*diametro/5, 0, 3*diametro/5, 0);
  137.   line(0, -3*diametro/5, 0, 3*diametro/5);
  138.   /*
  139.   if (begin==1)
  140.    {
  141.    x++;
  142.    if (x>7)
  143.    {
  144.    x=0;
  145.    }
  146.    for (y=0; y<4; y++)
  147.    {
  148.    arduino.digitalWrite((y+2), matriz[x][y]);
  149.    arduino.digitalWrite(vermelho, 1);
  150.    }
  151.    delay(t);
  152.    arduino.digitalWrite(vermelho, 0);
  153.    }
  154.    */
  155.   sensor = map(arduino.analogRead(joystick), 0, 1023, 1, 92);
  156.   if ((motorState==0)&&(begin==0))
  157.   {
  158.     x++;
  159.     if (x>7)
  160.     {
  161.       x=0;
  162.     }
  163.     for (y=0; y<4; y++)
  164.     {
  165.       if (begin==0)
  166.         arduino.digitalWrite((y+2), matriz[x][y]);
  167.           }
  168.   }
  169.  if ((motorState==0)&&(begin==1))
  170.       {
  171.         motorState=1;
  172.         count=92;
  173.       }
  174.  
  175.   if ((count<=sensor)&&(sensor-count>2)&&(motorState==1))
  176.   {
  177.     x++;
  178.     if (x>7)
  179.     {
  180.       x=0;
  181.     }
  182.     count++;
  183.     for (y=0; y<4; y++)
  184.     {
  185.       arduino.digitalWrite((y+2), matriz[x][y]);
  186.     }
  187.   } else if ((count>sensor)&&(count-sensor>2)&&(motorState==1))
  188.   {
  189.     x--;
  190.     if (x<0)
  191.     {
  192.       x=7;
  193.     }
  194.     count--;
  195.     for (y=0; y<4; y++)
  196.     {
  197.       arduino.digitalWrite((y+2), matriz[x][y]);
  198.     }
  199.   }
  200.  
  201.   fill(255, 0, 0);
  202.   stroke(0);
  203.   strokeWeight(5);
  204.   rect(3*diametro/2, 0, 50, -sensor);
  205.   textAlign(LEFT);
  206.   textFont(Arial, 18);
  207.   text(angle+" graus", diametro, 0);
  208.   text(count+" count", diametro, 20);
  209.   text(motorState+" motorState", diametro, 40);
  210.   text(begin+" begin", diametro, 60);
  211.   ellipse(0, 0, 20, 20);
  212.   rotate(radians(-angle));
  213.   quad(0, -20, -20, 0, 0, 20, 20+diametro/2, 0);
  214. }
  215. // \m/ ROCK N ROLL !!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement