先更新展示系统 的代码,随后更新图图
第一次使用Arduino,纯是按照新手的51思路写的,异常冗杂,但是蛮好用= =
没有使用中断服务,纯键盘扫描
#if ARDUINO >= 100 #include "Arduino.h" #else #include "WProgram.h" #endif //官方的库 #include "LedControl.h" #include <Wire.h> #include <Adafruit_PWMServoDriver.h> const int button1=13; const int button2=12; int button3=11; int step_next=3; int step_before=2; int switch1=0; int switch2=1; int step_stop=10; int mode_flag=0; int step_flag=0; int light=3; int mode_temp=0; int step_temp=0; /*led部分相关定义*/ /* Now we need a LedControl to work with. ***** These pin numbers will probably not work with your hardware ***** 第一个参数:pin 4 is connected to the DataIn 第二个参数:pin 6 is connected to the CLK 第三个参数:pin 5 is connected to LOAD 第四个参数:We have only a single MAX72XX. */ LedControl lc=LedControl(4,6,5,2); LedControl dig=LedControl(7,8,9,1); /* we always wait a bit between updates of the display */ unsigned long delaytime=100; unsigned long delaytime1=2000; //舵机部分定义 // called this way, it uses the default address 0x40 //Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // you can also call it with a different address you want Adafruit_PWMServoDriver servo_pwm1 = Adafruit_PWMServoDriver(0x40); //Dervo x16 #1 Adafruit_PWMServoDriver servo_pwm2 = Adafruit_PWMServoDriver(0x41); //Dervo X16 #2 // Depending on your servo make, the pulse width min and max may vary, you // want these to be as small/large as possible without hitting the hard stop // for max range. You'll have to tweak them as necessary to match the servos you // have! #define SERVOMIN 150 // this is the 'minimum' pulse length count (out of 4096) #define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096) void setServoPulse(uint8_t n, double pulse) { double pulselength; pulselength = 1000000; // 1,000,000 us per second pulselength /= 60; // 60 Hz Serial.print(pulselength); Serial.println(" us per period"); pulselength /= 4096; // 12 bits of resolution Serial.print(pulselength); Serial.println(" us per bit"); pulse *= 1000; pulse /= pulselength; Serial.println(pulse); servo_pwm2.setPWM(n, 0, pulse); } void setup() { mode_flag=0; step_flag=0; Serial.begin(9600); Serial.println("16 channel Servo test!"); pinMode(button1,INPUT); pinMode(button2,INPUT); pinMode(button3,INPUT); pinMode(step_next,INPUT); pinMode(step_before,INPUT); pinMode(switch1,OUTPUT); pinMode(switch2,OUTPUT); pinMode(light,OUTPUT); digitalWrite(light,1); /*初始化led部分*/ /* The MAX72XX is in power-saving mode on startup, we have to do a wakeup call */ lc.shutdown(0,false); /* Set the brightness to a medium values */ lc.setIntensity(0,8); /* and clear the display */ lc.clearDisplay(0); lc.shutdown(1,false); /* Set the brightness to a medium values */ lc.setIntensity(1,8); /* and clear the display */ lc.clearDisplay(1); //此块为数码管(温度) dig.shutdown(0,false); /* Set the brightness to a medium values */ dig.setIntensity(0,8); /* and clear the display */ dig.clearDisplay(0); //初始化舵机部分 servo_pwm1.begin(); servo_pwm1.setPWMFreq(50); servo_pwm2.begin(); servo_pwm2.setPWMFreq(50); // Analog servos run at ~60 Hz updates yield(); unsigned int servo_position01[18]{ 475,475,475,475,475,475,475,475 ,475,475,475,475,475,475,475,475,475,475}; for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm1.setPWM(counter1, 0, servo_position01[counter1]); delay(50); } for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm2.setPWM(counter1, 0, servo_position01[counter1]); delay(50); } } void key_scan() { if(digitalRead(button1)==1) { delay(8); if(digitalRead(button1)==1) { mode_flag=1; step_flag=0; } while(digitalRead(button1)==1); } if(digitalRead(button2)==1) { delay(8); if(digitalRead(button2)==1) { mode_flag=2; step_flag=0; } while(digitalRead(button2)); } if(digitalRead(button3)==1) { delay(100); if(digitalRead(button3)==1) { mode_flag=3; step_flag=0; } while(digitalRead(button3)); } if(digitalRead(step_next)==1) { delay(8); if(digitalRead(step_next)==1) { step_flag=step_flag+1; } while(digitalRead(step_next)); } if(digitalRead(step_before)==1) { delay(100); if(digitalRead(step_before)==1) { if(step_flag!=0) step_flag=step_flag-1; while(digitalRead(step_before)); } } if(digitalRead(step_stop)==1) { delay(100); if(digitalRead(step_stop)==1) { step_flag=0; mode_flag=0; while(digitalRead(step_stop)); } } } int wait() { while(digitalRead(button1)==0); while(digitalRead(button1)==1); return 1; } int select() { int temp=0; dig.clearDisplay(0); dig.setDigit(0,0,0x0,false); do{ if(digitalRead(button2)==0) { delay(50); if(digitalRead(button2)==0) { if(temp<4) temp++; else temp=0; dig.setDigit(0,0,temp,false); } while(digitalRead(button2)==0); } if(digitalRead(button1)==0) { delay(50); if(digitalRead(button1)==0) return temp; } }while(1); } void mode1() { dig.clearDisplay(0); unsigned int servo_position01[18]{ 280,250,250, 280,280,280,270, 270,270,250,250, 260,260,255,250, 270,270,270}; for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm1.setPWM(counter1, 0, servo_position01[counter1]); delay(50); } for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm2.setPWM(counter1, 0, 270); delay(50); } dig.setDigit(0,0,0x5,false); dig.setDigit(0,1,0x5,false); dig.setDigit(0,4,0x0,false); dig.setDigit(0,5,0x3,false); delay(300); lc.setRow(1,2,B00000010); delay(300); lc.setRow(1,2,B00000001); delay(300); lc.setRow(1,2,B00001000); delay(300); lc.setRow(1,2,B00000100); delay(300); lc.setRow(1,2,B00010000); delay(300); delay(700); lc.setRow(1,2,B00010010); delay(300); lc.setRow(1,2,B00010001); delay(300); lc.setRow(1,2,B00011000); delay(300); lc.setRow(1,2,B00010100); delay(300); delay(700); lc.setRow(1,2,B00010110); delay(300); lc.setRow(1,2,B00010101); delay(300); lc.setRow(1,2,B00011100); delay(300); wait(); //!继电器2开启 digitalWrite(light,0); byte led1[8]={ B11111111, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 } ; byte led2[8]={ B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B10111111, B10000000 } ; byte led3[8]={ B00000000, B00000000, B00000000, B00000000, B11111111, B00000000, B00000000, B00000000 } ; byte led4[8]={ B00000000, B00000000, B11111111, B00000000, B00000000, B00000000, B00000000, B00000000 } ; for(int i=0;i<8;i++) { lc.setRow(0,0,led1[0]); lc.setRow(0,1,led1[1]); lc.setRow(0,2,led1[2]); lc.setRow(0,3,led1[3]); lc.setRow(0,4,led1[4]); lc.setRow(0,5,led1[5]); lc.setRow(0,6,led1[6]); lc.setRow(0,7,led1[7]); delay(300); lc.setRow(0,0,led2[0]); lc.setRow(0,1,led2[1]); lc.setRow(0,2,led2[2]); lc.setRow(0,3,led2[3]); lc.setRow(0,4,led2[4]); lc.setRow(0,5,led2[5]); lc.setRow(0,6,led2[6]); lc.setRow(0,7,led2[7]); delay(300); lc.setRow(0,0,led3[0]); lc.setRow(0,1,led3[1]); lc.setRow(0,2,led3[2]); lc.setRow(0,3,led3[3]); lc.setRow(0,4,led3[4]); lc.setRow(0,5,led3[5]); lc.setRow(0,6,led3[6]); lc.setRow(0,7,led3[7]); delay(300); lc.setRow(0,0,led4[0]); lc.setRow(0,1,led4[1]); lc.setRow(0,2,led4[2]); lc.setRow(0,3,led4[3]); lc.setRow(0,4,led4[4]); lc.setRow(0,5,led4[5]); lc.setRow(0,6,led4[6]); lc.setRow(0,7,led4[7]); delay(300); } wait(); lc.clearDisplay(1); dig.clearDisplay(0); dig.setDigit(0,0,0x0,false); delay(5); dig.setDigit(0,1,0x4,false); delay(5); dig.setDigit(0,4,0x0,false); delay(5); dig.setDigit(0,5,0x3,false); delay(500); for(int i=0;i<8;i++) { lc.setRow(0,0,led1[0]); lc.setRow(0,1,led1[1]); lc.setRow(0,2,led1[2]); lc.setRow(0,3,led1[3]); lc.setRow(0,4,led1[4]); lc.setRow(0,5,led1[5]); lc.setRow(0,6,led1[6]); lc.setRow(0,7,led1[7]); delay(300); lc.setRow(0,0,led2[0]); lc.setRow(0,1,led2[1]); lc.setRow(0,2,led2[2]); lc.setRow(0,3,led2[3]); lc.setRow(0,4,led2[4]); lc.setRow(0,5,led2[5]); lc.setRow(0,6,led2[6]); lc.setRow(0,7,led2[7]); delay(300); lc.setRow(0,0,led3[0]); lc.setRow(0,1,led3[1]); lc.setRow(0,2,led3[2]); lc.setRow(0,3,led3[3]); lc.setRow(0,4,led3[4]); lc.setRow(0,5,led3[5]); lc.setRow(0,6,led3[6]); lc.setRow(0,7,led3[7]); delay(300); lc.setRow(0,0,led4[0]); lc.setRow(0,1,led4[1]); lc.setRow(0,2,led4[2]); lc.setRow(0,3,led4[3]); lc.setRow(0,4,led4[4]); lc.setRow(0,5,led4[5]); lc.setRow(0,6,led4[6]); lc.setRow(0,7,led4[7]); delay(300); } wait(); } void mode2() { dig.clearDisplay(0); unsigned int servo_position01[18]{475,475,475, 475,475,475,475, 475,475,475,475, 475,475,475 }; for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm1.setPWM(counter1, 0, servo_position01[counter1]); delay(50); } for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm2.setPWM(counter1, 0, servo_position01[counter1]); delay(50); } dig.setDigit(0,0,0x5,false); delay(5); dig.setDigit(0,1,0x5,false); delay(5); dig.setDigit(0,4,0x0,false); delay(5); dig.setDigit(0,5,0x3,false); delay(5); wait(); for(int i=0;i<8;i++) { lc.setRow(1,2,B11111111); delay(400); lc.setRow(1,2,B00000000); delay(400); } lc.setRow(1,2,B11111111); wait(); digitalWrite(light,1); byte led1[8]={ B10000000, B10000000, B10000000, B10000000, B10000000, B10000000, B11000000, B00000000 } ; byte led2[8]={ B00000010, B00000010, B00000010, B00000010, B00000010, B00000010, B00000010, B00000010 } ; byte led3[8]={ B00001000, B00001000, B00001000, B00001000, B00001000, B00001000, B00000000, B00000000 } ; byte led4[8]={ B00100000, B00100000, B00100000, B00100000, B00100000, B00100000, B00100000, B00100000 } ; for(int i=0;i<8;i++) { lc.setRow(0,0,led1[0]); lc.setRow(0,1,led1[1]); lc.setRow(0,2,led1[2]); lc.setRow(0,3,led1[3]); lc.setRow(0,4,led1[4]); lc.setRow(0,5,led1[5]); lc.setRow(0,6,led1[6]); lc.setRow(0,7,led1[7]); delay(300); lc.setRow(0,0,led2[0]); lc.setRow(0,1,led2[1]); lc.setRow(0,2,led2[2]); lc.setRow(0,3,led2[3]); lc.setRow(0,4,led2[4]); lc.setRow(0,5,led2[5]); lc.setRow(0,6,led2[6]); lc.setRow(0,7,led2[7]); delay(300); lc.setRow(0,0,led3[0]); lc.setRow(0,1,led3[1]); lc.setRow(0,2,led3[2]); lc.setRow(0,3,led3[3]); lc.setRow(0,4,led3[4]); lc.setRow(0,5,led3[5]); lc.setRow(0,6,led3[6]); lc.setRow(0,7,led3[7]); delay(300); lc.setRow(0,0,led4[0]); lc.setRow(0,1,led4[1]); lc.setRow(0,2,led4[2]); lc.setRow(0,3,led4[3]); lc.setRow(0,4,led4[4]); lc.setRow(0,5,led4[5]); lc.setRow(0,6,led4[6]); lc.setRow(0,7,led4[7]); delay(300); } dig.clearDisplay(0); dig.setDigit(0,0,0x0,false); delay(5); dig.setDigit(0,1,0x4,false); delay(5); dig.setDigit(0,4,0x0,false); delay(20); dig.setDigit(0,5,0x3,false); delay(5); lc.setRow(1,2,B00000000); wait(); } void mode3() { dig.clearDisplay(0); unsigned int servo_position01[18]{ 475,475,475, 270,475,475,270, 270,270,250,250, 270,475,475,260, 475,475,475}; for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm1.setPWM(counter1, 0, servo_position01[counter1]); delay(50); } for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm2.setPWM(counter1, 0, servo_position01[16]); delay(50); } wait(); dig.setDigit(0,0,0x9,false); delay(5); dig.setDigit(0,1,0x0,false); delay(5); dig.setDigit(0,4,0x2,false); delay(5); dig.setDigit(0,5,0x0,false); delay(300); wait(); for(int i=0;i<8;i++) { lc.setRow(1,2,B11111111); delay(400); lc.setRow(1,2,B00000000); delay(400); } lc.setRow(1,2,B11111111); wait(); } void mode4() { dig.clearDisplay(0); lc.clearDisplay(0); unsigned int servo_position01[18]{ 280,250,250, 280,280,280,270, 270,270,250,250, 260,260,255,250, 270,270,270}; for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm1.setPWM(counter1, 0, servo_position01[counter1]); delay(50); } for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm2.setPWM(counter1, 0, 270); delay(50); } dig.setDigit(0,0,0x5,false); dig.setDigit(0,1,0x5,false); dig.setDigit(0,4,0x0,false); dig.setDigit(0,5,0x3,false); delay(300); lc.setRow(1,2,B00000010); delay(300); lc.setRow(1,2,B00000001); delay(300); lc.setRow(1,2,B00001000); delay(300); lc.setRow(1,2,B00000100); delay(300); lc.setRow(1,2,B00010000); delay(300); delay(700); lc.setRow(1,2,B00010010); delay(300); lc.setRow(1,2,B00010001); delay(300); lc.setRow(1,2,B00011000); delay(300); lc.setRow(1,2,B00010100); delay(300); delay(700); lc.setRow(1,2,B00010110); delay(300); lc.setRow(1,2,B00010101); delay(300); lc.setRow(1,2,B00011100); delay(300); delay(3000); //!继电器2开启 byte led1[8]={ B11111111, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 } ; byte led2[8]={ B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B10111111, B10000000 } ; byte led3[8]={ B00000000, B00000000, B00000000, B00000000, B11111111, B00000000, B00000000, B00000000 } ; byte led4[8]={ B00000000, B00000000, B11111111, B00000000, B00000000, B00000000, B00000000, B00000000 } ; for(int i=0;i<8;i++) { lc.setRow(0,0,led1[0]); lc.setRow(0,1,led1[1]); lc.setRow(0,2,led1[2]); lc.setRow(0,3,led1[3]); lc.setRow(0,4,led1[4]); lc.setRow(0,5,led1[5]); lc.setRow(0,6,led1[6]); lc.setRow(0,7,led1[7]); delay(300); lc.setRow(0,0,led2[0]); lc.setRow(0,1,led2[1]); lc.setRow(0,2,led2[2]); lc.setRow(0,3,led2[3]); lc.setRow(0,4,led2[4]); lc.setRow(0,5,led2[5]); lc.setRow(0,6,led2[6]); lc.setRow(0,7,led2[7]); delay(300); lc.setRow(0,0,led3[0]); lc.setRow(0,1,led3[1]); lc.setRow(0,2,led3[2]); lc.setRow(0,3,led3[3]); lc.setRow(0,4,led3[4]); lc.setRow(0,5,led3[5]); lc.setRow(0,6,led3[6]); lc.setRow(0,7,led3[7]); delay(300); lc.setRow(0,0,led4[0]); lc.setRow(0,1,led4[1]); lc.setRow(0,2,led4[2]); lc.setRow(0,3,led4[3]); lc.setRow(0,4,led4[4]); lc.setRow(0,5,led4[5]); lc.setRow(0,6,led4[6]); lc.setRow(0,7,led4[7]); delay(300); } delay(3000); lc.clearDisplay(1); dig.clearDisplay(0); dig.setDigit(0,0,0x0,false); delay(5); dig.setDigit(0,1,0x4,false); delay(5); dig.setDigit(0,4,0x0,false); delay(5); dig.setDigit(0,5,0x3,false); delay(500); for(int i=0;i<8;i++) { lc.setRow(0,0,led1[0]); lc.setRow(0,1,led1[1]); lc.setRow(0,2,led1[2]); lc.setRow(0,3,led1[3]); lc.setRow(0,4,led1[4]); lc.setRow(0,5,led1[5]); lc.setRow(0,6,led1[6]); lc.setRow(0,7,led1[7]); delay(300); lc.setRow(0,0,led2[0]); lc.setRow(0,1,led2[1]); lc.setRow(0,2,led2[2]); lc.setRow(0,3,led2[3]); lc.setRow(0,4,led2[4]); lc.setRow(0,5,led2[5]); lc.setRow(0,6,led2[6]); lc.setRow(0,7,led2[7]); delay(300); lc.setRow(0,0,led3[0]); lc.setRow(0,1,led3[1]); lc.setRow(0,2,led3[2]); lc.setRow(0,3,led3[3]); lc.setRow(0,4,led3[4]); lc.setRow(0,5,led3[5]); lc.setRow(0,6,led3[6]); lc.setRow(0,7,led3[7]); delay(300); lc.setRow(0,0,led4[0]); lc.setRow(0,1,led4[1]); lc.setRow(0,2,led4[2]); lc.setRow(0,3,led4[3]); lc.setRow(0,4,led4[4]); lc.setRow(0,5,led4[5]); lc.setRow(0,6,led4[6]); lc.setRow(0,7,led4[7]); delay(300); } delay(3000); delay(3000); dig.clearDisplay(0); lc.clearDisplay(0); dig.clearDisplay(0); unsigned int servo_position03[18]{475,475,475, 475,475,475,475, 475,475,475,475, 475,475,475 }; for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm1.setPWM(counter1, 0, servo_position03[counter1]); delay(50); } for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm2.setPWM(counter1, 0, servo_position03[counter1]); delay(50); } dig.setDigit(0,0,0x5,false); delay(5); dig.setDigit(0,1,0x5,false); delay(5); dig.setDigit(0,4,0x0,false); delay(5); dig.setDigit(0,5,0x3,false); delay(5); delay(3000); for(int i=0;i<8;i++) { lc.setRow(1,2,B11111111); delay(400); lc.setRow(1,2,B00000000); delay(400); } lc.setRow(1,2,B11111111); delay(3000); byte led11[8]={ B10000000, B10000000, B10000000, B10000000, B10000000, B10000000, B11000000, B00000000 } ; byte led12[8]={ B00000010, B00000010, B00000010, B00000010, B00000010, B00000010, B00000010, B00000010 } ; byte led13[8]={ B00001000, B00001000, B00001000, B00001000, B00001000, B00001000, B00000000, B00000000 } ; byte led14[8]={ B00100000, B00100000, B00100000, B00100000, B00100000, B00100000, B00100000, B00100000 } ; for(int i=0;i<8;i++) { lc.setRow(0,0,led11[0]); lc.setRow(0,1,led11[1]); lc.setRow(0,2,led11[2]); lc.setRow(0,3,led11[3]); lc.setRow(0,4,led11[4]); lc.setRow(0,5,led11[5]); lc.setRow(0,6,led11[6]); lc.setRow(0,7,led11[7]); delay(300); lc.setRow(0,0,led12[0]); lc.setRow(0,1,led12[1]); lc.setRow(0,2,led12[2]); lc.setRow(0,3,led12[3]); lc.setRow(0,4,led12[4]); lc.setRow(0,5,led12[5]); lc.setRow(0,6,led12[6]); lc.setRow(0,7,led12[7]); delay(300); lc.setRow(0,0,led13[0]); lc.setRow(0,1,led13[1]); lc.setRow(0,2,led13[2]); lc.setRow(0,3,led13[3]); lc.setRow(0,4,led13[4]); lc.setRow(0,5,led13[5]); lc.setRow(0,6,led13[6]); lc.setRow(0,7,led13[7]); delay(300); lc.setRow(0,0,led14[0]); lc.setRow(0,1,led14[1]); lc.setRow(0,2,led14[2]); lc.setRow(0,3,led14[3]); lc.setRow(0,4,led14[4]); lc.setRow(0,5,led14[5]); lc.setRow(0,6,led14[6]); lc.setRow(0,7,led14[7]); delay(300); } dig.clearDisplay(0); dig.setDigit(0,0,0x0,false); delay(5); dig.setDigit(0,1,0x4,false); delay(5); dig.setDigit(0,4,0x0,false); delay(20); dig.setDigit(0,5,0x3,false); delay(5); lc.setRow(1,2,B00000000); delay(300); delay(3000); dig.clearDisplay(0); lc.clearDisplay(0); unsigned int servo_position02[18]{ 475,475,475, 270,475,475,270, 270,270,250,250, 270,475,475,260, 475,475,475}; for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm1.setPWM(counter1, 0, servo_position02[counter1]); delay(50); } for (uint16_t counter1=0;counter1<16;counter1++) { servo_pwm2.setPWM(counter1, 0, servo_position02[16]); delay(50); } delay(3000); dig.setDigit(0,0,0x9,false); delay(5); dig.setDigit(0,1,0x0,false); delay(5); dig.setDigit(0,4,0x2,false); delay(5); dig.setDigit(0,5,0x0,false); delay(3000); for(int i=0;i<8;i++) { lc.setRow(1,2,B11111111); delay(400); lc.setRow(1,2,B00000000); delay(400); } lc.setRow(1,2,B11111111); delay(3000); } void loop() { mode_flag=select(); if(mode_flag==1) mode1(); if(mode_flag==2) mode2(); if(mode_flag==3) mode3(); if(mode_flag==4) do{mode4();}while(1); setup(); }