I can't tell if this is a problem with the code or the hardware at this point.
I'm using a SRD-05DC-SL-C single relay module. I have no load attached to it for the sake of troubleshooting.
the relay functions exactly as I expect it to with this one example I made.
int sol=0;
void setup() {
// put your setup code here, to run once:
pinMode(sol,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(sol,HIGH);
delay(3000);
digitalWrite(sol,LOW);
delay(100);
}
now that I've established that everything can work properly I made new code and kept all the existing wiring exactly the same. (with the addition of an ultrasonic sensor and its own wiring)
The IF statements at the end is where the important debug messages are.
const int trigPin = 5;
const int echoPin = 6;
const int rumble = 3;
const int sol=0;
long duration;
int distance;
int rumbleValue;
int solValue;
int motorCap;
int timer;
int lastTen[11];
int lastAverage;
int i;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(sol, OUTPUT);
timer=0;
solValue=3000;
for (i=0; i<11; i++)
{
lastTen[i]=0;
}
Serial.begin(9600);
}
void loop() {
// trigger ultra sound (takes 12 uS)
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//calcs from ultra sound
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
motorCap = distance;
if (motorCap >= 200)
{
motorCap = 200;
}
rumbleValue = map(motorCap, 0, 200, 255, 0);
//get average distance from past 10 millisecs
for (i=1; i<10; i++)
{
lastTen[i]=lastTen[i-1];
lastTen[10]=lastTen[10]+lastTen[i];
}
lastTen[0]=distance;
lastTen[10]=lastTen[10]+lastTen[0];
lastAverage=lastTen[10]/10;
lastTen[10]=0;
//time solenoid movement
if(lastAverage>199)
{
solValue=3000;
}
if(lastAverage<200)
{
solValue=1000;
}
if(lastAverage<150)
{
solValue=750;
}
if(lastAverage<100)
{
solValue=500;
}
if(lastAverage<50)
{
solValue=250;
}
//debug
Serial.print("Distance: ");
Serial.println(distance);
Serial.print("Solenoid Time: ");
Serial.println(solValue);
Serial.print("Time: ");
Serial.println(timer);
delay(1);
//timer and delay for solenoid
timer++;
if(timer==100)
{
digitalWrite(sol,LOW);
Serial.println("OFF");
}
if(timer>=solValue)
{
Serial.println("ON");
timer=0;
digitalWrite(sol,HIGH);
}
}
this is not working. I have made enough debug messages to show me that it should be firing when I expect it to, but alas, it is not.
here is the stream of debug messages which shows me that it should be firing when the words "ON" and "OFF" appear. (i put some breaks in it so it doesn't take so much space)
Solenoid Time: 250
Time: 0
Distance: 38
Solenoid Time: 250
Time: 1
Distance: 38
Solenoid Time: 250
Time: 2
Distance: 38
Solenoid Time: 250
Time: 3
Distance: 38
Solenoid Time: 250
Time: 4
Distance: 38
Solenoid Time: 250
Time: 5
Distance: 38
Solenoid Time: 250
Time: 6
Distance: 38
Solenoid Time: 250
Time: 7
Distance: 38
Solenoid Time: 250
Time: 8
Distance: 38
Solenoid Time: 250
Time: 9
Distance: 38
Solenoid Time: 250
Time: 10
Distance: 38
Solenoid Time: 250
etc................................................
Distance: 38
Solenoid Time: 250
Time: 98
Distance: 38
Solenoid Time: 250
Time: 99
OFF
Distance: 39
Solenoid Time: 250
Time: 100
Distance: 38
Solenoid Time: 250
Time: 101
Distance: 39
Solenoid Time: 250
Time: 102
Distance: 38
Solenoid Time: 250
Time: 103
Distance: 38
Solenoid Time: 250
etc..........................................
Distance: 38
Solenoid Time: 250
Time: 249
ON
Distance: 38
Solenoid Time: 250
Time: 0
Distance: 39
Solenoid Time: 250
Time: 1
Distance: 38
Solenoid Time: 250
Time: 2
Distance: 38
Solenoid Time: 250
etc.....................................
Distance: 38
Solenoid Time: 250
Time: 98
Distance: 38
Solenoid Time: 250
Time: 99
OFF
Distance: 38
Solenoid Time: 250
Time: 100
Distance: 38
Solenoid Time: 250
Time: 101
Distance: 39
Solenoid Time: 250
I can switch back and forth between the two programs without touching any hardware, and I get the same issue. It works with the first program, but not the second program.
I'm completely stumped at this point. please help me.