'setup' типті емес деп аталатын қатені көрсетуді жалғастыруда менің қажеттіліктерімді қанағаттандыру үшін интернетте табылған кодты жұмыс істеуге тырыстым. Мен оны жарамсыз деп белгіледім, кодты қайта жаздым, бірақ кодты қайта жаздым, бірақ сол мәселе туындайды және бұл мәселені қалай шешуге болады деп ойладым.
Бұл бағдарлама бір сәтте 3 түрлі сенсордан деректер жинайды және әр түрлі есептеулерді осы бөліктерде жасайды және оларды сақтауға немесе экспорттауға болатын кестеде дәйекті түрде басып шығарады. Қазіргі уақытта жалғыз сенсор - бұл ультрадыбыстық сенсор, ол қашықтықты өлшейді. (кодтың соңғы 2 тобы тек қана циклды басып шығару үшін негізгі басып шығару функциясына ие)
Міне код:
#include
long timeCounter;//milliseconds counter
long timePassed;//The actual milliseconds time
int trigPin = 2;
int echoPin = 3;
long delay1 = 10; //ms for cycle of event 1
long delay2 = 5; //ms for cycle of event 2
long delay3 = 25; //ms for cycle of event 3
long delay4 = 10;
// Counters for the number of triggered events before the
// corresponding event starts
int countEvent1, countEvent2, countEvent3, countEvent4;
int TRIGGER = 5;//ms to trigger the milliseconds timer
int MAX1 = 2;//Number or triggered milliseconds to start event1
int MAX2 = 1;//Number or triggered milliseconds to start event2
int MAX3 = 5;//Number or triggered milliseconds to start event3
int MAX4 = 2;
// Initialisation
setup {
timeCounter = millis();//read the actual internal time
timePassed = 0;//we start with no time passed
countEvent1 = 0;
countEvent2 = 0;
countEvent3 = 0;
countEvent4 = 0;
}
// Infinite cycle
loop() {
//Read the actual time
timePassed = millis() - timeCounter;
//The smallest unit of time that should trigger an event is for event 2 (5 ms)
//so our trigger should count this smaller unit
//Check if the time passed after last reading is the trigger time
if(timePassed >= TRIGGER) {
if(++countEvent1 == MAX1){
//Reset the trigger and start the event
event1();
countEvent1 = 0;
}//Event 1
//It's time to manage the events
if(++countEvent2 == MAX2){
//Reset the trigger and start the event
event2();
countEvent2 = 0;
}//Event2
//It's time to manage the events
if(++countEvent3 == MAX3){
//Reset the trigger and start the event
event3();
countEvent3 = 0;
}//Event3
if(++countEvent4 == MAX4){
//Reset the trigger and start the event
event4();
countEvent4 = 0;
}//Event4
}//Trigger
}//LOOP
// ============ Event functions ==============
event1() {
long duration, distance;
digitalWrite(trigPin,HIGH);
delay(100);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance =(duration/2)/29.1;
}
event2() {
Serial.print("work")
}
event3() {
Serial.print("work")
}
Мен кодтау үшін өте жаңашыл боламын, сонда жасырын қателіктер жасырылады, осылайша кепілдік бере аламын.