我有2个草图,一个是我led的运行代码,另一个是blynk连接代码,我希望将这2个草图结合起来,以确保我可以连接到我的blynk应用,还可以使我的led代码运行。我使用esp 8266 wifi sheild连接到我的手机应用程序,因为我已经用完了我的arduino的所有针脚
blynk连接代码
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "ghznMVLIilXc08bYkO9qyNl_A6KaY7kT";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "find yr own one";//store ssid user name
char pass[] = "yashwini123";//store password
// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600 // set to 9600
ESP8266 wifi(&EspSerial);
void setup()
{
// Debug console
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
}
void loop()
{
Blynk.run();
}
LED代码
#include <Wire.h>
/*
4x4x4 LED Cube
Connection Setup:
Columns
[(x,y)-Pin]
(1,1)-13
(1,2)-12
(1,3)-11
(1,4)-10
(2,1)-9
(2,2)-8
(2,3)-7
(2,4)-6
(3,1)-5
(3-2)-4
(3-3)-3
(3,4)-2
(4,1)-1
(4,2)-0
(4,3)-A5
(4,4)-A4
Layers
[layer-Pin]
a-A0
b-A1
c-A2
d-A3
*/
//initializing and declaring led rows
int column[16]={13,12,11,10,A4,A5,9,8,7,6,5,4,3,2,1,0};//switch a4 a5 with 6 7
//initializing and declaring led layers
int layer[4]={A3,A2,A1,A0};
int time = 250;
void setup()
{
//setting rows to ouput
for(int i = 0; i<16; i++)
{
pinMode(column[i], OUTPUT);
}
//setting layers to output
for(int i = 0; i<4; i++)
{
pinMode(layer[i], OUTPUT);
}
//seeding random for random pattern
randomSeed(analogRead(10));
}
//xxxxxxxxxxxxxxxxxxxxFUNCTION LOOPxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void loop()
{
turnEverythingOff();//turn all off
flickerOn();
turnEverythingOn();//turn all on
delay(time);
turnOnAndOffAllByLayerUpAndDownNotTimed();
layerstompUpAndDown();
turnOnAndOffAllByColumnSideways();
delay(time);
aroundEdgeDown();
turnEverythingOff();
randomflicker();
randomRain();
diagonalRectangle();
goThroughAllLedsOneAtATime();
propeller();
spiralInAndOut();
flickerOff();
turnEverythingOff();
delay(2000);
}
//xxxxxxxxxxxxxxxxxxxxFUNCTIONSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
///////////////////////////////////////////////////////////turn all off
void turnEverythingOff()
{
for(int i = 0; i<16; i++)
{
digitalWrite(column[i], 1);
}
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 0);
}
}
////////////////////////////////////////////////////////////turn all on
void turnEverythingOn()
{
for(int i = 0; i<16; i++)
{
digitalWrite(column[i], 0);
}
//turning on layers
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1);
}
}
///////////////////////////////////////////////////////turn columns off
void turnColumnsOff()
{
for(int i = 0; i<16; i++)
{
digitalWrite(column[i], 1);
}
}
/////////////////////////////////////////////////////////////flicker on
void flickerOn()
{
int i = 150;
while(i != 0)
{
turnEverythingOn();
delay(i);
turnEverythingOff();
delay(i);
i-= 5;
}
}
//////////////turn everything on and off by layer up and down NOT TIMED
void turnOnAndOffAllByLayerUpAndDownNotTimed()
{
int x = 75;
for(int i = 5; i != 0; i--)
{
turnEverythingOn();
for(int i = 4; i!=0; i--)
{
digitalWrite(layer[i-1], 0);
delay(x);
}
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1);
delay(x);
}
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 0);
delay(x);
}
for(int i = 4; i!=0; i--)
{
digitalWrite(layer[i-1], 1);
delay(x);
}
}
}
//////////////////////////turn everything on and off by column sideways
void turnOnAndOffAllByColumnSideways()
{
int x = 75;
turnEverythingOff();
//turn on layers
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1);
}
for(int y = 0; y<3; y++)
{
//turn on 0-3
for(int i = 0; i<4; i++)
{
digitalWrite(column[i], 0);
delay(x);
}
//turn on 4-7
for(int i = 4; i<8; i++)
{
digitalWrite(column[i], 0);
delay(x);
}
//turn on 8-11
for(int i = 8; i<12; i++)
{
digitalWrite(column[i], 0);
delay(x);
}
//turn on 12-15
for(int i = 12; i<16; i++)
{
digitalWrite(column[i], 0);
delay(x);
}
//turn off 0-3
for(int i = 0; i<4; i++)
{
digitalWrite(column[i], 1);
delay(x);
}
//turn off 4-7
for(int i = 4; i<8; i++)
{
digitalWrite(column[i], 1);
delay(x);
}
//turn off 8-11
for(int i = 8; i<12; i++)
{
digitalWrite(column[i], 1);
delay(x);
}
//turn off 12-15
for(int i = 12; i<16; i++)
{
digitalWrite(column[i], 1);
delay(x);
}
//turn on 12-15
for(int i = 12; i<16; i++)
{
digitalWrite(column[i], 0);
delay(x);
}
//turn on 8-11
for(int i = 8; i<12; i++)
{
digitalWrite(column[i], 0);
delay(x);
}
//turn on 4-7
for(int i = 4; i<8; i++)
{
digitalWrite(column[i], 0);
delay(x);
}
//turn on 0-3
for(int i = 0; i<4; i++)
{
digitalWrite(column[i], 0);
delay(x);
}
//turn off 12-15
for(int i = 12; i<16; i++)
{
digitalWrite(column[i], 1);
delay(x);
}
//turn off 8-11
for(int i = 8; i<12; i++)
{
digitalWrite(column[i], 1);
delay(x);
}
//turn off 4-7
for(int i = 4; i<8; i++)
{
digitalWrite(column[i], 1);
delay(x);
}
//turn off 0-3
for(int i = 0; i<4; i++)
{
digitalWrite(column[i], 1);
delay(x);
}
}
}
/////////////////////////////////////////up and down single layer stomp
void layerstompUpAndDown()
{
int x = 75;
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 0);
}
for(int y = 0; y<5; y++)
{
for(int count = 0; count<1; count++)
{
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1);
delay(x);
digitalWrite(layer[i], 0);
}
for(int i = 4; i !=0; i--)
{
digitalWrite(layer[i-1], 1);
delay(x);
digitalWrite(layer[i-1], 0);
}
}
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1);
delay(x);
}
for(int i = 4; i!=0; i--)
{
digitalWrite(layer[i-1], 0);
delay(x);
}
}
}
////////////////////////////////////////////////////////////flicker off
void flickerOff()
{
turnEverythingOn();
for(int i = 0; i!= 150; i+=5)
{
turnEverythingOff();
delay(i+50);
turnEverythingOn();
delay(i);
}
}
///////////////////////////////////////////around edge of the cube down
void aroundEdgeDown()
{
for(int x = 200; x != 0; x -=50)
{
turnEverythingOff();
for(int i = 4; i != 0; i--)
{
digitalWrite(layer[i-1], 1);
digitalWrite(column[5], 0);
digitalWrite(column[6], 0);
digitalWrite(column[9], 0);
digitalWrite(column[10], 0);
digitalWrite(column[0], 0);
delay(x);
digitalWrite(column[0], 1);
digitalWrite(column[4], 0);
delay(x);
digitalWrite(column[4], 1);
digitalWrite(column[8], 0);
delay(x);
digitalWrite(column[8], 1);
digitalWrite(column[12], 0);
delay(x);
digitalWrite(column[12], 1);
digitalWrite(column[13], 0);
delay(x);
digitalWrite(column[13], 1);
digitalWrite(column[15], 0);
delay(x);
digitalWrite(column[15], 1);
digitalWrite(column[14], 0);
delay(x);
digitalWrite(column[14], 1);
digitalWrite(column[11], 0);
delay(x);
digitalWrite(column[11], 1);
digitalWrite(column[7], 0);
delay(x);
digitalWrite(column[7], 1);
digitalWrite(column[3], 0);
delay(x);
digitalWrite(column[3], 1);
digitalWrite(column[2], 0);
delay(x);
digitalWrite(column[2], 1);
digitalWrite(column[1], 0);
delay(x);
digitalWrite(column[1], 1);
}
}
}
/////////////////////////////////////////////////////////random flicker
void randomflicker()
{
turnEverythingOff();
int x = 10;
for(int i = 0; i !=750; i+=2)
{
int randomLayer = random(0,4);
int randomColumn = random(0,16);
digitalWrite(layer[randomLayer], 1);
digitalWrite(column[randomColumn], 0);
delay(x);
digitalWrite(layer[randomLayer], 0);
digitalWrite(column[randomColumn], 1);
delay(x);
}
}
////////////////////////////////////////////////////////////random rain
void randomRain()
{
turnEverythingOff();
int x = 100;
for(int i = 0; i!=60; i+=2)
{
int randomColumn = random(0,16);
digitalWrite(column[randomColumn], 0);
digitalWrite(layer[0], 1);
delay(x+50);
digitalWrite(layer[0], 0);
digitalWrite(layer[1], 1);
delay(x);
digitalWrite(layer[1], 0);
digitalWrite(layer[2], 1);
delay(x);
digitalWrite(layer[2], 0);
digitalWrite(layer[3], 1);
delay(x+50);
digitalWrite(layer[3], 0);
digitalWrite(column[randomColumn], 1);
}
}
/////////////////////////////////////////////////////diagonal rectangle
void diagonalRectangle()
{
int x = 350;
turnEverythingOff();
for(int count = 0; count<5; count++)
{
//top left
for(int i = 0; i<8; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[3], 1);
digitalWrite(layer[2], 1);
delay(x);
turnEverythingOff();
//middle middle
for(int i = 4; i<12; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[1], 1);
digitalWrite(layer[2], 1);
delay(x);
turnEverythingOff();
//bottom right
for(int i = 8; i<16; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[0], 1);
digitalWrite(layer[1], 1);
delay(x);
turnEverythingOff();
//bottom middle
for(int i = 4; i<12; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[0], 1);
digitalWrite(layer[1], 1);
delay(x);
turnEverythingOff();
//bottom left
for(int i = 0; i<8; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[0], 1);
digitalWrite(layer[1], 1);
delay(x);
turnEverythingOff();
//middle middle
for(int i = 4; i<12; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[1], 1);
digitalWrite(layer[2], 1);
delay(x);
turnEverythingOff();
//top right
for(int i = 8; i<16; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[2], 1);
digitalWrite(layer[3], 1);
delay(x);
turnEverythingOff();
//top middle
for(int i = 4; i<12; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[2], 1);
digitalWrite(layer[3], 1);
delay(x);
turnEverythingOff();
}
//top left
for(int i = 0; i<8; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[3], 1);
digitalWrite(layer[2], 1);
delay(x);
turnEverythingOff();
}
//////////////////////////////////////////////////////////////propeller
void propeller()
{
turnEverythingOff();
int x = 90;
for(int y = 4; y>0; y--)
{
for(int i = 0; i<6; i++)
{
//turn on layer
digitalWrite(layer[y-1], 1);
//a1
turnColumnsOff();
digitalWrite(column[0], 0);
digitalWrite(column[5], 0);
digitalWrite(column[10], 0);
digitalWrite(column[15], 0);
delay(x);
//b1
turnColumnsOff();
digitalWrite(column[4], 0);
digitalWrite(column[5], 0);
digitalWrite(column[10], 0);
digitalWrite(column[11], 0);
delay(x);
//c1
turnColumnsOff();
digitalWrite(column[6], 0);
digitalWrite(column[7], 0);
digitalWrite(column[8], 0);
digitalWrite(column[9], 0);
delay(x);
//d1
turnColumnsOff();
digitalWrite(column[3], 0);
digitalWrite(column[6], 0);
digitalWrite(column[9], 0);
digitalWrite(column[12], 0);
delay(x);
//d2
turnColumnsOff();
digitalWrite(column[2], 0);
digitalWrite(column[6], 0);
digitalWrite(column[9], 0);
digitalWrite(column[13], 0);
delay(x);
//d3
turnColumnsOff();
digitalWrite(column[1], 0);
digitalWrite(column[5], 0);
digitalWrite(column[10], 0);
digitalWrite(column[14], 0);
delay(x);
}
}
//d4
turnColumnsOff();
digitalWrite(column[0], 0);
digitalWrite(column[5], 0);
digitalWrite(column[10], 0);
digitalWrite(column[15], 0);
delay(x);
}
//////////////////////////////////////////////////////spiral in and out
void spiralInAndOut()
{
turnEverythingOn();
int x = 60;
for(int i = 0; i<6; i++)
{
//spiral in clockwise
digitalWrite(column[0], 1);
delay(x);
digitalWrite(column[1], 1);
delay(x);
digitalWrite(column[2], 1);
delay(x);
digitalWrite(column[3], 1);
delay(x);
digitalWrite(column[7], 1);
delay(x);
digitalWrite(column[11], 1);
delay(x);
digitalWrite(column[15], 1);
delay(x);
digitalWrite(column[14], 1);
delay(x);
digitalWrite(column[13], 1);
delay(x);
digitalWrite(column[12], 1);
delay(x);
digitalWrite(column[8], 1);
delay(x);
digitalWrite(column[4], 1);
delay(x);
digitalWrite(column[5], 1);
delay(x);
digitalWrite(column[6], 1);
delay(x);
digitalWrite(column[10], 1);
delay(x);
digitalWrite(column[9], 1);
delay(x);
///////////////////////////////////////spiral out counter clockwise
digitalWrite(column[9], 0);
delay(x);
digitalWrite(column[10], 0);
delay(x);
digitalWrite(column[6], 0);
delay(x);
digitalWrite(column[5], 0);
delay(x);
digitalWrite(column[4], 0);
delay(x);
digitalWrite(column[8], 0);
delay(x);
digitalWrite(column[12], 0);
delay(x);
digitalWrite(column[13], 0);
delay(x);
digitalWrite(column[14], 0);
delay(x);
digitalWrite(column[15], 0);
delay(x);
digitalWrite(column[11], 0);
delay(x);
digitalWrite(column[7], 0);
delay(x);
digitalWrite(column[3], 0);
delay(x);
digitalWrite(column[2], 0);
delay(x);
digitalWrite(column[1], 0);
delay(x);
digitalWrite(column[0], 0);
delay(x);
///////////////////////////////////////spiral in counter clock wise
digitalWrite(column[0], 1);
delay(x);
digitalWrite(column[4], 1);
delay(x);
digitalWrite(column[8], 1);
delay(x);
digitalWrite(column[12], 1);
delay(x);
digitalWrite(column[13], 1);
delay(x);
digitalWrite(column[14], 1);
delay(x);
digitalWrite(column[15], 1);
delay(x);
digitalWrite(column[11], 1);
delay(x);
digitalWrite(column[7], 1);
delay(x);
digitalWrite(column[3], 1);
delay(x);
digitalWrite(column[2], 1);
delay(x);
digitalWrite(column[1], 1);
delay(x);
digitalWrite(column[5], 1);
delay(x);
digitalWrite(column[9], 1);
delay(x);
digitalWrite(column[10], 1);
delay(x);
digitalWrite(column[6], 1);
delay(x);
//////////////////////////////////////////////spiral out clock wise
digitalWrite(column[6], 0);
delay(x);
digitalWrite(column[10], 0);
delay(x);
digitalWrite(column[9], 0);
delay(x);
digitalWrite(column[5], 0);
delay(x);
digitalWrite(column[1], 0);
delay(x);
digitalWrite(column[2], 0);
delay(x);
digitalWrite(column[3], 0);
delay(x);
digitalWrite(column[7], 0);
delay(x);
digitalWrite(column[11], 0);
delay(x);
digitalWrite(column[15], 0);
delay(x);
digitalWrite(column[14], 0);
delay(x);
digitalWrite(column[13], 0);
delay(x);
digitalWrite(column[12], 0);
delay(x);
digitalWrite(column[8], 0);
delay(x);
digitalWrite(column[4], 0);
delay(x);
digitalWrite(column[0], 0);
delay(x);
}
}
//////////////////////////////////////go through all leds one at a time
void goThroughAllLedsOneAtATime()
{
int x = 15;
turnEverythingOff();
for(int y = 0; y<5; y++)
{
//0-3
for(int count = 4; count != 0; count--)
{
digitalWrite(layer[count-1], 1);
for(int i = 0; i<4; i++)
{
digitalWrite(column[i], 0);
delay(x);
digitalWrite(column[i], 1);
delay(x);
}
digitalWrite(layer[count-1], 0);
}
//4-7
for(int count = 0; count < 4; count++)
{
digitalWrite(layer[count], 1);
for(int i = 4; i<8; i++)
{
digitalWrite(column[i], 0);
delay(x);
digitalWrite(column[i], 1);
delay(x);
}
digitalWrite(layer[count], 0);
}
//8-11
for(int count = 4; count != 0; count--)
{
digitalWrite(layer[count-1], 1);
for(int i = 8; i<12; i++)
{
digitalWrite(column[i], 0);
delay(x);
digitalWrite(column[i], 1);
delay(x);
}
digitalWrite(layer[count-1], 0);
}
//12-15
for(int count = 0; count < 4; count++)
{
digitalWrite(layer[count], 1);
for(int i = 12; i<16; i++)
{
digitalWrite(column[i], 0);
delay(x);
digitalWrite(column[i], 1);
delay(x);
}
digitalWrite(layer[count], 0);
}
}
}
最佳答案
理论上,您必须将两个项目/草图的setup()
和loop()
功能组合在一起。
对于您而言,这有点困难,因为您的LED项目的loop()
函数在delay
和loop()
调用的函数中都包含对loop()
的调用。
您必须确保在执行LED工作时经常调用Blynk.run();
。因此,您可能必须重构LED项目的loop()
函数以及从其中调用的包含delay
调用的所有函数,否则将花费很长时间才能运行。
也可以看看
http://help.blynk.cc/en/articles/2091699-keep-your-void-loop-clean
https://dzone.com/articles/arduino-using-millis-instead-of-delay
作为一个简单的技巧,您可以将所有对delay(x)
的调用替换为delayWithBlynk(x)
并使用类似
void delayWithBlynk(unsigned long delay)
{
unsigned long time_now; // for current time in loop
unsigned long time_start = millis(); // start time of intended delay
do {
Blynk.run();
time_now = millis(); // current time
while(time_now - time_start < delay); // repeat if delay time has not passed yet
}
想法是每当LED功能的执行必须等待延迟时,重复调用
Blynk.run()
。 (此代码/解决方案未经测试。)更好的解决方案是重构所有函数,使其在固定周期内重复调用,例如每毫秒一次或每10毫秒一次,并注意内部的时间和状态。
想法:从
turnEverythingOff()
调用的所有函数(loop()
等)均通过其返回值指示是否需要再次调用它们或任务已完成。在loop()
中,保持一个状态,该状态指示必须立即调用哪个从属函数。该函数将被反复调用,直到发出完成任务的信号。然后状态将切换到下一个功能,该功能将在下一个周期等中调用。关于c++ - 如何在blynk(arduino)中合并2个文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58269469/