Ir ao conteúdo
  • Cadastre-se

xandeco1603

Membro Júnior
  • Posts

    1
  • Cadastrado em

  • Última visita

posts postados por xandeco1603

  1. 
    #include <Wire.h>
    #include "SSD1306Ascii.h"
    #include "SSD1306AsciiWire.h"
    #define I2C_ADDRESS 0x3C
    #define RST_PIN -1
    SSD1306AsciiWire oled;
    #include "U8glib.h"
    U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);	// I2C / TWI
    const uint8_t rook_bitmap[] PROGMEM = {
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x0C, 0x00, 0x0E, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1E, 0x00, 0x0F, 0x00, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0x80, 0x0F, 0xE0, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xC0, 0x0F, 0xE0, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xE0, 0x0F, 0xF0, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xF8, 0x0F, 0xFC, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFC, 0x0F, 0xFE, 0x07, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0x0F, 0xFF, 0x87, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0x8F, 0xFF, 0xC7, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xCF, 0xFF, 0xE7, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xEF, 0xFF, 0xE7, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };
    #include <Servo.h>
    Servo myservo;
    #define  EN   6       
    #define  M1   4      
    #define  M2   7      
    #define  OUTA   12       
    #define  OUTB    10  
    #define  SW    11
    
    int state = 0; 
    int statea = 0; 
    int Astate = 0;
    int Alaststate = 0;
    int count = 0;
    int PWM=200;
    int startpos =120;
    int minpos=140;
    int maxpos =178;
    int pos;
    int pause = 20;
    int k;
    int encoder_pin = 2; // pulse output from the module
    
    int pulses;
    int x ;
    
    
    void draw(void) {
      // graphic commands to redraw the complete screen should be placed here 
      
      u8g.drawBitmapP( 10, 0, 16, 64, rook_bitmap);
     
    }
    
    
    
    void counter()
    {
       //Update count
       pulses++;
    }
       
    void setup() {
      Wire.begin();
      Wire.setClock(400000L);
      //Serial.begin(9600);
    #if RST_PIN >= 0
      oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
    #else // RST_PIN >= 0
      oled.begin(&Adafruit128x64, I2C_ADDRESS);
    #endif // RST_PIN >= 0
    
      oled.setFont(Adafruit5x7);
      oled.clear();
      pinMode(SW, INPUT_PULLUP);
      pinMode(OUTA, INPUT);
      pinMode(OUTB, INPUT);
      pinMode(EN, OUTPUT);
      pinMode(M1, OUTPUT);
      pinMode(M2, OUTPUT);
     Alaststate = digitalRead(OUTA); 
     myservo.attach(9);
     myservo.write(minpos);
    Serial.begin(9600);
    
    pinMode(encoder_pin, INPUT);
       //Interrupt 0 is digital pin 2
       //Triggers on Falling Edge (change from HIGH to LOW)
       attachInterrupt(0, counter, CHANGE);
       // Initialize
       pulses = 0;
    }
    
      
    void loop() {
      
    
         
        
     
      if (state == 0) {
       u8g.firstPage();  
      do {
        draw();
      } while( u8g.nextPage() );
    
      }
      
    
    
    
    
    if (!digitalRead(SW) && state == 0) {
       oled.clear();
        staticmenu();
        mainmenu();
        mot_stop();
        state = 1;
      }
       
    if (state == 1){   
       Astate = digitalRead(OUTA);
      
       
      if (Astate != Alaststate){     
         // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
         if (digitalRead(OUTB) != Astate) { 
           count ++;
         } else {
           count --;
         }
         Serial.print("Position: ");
         Serial.println(count);
       } 
       Alaststate = Astate; // Updates the previous state of the outputA with the current state
     
     if ( count ==0){
       cursorup();
       statea = 1;
     }
     
     if ( count <0){
       cursordown();
       statea =2;
       
     }
     
     if ( count >0){
       cursordown();
       statea =2;
     }
     
    
    
    if (count <-2 || count >2){
      count = 0;
     cursorup();
     statea = 1;
    }
    }
    
    
    
    
    
    if (statea ==1 && !digitalRead(SW)){
      
    oled.clear();
    state = 2;
        staticmenu();
        automenu();
    
    }
    
    if (state == 2){   
       Astate = digitalRead(OUTA);
      
       
      if (Astate != Alaststate){     
         // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
         if (digitalRead(OUTB) != Astate) { 
           count ++;
         } else {
           count --;
         }
         Serial.print("Position: ");
         Serial.println(count);
         oled.set2X();
      oled.setCursor(10, 6);
      oled.println(abs(count));
      oled.setCursor(50, 6);
      oled.println("mtr");
       } 
       Alaststate = Astate; // Updates the previous state of the outputA with the current state
    statea =5;
    }
    
    if (state ==2 && !digitalRead(SW)){
      oled.clear();
      mot_run();
      start();
      state = 4;
    }
    
    if (state == 4){ 
      
      
      for(pos = minpos; pos <= maxpos; pos += 1) 
      {    Serial.println(pulses);                                 
        myservo.write(pos); 
         pulse();
        if (k >= abs(count)){
           state = 10;
          done();
    //pulses = 0;        // bail out on sensor detect
           break;
        
        }    // waits 15ms for the servo to reach the position 
        delay(pause); 
      } 
      for(pos = maxpos; pos>=minpos; pos-=1)     // goes from 180 degrees to 0 degrees 
      {      Serial.println(pulses);                            
        myservo.write(pos); 
        pulse();
          // tell servo to go to position in variable 'pos' 
        if (k >= abs(count)){ 
    state = 10; 
    mot_stop();
    done();
    //pulses = 0; // bail out on sensor detect
           break;
        
        }                       // waits 15ms for the servo to reach the position
       delay(pause); 
      } 
      
      
      
    }
    
    
    
    
    
    
    
    if (statea == 2 && !digitalRead(SW)){
      oled.clear();
        staticmenu();
        manualmenu();
        mot_stop();
    statea = 3;
    state = 3;
    }
    
     
    if ( statea == 3 && !digitalRead(SW)){
      oled.clear();
     
      mot_run();
      start();
      
      statea = 4;
      
    }
    
    
    if (statea ==4){
      
      for(pos = minpos; pos <= maxpos; pos += 1) // goes from 0 degrees to 180 degrees 
      {    Serial.println(pulses);                                // in steps of 1 degree 
        myservo.write(pos); 
         pulse();
        if (!digitalRead(SW)){
            statea = 2;  
    pulses = 0;        // bail out on sensor detect
           break;
        
        }    // waits 15ms for the servo to reach the position 
        delay(pause); 
      } 
      for(pos = maxpos; pos>=minpos; pos-=1)     // goes from 180 degrees to 0 degrees 
      {      Serial.println(pulses);                            
        myservo.write(pos); 
        pulse();
          // tell servo to go to position in variable 'pos' 
        if (!digitalRead(SW)){ 
     statea = 2; 
    pulses = 0; // bail out on sensor detect
           break;
        
        }                       // waits 15ms for the servo to reach the position
       delay(pause); 
      } 
      
    }
    
    
     attachInterrupt(0, counter, FALLING); 
    
    }
    
     void staticmenu() {
      oled.set2X();
      oled.setCursor(0, 0);
      oled.println("BOBINADEIRA");
      oled.set1X();
      oled.setCursor(0, 13);
      oled.println("---------------------");
      //oled.display();
    
    }
     
    void mainmenu() {
      oled.set2X();
      oled.setCursor(35, 25);
      oled.println("AUTO  ");
      oled.setCursor(35, 55);
      oled.println("MANUAL");
      
    
    } 
    
    void settingmenu() {
      oled.set2X();
      oled.setCursor(35, 25);
      oled.println("SETTING");
      oled.setCursor(35, 55);
      oled.println(count);
      
      
    
    } 
     
     
     void cursorup() {
      oled.set2X();
      oled.setCursor(10, 3);
      oled.println(">");
      oled.setCursor(10, 5);
      oled.println(" ");
    }
    
    void cursordown() {
      oled.set2X();
      oled.setCursor(10, 3);
      oled.println(" ");
      oled.setCursor(10, 5);
      oled.println(">");
    }
     
     
    void manualmenu() {
      oled.set2X();
      oled.setCursor(25, 65);
      oled.println("PRESS");
      oled.setCursor(10, 250);
      oled.println("TO START");
    }
    
    
    
    void automenu() {
      oled.set2X();
      oled.setCursor(5, 65);
      oled.println("SET LENGTH");
     
    }
    
    void set_length(){
      oled.set2X();
     
      oled.setCursor(10, 250);
      oled.println(count);
    }
    
    
    void start() {
      oled.set2X();
      oled.setCursor(5, 65);
      oled.println("RUNNING.. ");
    }
    void done() {
     oled.clear();
       oled.set2X();
      oled.setCursor(0, 0);
      oled.println("...DONE...");
      oled.setCursor(10, 65);
      oled.println("       ");  
       oled.setCursor(10, 80);
      oled.println(abs(count));
       oled.setCursor(50, 4);
      oled.println("mtr");
      
      
    }
    void pulse() {
      k = pulses/450;
      oled.set2X();
      oled.setCursor(10, 5);
      oled.println(k);
      oled.setCursor(60, 5);
      oled.println("mtr");
      
      
    }
    
    
    
    
    
    void mot_run(){
      analogWrite(EN, PWM);
      digitalWrite(M1,HIGH);
      digitalWrite(M2,LOW); 
    }
    
    void servo(){
      for(pos = minpos; pos <= maxpos; pos += 1) // goes from 0 degrees to 180 degrees 
      {                                  // in steps of 1 degree 
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(15);     // waits 15ms for the servo to reach the position 
      } 
      for(pos = maxpos; pos>=minpos; pos-=1)     // goes from 180 degrees to 0 degrees 
      {                                
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(15);                       // waits 15ms for the servo to reach the position 
      } 
    
      } 
    
    
    void mot_stop(){
      analogWrite(EN, 0);
      digitalWrite(M1,LOW);
      digitalWrite(M2,LOW);
      
    }
    
    
    
    
     

    Alguém poderia me auxiliar com o código abaixo: Quero usa-lo com modulo L293d, duvida onde ligar
    Display Lcd Oled Modulo Para Arduino 0.96 I2c Iic Spi e o Encoder Decoder KY-040 Rotacional.

    Outra duvida, muda alguma coisa no código

    Em anexo circuito e o código.

     

     


     

    CIRCUIT.png

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!