đếm 2 cảm biến hông ngoại trên arduino

lucks

Học sinh phổ thông
#1
các b giúp mình xem sai ở đâu mà làm k ra
#include <Servo.h>
#include <EEPROM.h>
Servo servo1;
Servo servo2;
int latchPin1 = 8;
int clockPin1 = 10;
int dataPin1 = 9;
int latchPin2 = 11;
int clockPin2 = 13;
int dataPin2 = 12;
int resetPin =2;
int Sensor1 =3;
int Sensor2 =4;

int SensorState1;
int SensorState2;
int LastSensorState1;
int LastSensorState2;
int b1,b2,c1,c2;
const int Seg[10] = {
0b11000000, //0
0b11111001, //1
0b10100100, //2
0b10110000, //3
0b10011001, //4
0b10010010, //5
0b10000010, //6
0b11111000, //7
0b10000000, //8
0b10010000, //9
};
const int led [9] = {
0,
0b1000,
0b0100,
0b0010,
0b0001 ,
};
void setup()
{
pinMode (latchPin1, OUTPUT);

pinMode (clockPin1, OUTPUT);
pinMode (dataPin1, OUTPUT);
pinMode (latchPin2, OUTPUT);
pinMode (clockPin2, OUTPUT);
pinMode (dataPin2, OUTPUT);
pinMode(Sensor1, INPUT);
pinMode(Sensor2, INPUT);
servo1.attach(7);
servo2.attach(6);
}


void Delay(float delayTime, void (func)())
{
unsigned long endTime = millis() + delayTime;
while(millis() < endTime)
{
func();
}
}
void Display()
{
led7segPlus (c1,1);
delay(1);
led7segPlus (b1,2);
delay(1);
led7segPlus (c2,3);
delay(1);
led7segPlus (b2,4);
delay(1);

}

void loai1()
{
Display();

SensorState1 = digitalRead(Sensor1);
if ((SensorState1 != LastSensorState1)&(SensorState1 == 0))
{

EEPROM.write(1,EEPROM.read(1)+1);
if (EEPROM.read(1) > 99) { EEPROM.write(1,0);}
c1 = EEPROM.read(1) % 10;
b1 = EEPROM.read(1) / 10;
//Delay(2300,loai2);
}

LastSensorState1 = SensorState1;
}

void loai2()

{
Display();
SensorState2 = digitalRead(Sensor2);

if ((SensorState2 != LastSensorState2)&(SensorState2 == 0))
{
EEPROM.write(2,EEPROM.read(2)+1);
if (EEPROM.read(2) > 99) { EEPROM.write(2,0);}
c2 = EEPROM.read(2) % 10;
b2 = EEPROM.read(2) / 10;
//Delay(2300,loai1);
}
LastSensorState2 = SensorState2;
}
void loop()
{
//Serial.print("Gia tri cam bien1:");
// Serial.println(digitalRead(Sensor1));
loai1();
loai2();
Display();

}


void led7segPlus (int number, int pin)
{
digitalWrite(latchPin1, 0);
shiftOut(dataPin1, clockPin1, MSBFIRST, led[pin]);
digitalWrite(latchPin2, 0);
shiftOut(dataPin2, clockPin2, MSBFIRST, Seg[number]);
digitalWrite(latchPin2, 1);
digitalWrite(latchPin1, 1);

}
 

Quảng cáo Google