本帖最后由 巨窝 于 2017-11-6 10:33 编辑
遥控器代码。
接收头盔的代码我改天再找找,很简单其实。
解释一下几个比较黑暗的牛逼库。
第一个就是scoop,任务多开,比如使用scoop让两个led各自按照自己的频率闪烁,那么在各自的线程里面使用sleep();这个函数就可以了,比起protothread要简单很多。
第二个是PinChangeInt,可以多开中断,arduino的中断默认只有2,3脚,用了这个库所有IO全部可以变成中断使用,适合多编码器。。。u8g库不解释
[kenrobot_code]
#include "SCoop.h"
#include <PinChangeInt.h>
#include <U8glib.h>
#define PinA 10 //中断10
#define PinB 11 //中断11
#define PinC 8
#define PinD 9
unsigned long time = 0;
long count = 0; //计数值
int num = 240;
// int sensorValue ;
long count1 = 0; //计数值
int numa= 240;
volatile int oleda;
volatile int oledb;
volatile int oledc;
//int A;
//int B;
int R;
int p;
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);//启用
static unsigned char u8g_zuo_bits[] U8G_PROGMEM =
{0x40,0x00,0x40,0x00,0x40,0x00,0xFF,0x7F,0x20,0x00,0x20,0x00,0x20,0x00,0x10,0x00,
0xF0,0x1F,0x08,0x01,0x08,0x01,0x04,0x01,0x02,0x01,0x01,0x01,0xFC,0x7F,0x00,0x00,};
//左,阴码,逐行,逆向
static unsigned char u8g_you_bits[] U8G_PROGMEM =
{0x40,0x00,0x40,0x00,0x40,0x00,0xFF,0x7F,0x20,0x00,0x20,0x00,0x10,0x00,0x10,0x00,
0xF8,0x1F,0x14,0x10,0x12,0x10,0x11,0x10,0x10,0x10,0x10,0x10,0xF0,0x1F,0x10,0x10,};
//右
static unsigned char u8g_maohao_bits[] U8G_PROGMEM =
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,};
//冒号
static unsigned char u8g_tong_bits[] U8G_PROGMEM =
{0x00,0x04,0x80,0x3F,0x1E,0x11,0x12,0x0A,0xD2,0x7F,0x1E,0x00,0x92,0x3F,0x92,0x24,
0x9E,0x3F,0x92,0x24,0x92,0x3F,0x12,0x04,0x9E,0x3F,0x12,0x04,0xC0,0x7F,0x00,0x00,};
//瞳
static unsigned char u8g_ju_bits[] U8G_PROGMEM =
{0x00,0x00,0xBE,0x7F,0xA2,0x00,0xA2,0x00,0xA2,0x00,0xBE,0x3F,0x88,0x20,0x88,0x20,
0xBA,0x20,0x8A,0x20,0x8A,0x3F,0x8A,0x00,0xBA,0x00,0x87,0x00,0x80,0x7F,0x00,0x00,};
//距
static unsigned char u8g_CSU_bits[] U8G_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,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,0xC0,0xFF,0x07,0xF0,0xFF,0x03,0x7F,0x00,
0xFC,0x01,0x00,0x00,0x00,0xF8,0xFF,0x07,0xFC,0xFF,0x03,0x7F,0x00,0xFC,0x01,0x00,
0x00,0x00,0xFC,0xFF,0x07,0xFE,0xFF,0x03,0x7F,0x00,0xFC,0x01,0x00,0x00,0x00,0xFE,
0xFF,0x07,0xFE,0xFF,0x03,0x7F,0x00,0xFC,0x01,0x00,0x00,0x00,0xFF,0xFF,0x07,0xFF,
0xFF,0x03,0x7F,0x00,0xFC,0x01,0x00,0x00,0x80,0xFF,0x81,0x87,0xFF,0x80,0x03,0x7F,
0x00,0xFC,0x01,0x00,0x00,0xC0,0x7F,0x00,0x86,0x3F,0x00,0x02,0x7F,0x00,0xFC,0x01,
0x00,0x00,0xC0,0x7F,0x00,0x80,0x3F,0x00,0x00,0x7F,0x00,0xFC,0x01,0x00,0x00,0xE0,
0x1F,0x00,0x80,0x3F,0x00,0x00,0x7F,0x00,0xFC,0x01,0x00,0x00,0xE0,0x0F,0x00,0x80,
0x3F,0x00,0x00,0x7F,0x00,0xFC,0x01,0x00,0x00,0xF0,0x07,0x00,0x80,0xFF,0x00,0x00,
0x7F,0x00,0xFC,0x01,0x00,0x00,0xF0,0x07,0x00,0x80,0xFF,0x01,0x00,0x7F,0x00,0xFC,
0x01,0x00,0x00,0xF0,0x07,0x00,0x80,0xFF,0x0F,0x00,0x7F,0x00,0xFC,0x01,0x00,0x00,
0xF8,0x07,0x00,0x00,0xFE,0x3F,0x00,0x7F,0x00,0xFC,0x01,0x00,0x00,0xF8,0x07,0x00,
0x00,0xFE,0x7F,0x00,0x7F,0x00,0xFC,0x01,0x00,0x00,0xF8,0x07,0x00,0x00,0xFC,0xFF,
0x01,0x7F,0x00,0xFC,0x01,0x00,0x00,0xF8,0x07,0x00,0x00,0xF8,0xFF,0x03,0x7F,0x00,
0xFC,0x01,0x00,0x00,0xF8,0x07,0x00,0x00,0xC0,0xFF,0x07,0x7F,0x00,0xFC,0x01,0x00,
0x00,0xF8,0x07,0x00,0x00,0x00,0xFE,0x07,0x7F,0x00,0xFC,0x01,0x00,0x00,0xF8,0x07,
0x00,0x00,0x00,0xFE,0x0F,0x7F,0x00,0xFC,0x01,0x00,0x00,0xF0,0x0F,0x00,0x00,0x00,
0xF0,0x0F,0x7F,0x00,0xFC,0x01,0x00,0x00,0xF0,0x0F,0x00,0x00,0x00,0xE0,0x0F,0x7F,
0x00,0xFC,0x01,0x00,0x00,0xF0,0x1F,0x00,0x00,0x00,0xE0,0x0F,0xFF,0x00,0xFE,0x01,
0x00,0x00,0xE0,0x7F,0x00,0x86,0x01,0xE0,0x0F,0xFF,0x01,0xFF,0x01,0x00,0x00,0xC0,
0xFF,0x00,0x87,0x07,0xF8,0x0F,0xFF,0x83,0xFF,0x01,0x00,0x00,0x80,0xFF,0xFF,0x87,
0x1F,0xFE,0x07,0xFE,0xFF,0xFF,0x00,0x00,0x00,0x00,0xFF,0xFF,0x87,0xFF,0xFF,0x07,
0xFC,0xFF,0x7F,0x00,0x00,0x00,0x00,0xFE,0xFF,0x87,0xFF,0xFF,0x03,0xF8,0xFF,0x3F,
0x00,0x00,0x00,0x00,0xFC,0xFF,0x87,0xFF,0xFF,0x01,0xF0,0xFF,0x1F,0x00,0x00,0x00,
0x00,0xF8,0xFF,0x87,0xFF,0xFF,0x00,0xE0,0xFF,0x0F,0x00,0x00,0x00,0x00,0xF0,0xFF,
0x01,0xFE,0x3F,0x00,0x80,0xFF,0x03,0x00,0x00,0x00,0x00,0x80,0x3F,0x00,0xF0,0x07,
0x00,0x00,0xFE,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,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,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,};
/*"C:\Users\zjz\Desktop\无标题.bmp",0*/
void setup() //真正主程不能删除修改
{
mySCoop.start();
}
void loop()
{
yield();
}
defineTask(TaskTest);
void TaskTest::setup()
{
Serial.begin(9600);
pinMode(PinA,INPUT_PULLUP); //D10脚为输入
pinMode(PinB,INPUT_PULLUP); //D11脚为输入
attachPinChangeInterrupt(PinA,blinkA,FALLING);
attachPinChangeInterrupt(PinB,blinkB,FALLING);
pinMode(PinC,INPUT_PULLUP); //D8脚为输入
pinMode(PinD,INPUT_PULLUP); //D9脚为输入
attachPinChangeInterrupt(PinC,blinkC,FALLING);
attachPinChangeInterrupt(PinD,blinkD,FALLING);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(4, INPUT);
u8g.firstPage();
do {
u8g.drawXBMP( 0, 0, 102, 63, u8g_CSU_bits);//欢迎界面。
} while ( u8g.nextPage() );
sleep(1000);
time = millis(); //初值
}
void TaskTest::loop()
{
int buttonState = digitalRead(4);
if (buttonState == HIGH) {
Serial.print(30000);
Serial.print("X");
delay(100);
if (Serial.available() > 0) // 串口收到字符数,否则返回0.
{
p = Serial.parseInt(); // 在串口数据流中查找一个有效整数。
while (Serial.read() == 'X')
{ oledc=map(p,0,1023,0,65);
digitalWrite(7,HIGH);
}
}
}
else {
while (num != count)
{
num = count;
}
oleda=num;
while (numa != count1)
{
numa = count1;
}
oledb=numa;
u8g.firstPage();
do {
u8g.drawXBMP( 0, 0, 16, 16, u8g_zuo_bits);
u8g.drawXBMP( 20, 0, 16, 16, u8g_maohao_bits);
u8g.setFont(u8g_font_unifont);
u8g.setFont(u8g_font_timB12);//字体设置
u8g.setPrintPos(37, 16);
u8g.print(-oleda*oleda*oleda*oleda*0.000000001185+oleda*oleda*oleda*0.0000005392+oleda*oleda*0.002042-2.515*oleda+465.8);
u8g.print(" D");
u8g.drawXBMP( 0, 20, 16, 16, u8g_you_bits);
u8g.drawXBMP(20,20, 16, 16, u8g_maohao_bits);
u8g.setFont(u8g_font_timB12);//字体设置
u8g.setPrintPos(37, 35);
u8g.print(-oledb*oledb*oledb*oledb*0.000000001185+oledb*oledb*oledb*0.0000005392+oledb*oledb*0.002042-2.515*oledb+465.8);
u8g.print(" D");
u8g.drawXBMP( 0, 43, 16, 16, u8g_tong_bits);
u8g.drawXBMP( 20, 43, 16, 16, u8g_ju_bits);
u8g.drawXBMP( 36, 40, 16, 16, u8g_maohao_bits);
u8g.setPrintPos(56,58);
u8g.print(oledc);
u8g.print("mm");
} while ( u8g.nextPage() );
digitalWrite(7, LOW);
int A=oleda;
int B=oledb+10000;
Serial.print(A);
Serial.print("X");
Serial.print(B);
Serial.print("X");
}
}
void blinkA()
{
if ((millis() - time) > 15) //防抖动处理
count=count+3;
time = millis();
analogWrite(5,255);
}
void blinkB()
{
if ((millis() - time) > 15) //防抖动处理
count=count-2;
time = millis();
analogWrite(5,0);
}
void blinkC()
{
if ((millis() - time) > 15) //防抖动处理
count1=count1-3;
time = millis();
analogWrite(6,255);
}
void blinkD()
{
if ((millis() - time) > 15) //防抖动处理
count1=count1+2;
time = millis();
analogWrite(6,0);
}[/kenrobot_code]
|