Stoplicht met lichtsensor

Arduino specifieke Software
Berichten: 2
Geregistreerd: 18 Okt 2020, 11:24

Stoplicht met lichtsensor

Berichtdoor youri242 » 18 Okt 2020, 11:41

Goedendag,


Ik zou graag hulp willen met mijn eerste Arduino project. Ik wil namelijk een stoplicht maken, die gekoppeld is aan een lichtsensor. Wanneer het donker is, (if (CDS > 50) ), dan moet het stoplicht gaan werken. Hierbij heb ik een schematische tekening gemaakt, welke in de bijlage staat. Desgelijks heb ik onderstaande code gebruikt.
Nu werkt het stoplicht, echter lukt het mij niet om de code van de lichtsensor toe te voegen. Op toevoeging van de schematische tekening, is mijn lichtsensor (de temt6000) als volgt aangesloten:
VCC (V): 5V
GND(G): Ground
Out(S): A0 (analoog)

Nu heb ik de volgende werkende code


[code2=]
// Traffic Lights with 7-Segment Timer
// Every 8 seconds, switch from Red Light to Green Light and vice-versa
// with 2 seconds delay in between for the Yellow Light
const int a=6; //a of 7-segment attach to digital pin 6
const int b=5; //b of 7-segment attach to digital pin 5
const int c=4; //c of 7-segment attach to digital pin 4
const int d=10; //d of 7-segment attach to digital pin 10
const int e=9; //e of 7-segment attach to digital pin 9
const int f=7; //f of 7-segment attach to digital pin 7
const int g=8; //g of 7-segment attach to digital pin 8

const int red=11; // Red LED attached to digital pin 11
const int yellow=12; // Yellow LED attached to digital pin 12
const int green=13; // Green LED attached to digital pin 13

void setup()
{
// loop over thisPin from 4 to 13 and set them all to output
for(int thisPin = 4; thisPin <= 13; thisPin++)
{
pinMode(thisPin,OUTPUT);
}
}

void loop()
{
// Initial State of LEDs
digitalWrite(red,HIGH);
digitalWrite(yellow,LOW);
digitalWrite(green,LOW);
countdown_from_8();

// Countdown 2 seconds and turn on Yellow LED
digitalWrite(red,LOW);
digitalWrite(yellow,HIGH);
digitalWrite(green,LOW);
countdown_from_2();

// Countdown 8 seconds and turn on Green LED
digitalWrite(red,LOW);
digitalWrite(yellow,LOW);
digitalWrite(green,HIGH);
countdown_from_8();

// Countdown 2 seconds and turn on Yellow LED
digitalWrite(red,LOW);
digitalWrite(yellow,HIGH);
digitalWrite(green,LOW);
countdown_from_2();
}

void countdown_from_2()
{
digital_2();// display 2 to the 7-segment
delay(1000); //wait for a second
digital_1();// display 1 to the 7-segment
delay(1000); //wait for a second
digital_0();// display 0 to the 7-segment
delay(1000);//wait for a second
}

void countdown_from_8()
{
digital_8();// display 8 to the 7-segment
delay(1000);//wait for a second
digital_7();// display 7 to the 7-segment
delay(1000); //wait for a second
digital_6();// display 6 to the 7-segment
delay(1000); //wait for a second
digital_5();// display 5 to the 7-segment
delay(1000); //wait for a second
digital_4();// display 4 to the 7-segment
delay(1000); //wait for a second
digital_3();// display 3 to the 7-segment
delay(1000); //wait for a second
digital_2();// display 2 to the 7-segment
delay(1000); //wait for a second
digital_1();// display 1 to the 7-segment
delay(1000); //wait for a second
digital_0();// display 0 to the 7-segment
delay(1000);//wait for a second
}

void digital_0(void) // display 0 to the 7-segment
{
for(int j = 4;j <= 10;j++)
digitalWrite(j,HIGH);
digitalWrite(g,LOW); // Turn off g of the 7-segment
}

void digital_1(void) // display 1 to the 7-segment
{
for(int j = 4;j <= 10;j++)//turn off the others
{
digitalWrite(j,LOW);
}
digitalWrite(c,HIGH);//turn the c of the 7-segment on
digitalWrite(b,HIGH);//turn the b of the 7-segment on
}

void digital_2(void) // display 2 to the 7-segment
{
digitalWrite(b,HIGH);
digitalWrite(a,HIGH);
for(int j = 8;j <= 10;j++)
digitalWrite(j,HIGH);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
}

void digital_3(void) // display 3 to the 7-segment
{
unsigned char j;
digitalWrite(g,HIGH);
digitalWrite(d,HIGH);
for(j=5;j<=7;j++)
digitalWrite(j,HIGH);
digitalWrite(f,LOW);
digitalWrite(e,LOW);
}

void digital_4(void) // display 4 to the 7-segment
{
digitalWrite(c,HIGH);
digitalWrite(b,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(a,LOW);
digitalWrite(e,LOW);
digitalWrite(d,LOW);
}

void digital_5(void) // display 5 to the 7-segment
{
for(int j = 4;j <= 10;j++) {
digitalWrite(j, HIGH);
}
digitalWrite(b,LOW);
digitalWrite(e,LOW);
}

void digital_6(void) // display 6 to the 7-segment
{
for(int j = 4;j <= 10;j++) {
digitalWrite(j, HIGH);
}
digitalWrite(b,LOW);
}

void digital_7(void) // display 7 to the 7-segment
{
for(int j = 4;j <= 10;j++) {
digitalWrite(j, LOW);
}
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(f,HIGH);
}

void digital_8(void) // display 8 to the 7-segment
{
unsigned char j;
for(j = 4;j <=10;j++)
digitalWrite(j,HIGH);
}
[/code2]


Nu wil ik het volgende if / else statement in de loop hieraan toevoegen:

int CDS = analogRead(A0);
Serial.println(CDS);
if (CDS > 50){
digitalWrite(green, LOW);

}
else {


Ik hoop dat mijn informatie voldoende is. Ik zou graag willen leren hoe ik het volgende moet doen.

Met vriendelijke groet
Youri

Ter toevoeging, ik heb onderstaande video gebruikt voor mijn project:
https://www.youtube.com/watch?v=L-6zf2_e20U
Bijlagen
Arduino schematic -2.jpg
Arduino schematic -2.jpg (42.88 KiB) 2877 keer bekeken

Advertisement

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Stoplicht met lichtsensor

Berichtdoor Koepel » 18 Okt 2020, 17:19

Ja, zo ongeveer gaat dat.

Misschien is het beter om die sequence van cijfers en leds in een apart functie te zetten.
Let er dan op, dat de leds uit zijn op het einde. en bij het begin de juiste leds weer aan gaan.

Code: Alles selecteren
void AparteFunctieVoorDieSequenceVanCijfersEnLeds()
{
  // Zet de leds goed voor de beginconditie
  ...


  // Initial State of LEDs
  digitalWrite(red,HIGH);
  digitalWrite(yellow,LOW);
  ...
  digitalWrite(yellow,HIGH);
  digitalWrite(green,LOW);
  countdown_from_2();

  // zet de leds uit
  ...
}


Dan kun je in de loop() kijken naar de sensor:

Code: Alles selecteren
void loop()
{
  int CDS = analogRead(A0);
  Serial.println(CDS);
  if (CDS > 50)
  {
    AparteFunctieVoorDieSequenceVanCijfersEnLeds();
  }

  delay( 100);   // slow down the sketch
}


Je kunt natuurlijk zelf een betere naam voor de functie "AparteFunctieVoorDieSequenceVanCijfersEnLeds()" verzinnen ;)

Met de delay(100) maak ik de sketch langzamer. Het is namelijk geen goed idee om teveel Serial.println() te doen. Als CDS kleiner dan 50 is, dan zouden er teveel Serial.println() gedaan worden.

P.S.: Let je even op de code-tags.
Met de "Code" knop krijg je [ code ] en [ / code ].
Met "Select a Syntax" en dan "C++" krijg een ander soort code-tags, dat kan ook.

Berichten: 2
Geregistreerd: 18 Okt 2020, 11:24

Re: Stoplicht met lichtsensor

Berichtdoor youri242 » 19 Okt 2020, 09:02

Het werkt! :) Dankjewel voor de informatie!

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 23 gasten