Arduino robotprogrammering

C, C++, Pascal, Assembly, Raspberry, Java, Matlab, Python, BASIC, SQL, PHP, etc.
Användarvisningsbild
Magnus_K
EF Sponsor
Inlägg: 5854
Blev medlem: 4 januari 2010, 17:53:25
Ort: Skogen mellan Uppsala-Gävle

Re: Arduino robotprogrammering

Inlägg av Magnus_K »

Efter samtal med Xyzzy och lite diskussion om hur saker och ting fungerade så kom vi fram till att biblioteket jag använder inte har någon funktion som fungerar för mitt ändamål.
Tyvärr är jag inte kapabel att läsa koden djupt nere i bibliotek men vi kom fram till att u8g.firstPage(); gör en initialisering/reset/clear samt ytterligare något som "inte går att vara utan".

Tog nya tag och bläddrade runt bland OLED bibliotek och hittade Adafruits GFX-bibliotek som tycktes ha lite enklare filer och se på fasen. Efter en stund så fungerar det nu klockrent! :shock:
Hade kunnat skicka en tårta till dom som skrivit biblioteket...

Så här ser det ut nu. Har inte städat rent koden än men en liten resultatbild syns nedan också. Inte ett enda blink på skärmen så långt ögat kan nå :D

Kod: Markera allt

/*****************************************PROJECT INFO*************************************************
 * Name:         Gorby_brain.c
 * Processor:    ATmega328P
 * Author:       Ida & Magnus
 * Peripherals:  128x64 I2C OLED Display
 *               Ultrasonic sensor
 *               4 ea pushbuttons
 *               
 *****************************************************************************************************/

/*****************************************INCLUDES****************************************************/

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

/*****************************************************************************************************/

/******************************************DEFINES****************************************************/

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);


/*****************************************************************************************************/

/*****************************************CONSTANTS****************************************************/
const uint8_t buttonLeft = 9;
const uint8_t buttonRight = 10;
const uint8_t buttonFwd = 11;
const uint8_t buttonX = 12;

const unsigned char arrow_up [] PROGMEM = {
0x00, 0x00, 0x01, 0x00, 0x03, 0x80, 0x07, 0xC0, 0x0F, 0xE0, 0x1F, 0xF0, 0x3F, 0xF8, 0x7B, 0xBC,
0x73, 0x9C, 0x63, 0x8C, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80,
0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
};

const unsigned char arrow_left [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x07, 0x80, 0x0F, 0x00, 0x1E, 0x00, 0x3F, 0xFE,
0x7F, 0xFE, 0x3F, 0xFE, 0x1E, 0x00, 0x0F, 0x00, 0x07, 0x80, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

const unsigned char arrow_right [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x01, 0xE0, 0x00, 0xF0, 0x00, 0x78, 0x7F, 0xFC,
0x7F, 0xFE, 0x7F, 0xFC, 0x00, 0x78, 0x00, 0xF0, 0x01, 0xE0, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};
/*****************************************************************************************************/

/**************************************GLOBAL VARIABLES***********************************************/
uint32_t debounceDelay = 5;
uint32_t lastDebounceChk = 0;
uint32_t lastRefresh = 0;
uint32_t screenRefreshRate = 300;
uint8_t debounceReg_Left = 0xFF;
uint8_t debounceReg_Right = 0xFF;
uint8_t debounceReg_Fwd = 0xFF;
uint8_t debounceReg_X = 0xFF;
uint8_t buttonLeft_Pressed = 0;
uint8_t buttonRight_Pressed = 0;
uint8_t buttonFwd_Pressed = 0;
uint8_t buttonX_Pressed = 0;
uint8_t Left_Press_Done = 0;
uint8_t Right_Press_Done = 0;
uint8_t Fwd_Press_Done = 0;
uint8_t X_Press_Done = 0;
uint8_t buttonCounter = 0;
uint8_t commandCounter = 0;
uint8_t X_axis = 0;
uint8_t Y_axis = 0;
uint8_t commands[24];

enum Directions : uint8_t {NONE, LEFT, RIGHT, FWD, X};
enum Directions key;
enum StatusOfButtons : uint8_t {WAIT_FOR_KEYPRESS, WAIT_FOR_KEYRELEASE};
enum StatusOfButtons Buttonstatus;


/*****************************************************************************************************/



void screenUpdate() {

       if(X_axis>=128){
          X_axis=0;
          Y_axis+=20;
            if(Y_axis>=60){
              Y_axis=0;
            }      
        }
     
      switch(commands[commandCounter]){

        case 'F':       
        display.drawBitmap(X_axis, Y_axis, arrow_up, 16, 20, 1);
        X_axis+=16;
        break;
   
        case 'L':
        display.drawBitmap(X_axis, Y_axis, arrow_left, 16, 20, 1);
        X_axis+=16;      
        break;
 
        case 'R':
        display.drawBitmap(X_axis, Y_axis, arrow_right, 16, 20, 1);
        X_axis+=16;
        break;
 
      }
      display.display();
}

void pushButtonChk() {
  if(millis() >= (lastDebounceChk + debounceDelay)) {     
    
    lastDebounceChk = millis();
    
    debounceReg_Left <<= 1;                                         // Every time the function is called
    debounceReg_Right <<= 1;                                        // the debounce registers is being shifted
    debounceReg_Fwd <<= 1;                                          // left and then the current button status
    debounceReg_X <<= 1;                                            // is read and added
    debounceReg_Left |= digitalRead(buttonLeft);
    debounceReg_Right |= digitalRead(buttonRight);
    debounceReg_Fwd |= digitalRead(buttonFwd);
    debounceReg_X |= digitalRead(buttonX);

    if(debounceReg_Left == 0x00) {                                  // As soon as the debounce register is full
      key = LEFT;                                                   // then the system reads the button as pushed
    }
    if(debounceReg_Right == 0x00) {
      key = RIGHT;
    }
    if(debounceReg_Fwd == 0x00) {
      key = FWD;
    }
    if(debounceReg_X == 0x00) {
      key = X;
    }
    else if((debounceReg_X & debounceReg_Fwd & debounceReg_Right & debounceReg_Left) == 0xFF) {
      key = NONE;
    }
  }
}


void setup() {                                                      // Using internal pullup for buttons

  Serial.begin(9600);
  pinMode(buttonLeft, INPUT);
  pinMode(buttonRight, INPUT);
  pinMode(buttonFwd, INPUT);
  pinMode(buttonX, INPUT);
  digitalWrite(buttonLeft, HIGH);
  digitalWrite(buttonRight, HIGH);
  digitalWrite(buttonFwd, HIGH);
  digitalWrite(buttonX, HIGH);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.clearDisplay();
  
  
}

void loop() {
  
  pushButtonChk();
  //screenUpdate();
  
  
  switch(Buttonstatus) { 

    case WAIT_FOR_KEYPRESS :
    
      buttonCounter = 0;

      switch(key) {

        case LEFT :
          if(!Left_Press_Done) {
            Left_Press_Done = 1;
            commands[commandCounter] = 'L';
            screenUpdate();
            commandCounter++;
            
          }       
          Buttonstatus = WAIT_FOR_KEYRELEASE;
          break;

        case RIGHT :
          if(!Right_Press_Done) {
            Right_Press_Done = 1;
            commands[commandCounter] = 'R';
            screenUpdate();
            commandCounter++;
          }
          Buttonstatus = WAIT_FOR_KEYRELEASE;
          break;

        case FWD :
          if(!Fwd_Press_Done) {
            Fwd_Press_Done = 1;
            commands[commandCounter] = 'F';
            screenUpdate();
            commandCounter++;
          }
          Buttonstatus = WAIT_FOR_KEYRELEASE;
          break;

        case X :
          if(!X_Press_Done) {
            X_Press_Done = 1;
            if(commandCounter > 0){             
              commandCounter--;
            }
            
          }
          Buttonstatus = WAIT_FOR_KEYRELEASE;
          break;

        case NONE :
          Buttonstatus = WAIT_FOR_KEYPRESS;
          break;
        
      }

      break;
    
    case WAIT_FOR_KEYRELEASE :

      switch(key) {

          case NONE :
          Left_Press_Done = 0;
          Right_Press_Done = 0;
          Fwd_Press_Done = 0;
          X_Press_Done = 0;
          Buttonstatus = WAIT_FOR_KEYPRESS;

          break;
      }

      break;
    } 
}
20180701_224630.jpg
Du har inte behörighet att öppna de filer som bifogats till detta inlägg.
Användarvisningsbild
Xyzzy
Inlägg: 1222
Blev medlem: 30 januari 2004, 22:31:07
Ort: Uppsala, Sweden

Re: Arduino robotprogrammering

Inlägg av Xyzzy »

Wow!
*publikens jubel*
*fanfarer*
Härligt Magnus! Men... Då blir det snacka skit I morgon istället för att koda? :D
Användarvisningsbild
Magnus_K
EF Sponsor
Inlägg: 5854
Blev medlem: 4 januari 2010, 17:53:25
Ort: Skogen mellan Uppsala-Gävle

Re: Arduino robotprogrammering

Inlägg av Magnus_K »

Jag har ju alltid 32 andra projekt som stannat av pga programmering :wink:
Användarvisningsbild
Xyzzy
Inlägg: 1222
Blev medlem: 30 januari 2004, 22:31:07
Ort: Uppsala, Sweden

Re: Arduino robotprogrammering

Inlägg av Xyzzy »

Haha, näääe, förstör nu inte känslan att man kan det där med att koda :vissla:
kodar-holger
EF Sponsor
Inlägg: 916
Blev medlem: 26 maj 2014, 12:54:35
Ort: Karlskoga

Re: Arduino robotprogrammering

Inlägg av kodar-holger »

Magnus_K skrev:Ok, perfekt. Ska använda mig av sizeof()
Tänk på att sizeof ger dig storleken i bytes på arrayen. I just precis detta fall blir det lika med antalet element eftersom du använder en 8-bitarstyp. Jag skulle undvika sizeof och använda en deklaration av antal element istället precis som Glattnos föreslog.
Användarvisningsbild
Magnus_K
EF Sponsor
Inlägg: 5854
Blev medlem: 4 januari 2010, 17:53:25
Ort: Skogen mellan Uppsala-Gävle

Re: Arduino robotprogrammering

Inlägg av Magnus_K »

Ok. Nu blev det ingen direkt nytta av det men inser att jag måste använda det när jag ska radera tecken.

Det som Glattnos skriver är nog för övrigt en bra "standard" så det är lika bra att försöka introducera sånt direkt :tumupp:
Skriv svar