int ledPin = 9;
int digitalPin7 =7;
int digitalPin6 =6;
int analogPin3 = 3;
int analogPin0 = 0;
int val3 = 0;
int val0 = 0;
float duration0 = 0.1;
float duration1 = 0.01;
unsigned long lastFlip0=0;
unsigned long lastFlip1=0;
int state0 = LOW;
int state1 = LOW;
int led = 13;
void setup()
{
pinMode(digitalPin7, OUTPUT);
pinMode(digitalPin6, OUTPUT);
pinMode(led,OUTPUT);
digitalWrite(digitalPin7, state0);
digitalWrite(digitalPin6, state1);
Serial.begin(9600);
}
void loop()
{
val3 = analogRead(analogPin3);
val0 = analogRead(analogPin0);
unsigned long now = millis();
if (now - lastFlip0 >= duration0)
{
lastFlip0 = now;
state0 = !state0;
digitalWrite(digitalPin7, state0);
}
if (now - lastFlip1 >= duration1)
{
lastFlip1 = now;
state1 = !state1;
digitalWrite(digitalPin6, state1);
}
if(val0 > val3)
{
digitalWrite(led, HIGH);
duration0 = duration0 - random(1, 10);
duration1 = duration1 + random(1, 10);
}
else
{
digitalWrite(led, LOW);
duration1 = duration1 - random(1, 10);
duration0 = duration0 + random(1, 10);
}
Serial.write("Val3: ");
Serial.println(val3);
Serial.write("Val0: ");
Serial.println(val0);
Serial.write("Time: ");
Serial.println(now);
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.