304-17 “Programmaliq injiniringi” Orinbaeva Nabira
1-Laboratoriyaliq jumisi
Kerekli úskeneler: Arduino UNO R3,aq svetodiot, jasil svetodiot,pereklyuchatel,resistor 220 Ω.
Bul mısal millis () benen waqtın gúzetip baradı, sol sebepli Arduino
belgilengen waqıt aralıǵinda basqa zatlardı islewi múmkin (sensorlardı oqıw hám
t.b.)
int message = 0;
unsigned long TASK_TIME = 10000;
unsigned long BREAK_TIME = 5000;
unsigned long taskDelayStart = 0;
unsigned long breakDelayStart = 0;
bool taskDelayRunning = false;
bool breakDelayRunning = false;
bool taskLightOn = false;
#define taskLampPin 13
#define breakLampPin 12
#define triggerPin 4
void setup() {
pinMode(triggerPin, INPUT_PULLUP); //set up switch pin with internal pullup resistor
pinMode(taskLampPin, OUTPUT);
digitalWrite(taskLampPin, LOW);
pinMode(breakLampPin, OUTPUT);
digitalWrite(breakLampPin, LOW);
// start the serial connection
Serial.begin(9600);
}
void loop() {
message = digitalRead(triggerPin);
if (message == 0){ //if a task is in progress
if (!taskLightOn && !taskDelayRunning & !breakDelayRunning){ // if the lamp is off and neither timer has started yet
taskDelayStart = millis(); //start delay
taskDelayRunning = true; //start the task timer
digitalWrite(taskLampPin, HIGH); //turns on the light
taskLightOn = true;
Serial.println("New task: begin work, and continue...");
}
if (taskLightOn && taskDelayRunning && ((millis() - taskDelayStart) >= TASK_TIME)) { // this code executes when task time is up
taskDelayStart += TASK_TIME; // this prevents drift in the delays
taskDelayRunning = false;
Serial.println("time has elapsed, you've been working hard, take a break");
digitalWrite(breakLampPin, HIGH); //turns on the light
digitalWrite(taskLampPin, LOW); //turns off the light
taskLightOn = false;
breakDelayStart = millis(); //start delay
breakDelayRunning = true;
}
if (!taskLightOn && breakDelayRunning && ((millis() - breakDelayStart) >= BREAK_TIME)) { // this code executes when break time is up
breakDelayStart += BREAK_TIME; // this prevents drift in the delays
breakDelayRunning = false;
Serial.println("break time is over, back to work");
taskDelayRunning = true;
taskDelayStart = millis(); //start delay
digitalWrite(breakLampPin, LOW); //turns on the light
digitalWrite(taskLampPin, HIGH); //turns on the light
taskLightOn = true;
}
}
if (message == 1){ //if task is completed, turn it off
breakDelayRunning = false;
taskDelayRunning = false;
if (taskLightOn){
Serial.println("task complete");
digitalWrite(taskLampPin, LOW); //turns off the light
digitalWrite(breakLampPin, LOW);
taskLightOn = false;
} Serial.println(" waiting for task");
}}
Do'stlaringiz bilan baham: |