Advertisement
tomateblue

DaniRobotIno

Sep 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. int s2 = A0;
  2. int s3 = A2;
  3. int s4 = A3;
  4.  
  5. int S2 = 0;
  6. int S4 = 0;
  7. int CENTRO = 0;
  8.  
  9. int IN1 = 4;
  10. int IN2 = 5;
  11. int IN3 = 6;
  12. int IN4 = 7;
  13.  
  14. int velocidadeA = 10;
  15. int velocidadeB = 11;
  16.  
  17. void velocidadeMotorA(int valor){
  18.     analogWrite(velocidadeA,valor);
  19.  
  20. }
  21. void velocidadeMotorB(int valor){
  22.     analogWrite(velocidadeB,valor);  
  23. }
  24.  
  25. void motor_andar(int valor1, int valor2){
  26.     velocidadeMotorA(valor1);
  27.     velocidadeMotorB(valor2);
  28.     digitalWrite(IN1, HIGH);
  29.     digitalWrite(IN2, LOW);
  30.     digitalWrite(IN3, HIGH);
  31.     digitalWrite(IN4, LOW);
  32.     delay(100);
  33.  
  34. }
  35.  
  36. void motor_esquerda(int valor){
  37.     velocidadeMotorA(valor);
  38.     digitalWrite(IN1, HIGH);
  39.     digitalWrite(IN2, LOW);
  40.     digitalWrite(IN3, HIGH);
  41.     digitalWrite(IN4, HIGH);
  42.     delay(100);
  43. }
  44.  
  45. void motor_direita(int valor){
  46.     velocidadeMotorB(valor);
  47.     digitalWrite(IN3, HIGH);
  48.     digitalWrite(IN4, LOW);
  49.     digitalWrite(IN1, HIGH);
  50.     digitalWrite(IN2, HIGH);
  51.     delay(100);
  52. }
  53.  
  54. void stop_motor(){
  55.    
  56.     digitalWrite(IN3, HIGH);
  57.     digitalWrite(IN4, HIGH);
  58.     digitalWrite(IN1, HIGH);
  59.     digitalWrite(IN2, HIGH);
  60.    
  61. }
  62.  
  63.  
  64. void setup () {
  65.  pinMode(s2, INPUT);
  66.  pinMode(s3, INPUT);
  67.  pinMode(s4, INPUT);
  68.  Serial.begin(9600);
  69.  pinMode(13,OUTPUT);
  70.  
  71.   pinMode(IN1, OUTPUT);
  72.   pinMode(IN2, OUTPUT);
  73.   pinMode(IN3, OUTPUT);
  74.   pinMode(IN4, OUTPUT);
  75.   pinMode(velocidadeA,OUTPUT);
  76.   pinMode(velocidadeB,OUTPUT);
  77.  
  78. }
  79. void loop () {
  80.   digitalWrite(13,HIGH);
  81.   S2 = digitalRead(s2);
  82.   Serial.print("S2: "); // <==
  83.   Serial.println(S2);
  84.  
  85.   CENTRO = digitalRead(s3);
  86.   Serial.print("S3: ");
  87.   Serial.println(CENTRO);
  88.  
  89.   S4 = digitalRead(s4);
  90.   Serial.print("S4: "); // <==
  91.   Serial.println(S4);
  92.   Serial.println("\n###########################################\n");
  93.  
  94.  
  95.  
  96.   if ( S2 == 0  && CENTRO == 0 && S4 == 1 ){
  97.     Serial.println("Motor Direita\n");
  98.     motor_direita(100);
  99.   }
  100.   if ( S2 == 1  &&  CENTRO == 1  && S4 == 0){
  101.     Serial.println("Motor Esquerda\n");
  102.     motor_esquerda(100);
  103.    
  104.   }
  105.  
  106.   if ( S2 == 1  &&  CENTRO == 0  && S4 == 1){
  107.     motor_andar(100,100);
  108.     Serial.println("RUN!!\n");
  109.   }
  110.   if ( S2 == 1  &&  CENTRO == 1  && S4 == 1){
  111.     stop_motor();
  112.     Serial.println("STOP MOTOR\n");
  113.   }
  114.    
  115.  
  116. delay(1000);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement