跳到主要內容

Arduino 滑鼠滾輪編碼器/旋轉編碼器

滑鼠拆下來的滾輪編碼器

時序為
                     → CW
A  -----------          -------------    HIGH
               ----------                  LOW

                     ← CCW
B  --------          ----------------    HIGH
            ----------                     LOW

A B

1  1↓CW
1  0
0  0
0  1

1  1
1  0
0  0
0  1↑CCW

程式碼參考底下網址來源修改而來


#define ENCODER_A_PIN 2
#define ENCODER_B_PIN 3
 
unsigned long time = 0;
long position = 0;
long num = 0;
 
void setup()
{
  pinMode(ENCODER_A_PIN,INPUT);
  pinMode(ENCODER_B_PIN,INPUT);
 
  attachInterrupt(0, RotaryEncoder, LOW);
  Serial.begin(9600);
  time = millis();
}
 
void loop()
{
  while (num != position)
  {
    num = position;
    Serial.println(num);
  }
}
 
 
void RotaryEncoder(){  
  int temp =digitalRead(ENCODER_B_PIN);
  if ((millis() - time) > 3) {
      if ( temp == LOW)
      {
        position++;
      }else{
      if ( temp == HIGH)
        position--;
      }
  }
  time = millis();
}

沒中斷版本

int trigger=0;
void RotaryEncoder(){  
  int tempA =digitalRead(ENCODER_A_PIN);
  int tempB =digitalRead(ENCODER_B_PIN);

  
  if ((millis() - time) > 3) {
    if(tempA==LOW && trigger==HIGH){
      if ( tempB == LOW)
      {
        position++;
      }else{
      if ( tempB == HIGH)
        position--;
      }
    }
    trigger=tempA;
    time = millis();
  }
}

參考來源:
http://www.arduino.cn/thread-2423-1-1.html
http://www.geek-workshop.com/thread-373-1-1.html


2015/05/24 更新



恩..多一顆按鈕
CLK 當作A
DT 當B

    #define ENCODER_A_PIN 2
    #define ENCODER_B_PIN 3
    #define SW_PIN 4
    
    unsigned long time = 0;
    volatile long position = 0;
    volatile long num = 0;
     
    void setup()
    {
      pinMode(ENCODER_A_PIN,INPUT_PULLUP);
      pinMode(ENCODER_B_PIN,INPUT_PULLUP);
      pinMode(SW_PIN, INPUT_PULLUP); // 輸入模式並啟用內建上拉電阻
      
      attachInterrupt(0, RotaryEncoder, LOW);
      Serial.begin(9600);
      time = millis();
    }
     
    void loop()
    {
       if(digitalRead(SW_PIN) == LOW){ // 按下開關,歸零
         position = 0; 
         Serial.println("count reset to 0");
         delay(300);
      }
      while (num != position)
      {
        num = position;
        Serial.println(num);
      }
    }
     
     
    void RotaryEncoder(){  
      int temp =digitalRead(ENCODER_B_PIN);
      if ((millis() - time) > 3) {
          if ( temp == LOW)
          {
            position++;
          }else{
          if ( temp == HIGH)
            position--;
          }
      }
      time = millis();
    }

留言

這個網誌中的熱門文章

小蟻智慧攝影機支援RTSP

來源: http://en.miui.com/thread-196713-1-1.html 小蟻智慧攝影機版本: 1.8.5.1l_201511061661(台灣版) 下載檔案 https://drive.google.com/file/d/0ByjBix7wpeJJY0ZWZXlvWl9Za1E/view?usp=sharing 把攝影機SD卡拿讀卡機讀取 把壓縮檔內的test資料夾放進SD根目錄 插回SD卡至攝影機 重開 攝影機會呈現橘燈並執行更新 等恢復至藍燈或是用app確認攝影機開啟 用VLC或相關程式讀取串流 Main stream:rtsp:// <CameraIP> :554/ch0_0.h264 Minor stream:rtsp:// <CameraIP> :554/ch0_1.h264 Audio stream:rtsp:// <CameraIP> :554/ch0_3.h264 Telnet與FTP開啟加上 # Telnet if [ ! -f "/etc/init.d/S88telnet" ]; then     echo "#!/bin/sh" > /etc/init.d/S88telnet     echo "telnetd &" >> /etc/init.d/S88telnet     chmod 755 /etc/init.d/S88telnet fi # FTP echo "#!/bin/sh" > /etc/init.d/S89ftp echo "tcpsvd -vE 0.0.0.0 21 ftpd -w / &" >> /etc/init.d/S89ftp chmod 755 /etc/init.d/S89ftp User:  root Password:  1234qwer 使用passwd改密碼 參考: http://en.miui.com/thread-224653-1-1.html

Ollama AMD GPU 支援

AMD Radeon RX 6300(gfx1034)為例 本次為官方不支援 LLVM Target 參考 ollama-for-amd 安裝ROCmLibs for HIP SDK 6.1.2 AMD HIP SDK for Windows 官方支援GPU 下載ollama https://github.com/likelovewant/ollama-for-amd/releases 執行Ollama 於資料夾執行ollama: ollama serve AMD Radeon RX 6300 為gfx1034 LLVM Target 回應不支援GPU: msg="amdgpu is not supported (supported types:[gfx1030 gfx1100 gfx1101 gfx1102 gfx906])" gpu_type=gfx1034 gpu=0 library="C:\\Program Files\\AMD\\ROCm\\6.1\\bin" 下載gfx1034 ROCm https://github.com/likelovewant/ROCmLibs-for-gfx1103-AMD780M-APU/releases/tag/v0.6.1.2 將rocblas.dll及library 備份後刪除 C:\Program Files\AMD\ROCm\6.1\bin\rocblas.dll C:\Program Files\AMD\ROCm\6.1\bin\rocblas\library 將下載後的gfx1034 ROCm解壓後放置於 C:\Program Files\AMD\ROCm\6.1\bin\rocblas.dll C:\Program Files\AMD\ROCm\6.1\bin\rocblas\library 重新執行 執行ollama: ollama serve 本次已找到適應版本 msg="inference compute" id=0 library=rocm variant="" compute=gfx1034 driver=6.1 name="AMD R