我目前正在开始通过Arduino编程学习C ++。我正在编写一个8 * 8 LED矩阵,目前有一个半工作代码,它使用操纵杆来控制屏幕上的点。唯一的问题是:它的长度接近1000行。这相当于写了一篇长达1000页的文章,一遍又一遍地说“猪,毯子,市场”,直到我了解所涉及的实际逻辑。
我的朋友建议缩短它,我可以创建一个返回一个字节的函数。
如何做到这一点?
#include "LedControl.h"
LedControl lc=LedControl(12,11,10,1);
// Pins: DIN,CLK,CS, # of Display connected
// I put all the points on the matrix into an array.
// Ex. a11 is an array which there is a dot at the point (1,1)
// a23 is an array which holds the point (2,3), etc.
// I don't know how to shorten this with for loops, but I know there is a way.
byte a11[] = {
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a12[] = {
0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a13[] = {
0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a14[] = {
0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a15[] = {
0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a16[] = {
0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a17[] = {
0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a18[] = {
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a21[] = {
0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a22[] = {
0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a23[] = {
0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a24[] = {
0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a25[] = {
0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a26[] = {
0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a27[] = {
0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a28[] = {
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte a31[] = {
0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,
};
byte a32[] = {
0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,
};
byte a33[] = {
0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,
};
byte a34[] = {
0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,
};
byte a35[] = {
0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,
};
byte a36[] = {
0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,
};
byte a37[] = {
0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,
};
byte a38[] = {
0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
};
byte a41[] = {
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
};
byte a42[] = {
0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,
};
byte a43[] = {
0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,
};
byte a44[] = {
0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,
};
byte a45[] = {
0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,
};
byte a46[] = {
0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,
};
byte a47[] = {
0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,
};
byte a48[] = {
0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
};
byte a51[] = {
0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,
};
byte a52[] = {
0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,
};
byte a53[] = {
0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
};
byte a54[] = {
0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,
};
byte a55[] = {
0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,
};
byte a56[] = {
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
};
byte a57[] = {
0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
};
byte a58[] = {
0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
};
byte a61[] = {
0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,
};
byte a62[] = {
0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,
};
byte a63[] = {
0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,
};
byte a64[] = {
0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,
};
byte a65[] = {
0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,
};
byte a66[] = {
0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,
};
byte a67[] = {
0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,
};
byte a68[] = {
0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,
};
byte a71[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,
};
byte a72[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,
};
byte a73[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,
};
byte a74[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,
};
byte a75[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
};
byte a76[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,
};
byte a77[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,
};
byte a78[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
};
byte a81[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,
};
byte a82[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,
};
byte a83[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,
};
byte a84[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,
};
byte a85[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,
};
byte a86[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,
};
byte a87[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,
};
byte a88[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
};
byte none[] = //array where no points are lit {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
byte all[] = //array which all points are lit {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};
int xPin = A1; //Pins for Joystick and starting int's
int yPin = A0;
int buttonPin = 2;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Begin Serial
lc.shutdown(0,false); // Wake up displays
lc.setIntensity(0,5); // Set intensity levels
lc.clearDisplay(0); // Clear Displays
sa44(); //Starting point
pinMode(xPin, INPUT); //Declaring joystick
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
// I then made a function to display each corresponsing point on the screen.
// For example, point (1,1) is a11, and the function is sa11.
// Point (2,3) is a23 and the function is sa23, etc.
// Again, I probably should have shortened it with for loops, but I don't know how to use them for multiple bytes.
void sa11() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a11[i]);
}
}
void sa12() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a12[i]);
}
}
void sa13() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a13[i]);
}
}
void sa14() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a14[i]);
}
}
void sa15() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a15[i]);
}
}
void sa16() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a16[i]);
}
}
void sa17() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a17[i]);
}
}
void sa18() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a18[i]);
}
}
void sa21() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a21[i]);
}
}
void sa22() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a22[i]);
}
}
void sa23() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a23[i]);
}
}
void sa24() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a24[i]);
}
}
void sa25() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a25[i]);
}
}
void sa26() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a26[i]);
}
}
void sa27() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a27[i]);
}
}
void sa28() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a28[i]);
}
}
void sa31() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a31[i]);
}
}
void sa32() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a32[i]);
}
}
void sa33() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a33[i]);
}
}
void sa34() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a34[i]);
}
}
void sa35() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a35[i]);
}
}
void sa36() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a36[i]);
}
}
void sa37() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a37[i]);
}
}
void sa38() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a38[i]);
}
}
void sa41() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a41[i]);
}
}
void sa42() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a42[i]);
}
}
void sa43() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a43[i]);
}
}
void sa44() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a44[i]);
}
}
void sa45() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a45[i]);
}
}
void sa46() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a46[i]);
}
}
void sa47() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a47[i]);
}
}
void sa48() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a48[i]);
}
}
void sa51() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a51[i]);
}
}
void sa52() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a52[i]);
}
}
void sa53() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a53[i]);
}
}
void sa54() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a54[i]);
}
}
void sa55() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a55[i]);
}
}
void sa56() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a56[i]);
}
}
void sa57() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a57[i]);
}
}
void sa58() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a58[i]);
}
}
void sa61() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a61[i]);
}
}
void sa62() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a62[i]);
}
}
void sa63() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a63[i]);
}
}
void sa64() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a64[i]);
}
}
void sa65() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a65[i]);
}
}
void sa66() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a66[i]);
}
}
void sa67() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a67[i]);
}
}
void sa68() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a68[i]);
}
}
void sa71() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a71[i]);
}
}
void sa72() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a72[i]);
}
}
void sa73() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a73[i]);
}
}
void sa74() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a74[i]);
}
}
void sa75() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a75[i]);
}
}
void sa76() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a76[i]);
}
}
void sa77() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a77[i]);
}
}
void sa78() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a78[i]);
}
}
void sa81() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a81[i]);
}
}
void sa82() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a82[i]);
}
}
void sa83() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a83[i]);
}
}
void sa84() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a84[i]);
}
}
void sa85() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a85[i]);
}
}
void sa86() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a86[i]);
}
}
void sa87() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a87[i]);
}
}
void sa88() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,a88[i]);
}
}
void snone() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,none[i]);
}
}
void sall() {
for (int i = 0; i < 8; i++) {
lc.setRow(0,i,all[i]);
}
}
void loop() {
// put your main code here, to run repeatedly:
xPosition = analogRead(xPin); //Read x, y, button values
yPosition = analogRead(yPin);
buttonState = digitalRead(buttonPin);
Serial.print("\nX: "); //Print x, y, and button values to console
Serial.print(xPosition);
Serial.print(" \nY: ");
Serial.print(yPosition);
Serial.print(" \nButton: ");
Serial.println(buttonState);
// This is the Logic behind it.
// The values range from 0 to 700: 340-350 being at rest, 0 being down, and 700 being up.
if(xPosition < 250 && xPosition > 200) {
sa43();
}
if(xPosition > 100 && xPosition < 200) {
sa42();
}
if(xPosition == 0) {
sa41();
}
if(xPosition > 400) {
sa45();
}
if(yPosition == 0 && xPosition > 340 && xPosition < 350) {
sa54();
}
if(yPosition > 600 && xPosition > 340 && xPosition < 350) {
sa34();
}
if(xPosition > 340 && xPosition < 350 && yPosition > 340 && yPosition < 350) {
sa44();
}
delay(10); //Wait 10 milliseconds before scanning for new input
if(buttonState == 0) {
//If button is pressed, light up whole screen.
sall();
}
}
非常感谢帮助。
答案 0 :(得分:3)
此代码的 lot 确实是不必要的。您可以将数组的所有数组和函数重写为1个函数:
void WriteBytes(byte* data, int length)
{
for (int i = 0; i < length; ++i)
lc.setRow(0, i, data[i]);
}
然后你会这样称呼它:
byte data[] =
{
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
WriteBytes(data, sizeof(data) / sizeof(data[0]));
答案 1 :(得分:0)
Jamey D提供了一个很好的答案。 Gernot1976显示了通过动态生成内容来减少数组声明的好方法。
但是,由于您的问题涉及处理实际数组,我可以使用对数组的引用向您展示如何执行此操作。
使用模板,您可以轻松地将数组传递给常用功能。
此方法接受对数组的引用。如果您想对大小N
进行硬编码,那么您可以完全删除该模板。
template< size_t N >
void WriteBytes( byte (&arr)[N] ){
for (int i = 0; i < N; ++i){
lc.setRow(0, i, arr[i]);
}
}
//Non template:
void WriteBytes( byte (&arr)[8] ){
for (int i = 0; i < 8; ++i){
lc.setRow(0, i, arr[i]);
}
}
但是你的问题要求返回一个数组。所以为了展示方式,这里有一个小例子。因为我没有LedControl
&amp; lc
在这些示例中,其行已注释掉。
//Use a typedef to make declarations simpler.
typedef byte (&ArrRef)[8]; //A reference to an array which contains 8 elements (type not variable)
ArrRef func(); //A prototype to prevent the IDE adding one above the typedef.
ArrRef func(){
static byte arr[8];
//Dynamically generate contents of array in arr.
//Return the array
return arr;
}
//The equivalent to the non-template version, just using a typedef instead.
void WriteBytes( ArrRef arr ); //Prototype.
void WriteBytes( ArrRef arr ){
for (int i = 0; i < 8; ++i){
//lc.setRow(0, i, arr[i]);
}
}
void setup() {
//Reference the array returned:
ArrRef ref = func();
//Use array
for( byte el : ref ){
//...
}
//Pass to function
WriteBytes(ref);
}
void loop() {}
我写了更深入的article here,其中介绍了使用指针和数组引用将数组传递给函数的基础知识。
答案 2 :(得分:-2)
在C ++语言中,数组是指向内存块的指针。因此,您可以返回指向数组的指针。请注意,您不能返回指向函数中创建的数组的指针,因为这个内存可能会被覆盖(也可能会被覆盖)。
byte a[100];
byte b[100];
byte* f(int x)
{
if(x % 2 == 0)
return a;
else
return b;
}