Cảm biến ánh sáng — Arduino Uno simulation

Đọc giá trị từ Grove Light Sensor và in ra Serial.

An interactive Arduino Uno circuit simulation you can run free in your browser on Velxio, by minh_khoa.

Components

Sketch code

int led = 4;
int sense = A0;
bool ledstate = false;
int button = 2;
int lastbutton = HIGH;
void setup() {
  pinMode(led, OUTPUT);
  pinMode(sense, INPUT);
  pinMode(button, INPUT_PULLUP);
  Serial.begin(9600);
}
void loop() {
int currentbutton = digitalRead(button);
if (lastbutton == HIGH){
  ledstate = true;
  delay(50);
}
if (currentbutton == LOW){
  ledstate = false;
  delay(50);
}
 int sunnysense = analogRead 
 (sense);
 Serial.println(sunnysense);
 delay(200);
 if (sunnysense < 400 && ledstate == true){
  digitalWrite(led, HIGH);
 } else {
  digitalWrite(led, LOW);
 }
 delay(200);
}

More projects by minh_khoa