[教程]虚谷号+扩展板体验 精华

2019-8-26 01:49:41 [显示全部楼层]
4224浏览
查看: 4224|回复: 1

[教程] 虚谷号+扩展板体验

[复制链接]
虚谷教育版+扩展板体验

演示视频(附部分操作):

器材:
虚谷号教育版裸板
厚物-虚谷号扩展板

参考资料:
https://wiki.dfrobot.com.cn/index.php?title=(SKU:_DFR0593)_%E5%8E%9A%E7%89%A9%E2%80%94%E2%80%94%E8%99%9A%E8%B0%B7%E5%8F%B7%E6%89%A9%E5%B1%95%E6%9D%BF,虚谷扩展版wiki

扩展板引脚图:
虚谷号+扩展板体验图1

虚谷号+扩展板体验图2

准备:
将厚物扩展板插到虚谷号上,虚谷号通电(推荐5V@2A USB供电),启动虚谷号U盘模式
虚谷号+扩展板体验图3

一、U盘方式运行

1. 点亮OLED屏
  • 下载U8glib库文件

下载地址:https://bintray.com/olikraus/u8g ... arduino_v1.18.1.zip
  • 将库文件复制到虚谷号U盘Arduino->02.library目录下

虚谷号+扩展板体验图5
  • 修改程序

在虚谷号U盘Arduino->02.lib->U8glib->examples->U8gLogo目录下,以Arduino IDE方式打开文件U8gLogo.ino
虚谷号+扩展板体验图6


查找 //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); 取消注释
虚谷号+扩展板体验图4


  • 修改配置

将U8Logo.ino文件复制到虚谷号U盘Arduino下,并修改arduino_config.ini的配置为Arduino=U8Logo.ino:
虚谷号+扩展板体验图7

  • 运行程序

短按虚谷号的reset键,刷新vvboard,当U盘标记重新出现在电脑后,可以看到OLED显示屏显示如下:
虚谷号+扩展板体验图8

其他演示:
显示HelloWorld
虚谷号+扩展板体验图13
旋转Hello
虚谷号+扩展板体验图14

2. RGB全彩LED灯
  • 库文件安装

厚物扩展板采用WS2812 RGB全彩LED灯珠,内部硬件连接到Arduino D9管脚,可通过Adafruit Neopixel灯带库来驱动。库文件已经预先保存到了02.lib目录下了。
虚谷号+扩展板体验图9


  • 编写程序

将以下代码复制并在IDE中新建文件粘贴,文件另存为strip.ino,路径为虚谷号U盘Arduino
[mw_shl_code=cpp,true]#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 9        //注意修改LED驱动管脚!!!

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code


  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Some example procedures showing how to display to the pixels:
  //colorWipe(strip.Color(255, 0, 0), 50); // Red
// colorWipe(strip.Color(0, 255, 0), 50); // Green
  //colorWipe(strip.Color(0, 0, 255), 50); // Blue
//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
  // Send a theater pixel chase in...
// theaterChase(strip.Color(127, 127, 127), 50); // White
  //theaterChase(strip.Color(127, 0, 0), 50); // Red
  //theaterChase(strip.Color(0, 0, 127), 50); // Blue

  rainbow(20);
  //rainbowCycle(20);
  //theaterChaseRainbow(50);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}[/mw_shl_code]
  • 修改配置文件

将arduino_config.ini的配置修改为Arduino=strip.ino

  • 运行程序

短按虚谷号的reset键,刷新vvboard,当U盘标记重新出现在电脑后,可以看到扩展板的LED灯带颜色变化:
虚谷号+扩展板体验图10
如果需要有其他的效果,可以将代码中的相关函数下的注释部分取消。

二、主机方式运行

3. 无源蜂鸣器
主机方式下运行桌面的Arduino IDE,新建一个文件,将下面的代码复制粘贴,将代码上传到虚谷号
虚谷号+扩展板体验图11

厚物扩展板采用无源蜂鸣器,内部硬件连接到Arduino D8管脚,可用于播放音乐,以下是儿歌“两只老虎”的音乐(网友分享)
程序代码:
[mw_shl_code=cpp,true]#define C_0  -1
#define C_1  262
#define C_2  294
#define C_3  330
#define C_4  350
#define C_5  393
#define C_6  441
#define C_7  495

//音符数组
int yinfu[]=
{
  C_1,C_2,C_3,C_1,
  C_1,C_2,C_3,C_1,
  C_3,C_4,C_5,
  C_3,C_4,C_5,
  C_5,C_6,C_5,C_4,C_3,C_1,
  C_5,C_6,C_5,C_4,C_3,C_1,
  C_1,C_5,C_1,
  C_1,C_5,C_1,
};
//音拍数组,每一行代表4拍
float yinpai[]=
{
  1,1,1,1,
  1,1,1,1,
  1,1,2,
  1,1,2,
  0.75,0.25,0.75,0.25,1,1,
  0.75,0.25,0.75,0.25,1,1,
  1,1,2,
  1,1,2
};
int length;//音符数量
int tonepin=8;//8号输出口


void setup() {
  // put your setup code here, to run once:
  pinMode(tonepin,OUTPUT);
  length=sizeof(yinfu)/sizeof(yinfu[0]);

}
int t;
void loop() {
  // put your main code here, to run repeatedly:
  for(t=0;t<length;t++)
  {
    tone(tonepin,yinfu[t]);  //发出声音
    delay(400*yinpai[t]);   //发音时间,用户可自调
    noTone(tonepin);    //停止发声
  }
  delay(3000);
}[/mw_shl_code]

4. 按键
程序上传成功后,在串口监视器可以显示按钮按下情况:
虚谷号+扩展板体验图12

代码如下:
[mw_shl_code=cpp,true]int buttonPinA = 2;     // 定义按键A  pin 2  
int buttonPinB = 3;     // 定义按键B  pin 3  

int buttonStateA = 0;     //初始化按键A状态
int buttonStateB = 0;     //初始化按键B状态

void setup() {
  pinMode(buttonPinA,INPUT);   //初始化按键A为输入
  pinMode(buttonPinB,INPUT);   //初始化按键B为输入
  Serial.begin(9600);
}

void loop(){
  buttonStateA = digitalRead(buttonPinA);  //读取按键A的状态,按下按键,BSA置高
  buttonStateB = digitalRead(buttonPinB);  //读取按键B的状态,按下按键,BSB置高

  if (buttonStateA == HIGH) {      //判断是否为高
    Serial.println("Button A Pressed");
  }
  if (buttonStateB == HIGH) {      //判断是否为高
    Serial.println("Button B Pressed");
  }
  delay(200);                     //延迟0.2s
}[/mw_shl_code]

体验总结:
  • 案例丰富,可以很好的体验不同的案例,易入门,好上手
  • 教育版比创客版明显运行要流畅不少



后续再继续体验扩展板的各个引脚、接口等。




rzyzzxw  版主

发表于 2019-8-28 10:47:08

哇哦,真心好贴
回复

使用道具 举报

安卓机器人  中级技神

发表于 2019-8-30 00:01:41

学习OLED,试了,可以显示
回复

使用道具 举报

gada888  版主

发表于 2019-9-15 07:33:13

不错的板子
回复

使用道具 举报

珠峰  学徒

发表于 2020-5-14 13:53:55

请问楼主,用如何编写python程序,才能在OLED12864中显示信息,有例程吗?这里只介绍了使用Arduino IDE显示信息,谢谢!
回复

使用道具 举报

szjuliet  版主
 楼主|

发表于 2020-5-14 14:47:16

珠峰 发表于 2020-5-14 13:53
请问楼主,用如何编写python程序,才能在OLED12864中显示信息,有例程吗?这里只介绍了使用Arduino IDE显示 ...

可参考:https://learn.adafruit.com/monoc ... outs/python-usage-2
https://learn.adafruit.com/pages/17419/elements/3041225/download
回复

使用道具 举报

珠峰  学徒

发表于 2020-5-21 20:07:52

szjuliet 发表于 2020-5-14 14:47
可参考:https://learn.adafruit.com/monochrome-oled-breakouts/python-usage-2
https://learn.adafruit ...

捣鼓了几天,后来由于忙也放下了,能力有限,捣鼓不出来啊!还望能提供个可以直接运行的例程出来。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail