Loading...
Pages: [1]   Go Down
Author Topic: arduino uno locked in loop  (Read 341 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i've been having some issues with the arduino uno.    i have written a code for it but the board keeps getting locked into a loop and running the alarm process regardless of what i do.   also for some reason the output marked red goes high whenever i disconnect the proxy, that is if the alarm doesn't continue to sound.   here is a sample of the code.  i thought it might be the board but i have bought two and they both do the same thing.  any suggestions?


const int Red = 7;
const int Green = 4;
const int Blue = 6;
const int Yellow = 8;
const int Proxy = A0;
const int Redin = A1;
const int Greenin = A2;
const int Bluein = A3;
const int Yellowin = A4;
const int Alarm = 9;
int pval = 0;  //proxy value
int rval = 0;  //red value
int gval = 0;  //green value
int bval = 0;  //blue value
int yval = 0;  //yellow value

void setup()
{
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);
  pinMode(Yellow, OUTPUT);
  pinMode(Proxy, INPUT);
  pinMode(Alarm, OUTPUT);
  pinMode(Redin, INPUT);
  pinMode(Greenin, INPUT);
  pinMode(Bluein, INPUT);
  pinMode(Yellowin, INPUT);
}
void loop()
{
  pval = analogRead(Proxy); //read proxy value and store it
 
  if (pval < 1000) {
    digitalWrite(Alarm, LOW);
    digitalWrite(Red, LOW);
    digitalWrite(Green, LOW);
    digitalWrite(Blue, LOW);
    digitalWrite(Yellow, LOW);
  }else{
   
  //Read the values
 
    rval = analogRead(Redin); 
    gval = analogRead(Greenin);
    bval = analogRead(Bluein);
    yval = analogRead(Yellowin);
  }
    if (rval > 1000){
      digitalWrite(Red, HIGH);
      digitalWrite(Green, LOW);
      digitalWrite(Blue, LOW);
      digitalWrite(Yellow, LOW);
      digitalWrite(Alarm, LOW);
      delay(2000);
   
}
    else if (gval > 1000){
      digitalWrite(Red, LOW);
      digitalWrite(Green, HIGH);
      digitalWrite(Blue, LOW);
      digitalWrite(Yellow, LOW);
      digitalWrite(Alarm, LOW);
      delay(2000);
}
    else if (bval > 1000){
      digitalWrite(Red, LOW);
      digitalWrite(Green, LOW);
      digitalWrite(Blue, HIGH);
      digitalWrite(Yellow, LOW);
      digitalWrite(Alarm, LOW);
      delay(2000);
}
    else if (yval > 1000){
      digitalWrite(Red, LOW);
      digitalWrite(Green, LOW);
      digitalWrite(Blue, LOW);
      digitalWrite(Yellow, HIGH);
      digitalWrite(Alarm, LOW);
      delay(2000);
}
    else{
      digitalWrite(Red, LOW);
      digitalWrite(Green, LOW);
      digitalWrite(Blue, LOW);
      digitalWrite(Yellow, LOW);
      digitalWrite(Alarm, HIGH);
      delay (400);
      digitalWrite(Red, LOW);
      digitalWrite(Green, LOW);
      digitalWrite(Blue, LOW);
      digitalWrite(Yellow, LOW);
      digitalWrite(Alarm, LOW);
      delay (200);
      digitalWrite(Red, LOW);
      digitalWrite(Green, LOW);
      digitalWrite(Blue, LOW);
      digitalWrite(Yellow, LOW);
      digitalWrite(Alarm, HIGH);
}

}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 108
Posts: 6607
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i've been having some issues with the arduino uno.    i have written a code for it but the board keeps getting locked into a loop and running the alarm process regardless of what i do.   also for some reason the output marked red goes high whenever i disconnect the proxy, that is if the alarm doesn't continue to sound.   here is a sample of the code.  i thought it might be the board but i have bought two and they both do the same thing.  any suggestions?

All the outputs should stay LOW as long as the 'proxy' value is below 1000.  When you say "disconnect the proxy" are you saying that the A0 is left floating?  If you leave an input pin floating you will get random results.

Without knowing what voltages are being applied to the analog input pins it is impossible to know what to expect from the software.  How are the inputs wired?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 334
Posts: 36443
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
pval = analogRead(Proxy); //read proxy value and store it
 
  if (pval < 1000) {
The analogRead function returns a value between 0 and 1023. For the majority of the possible values, you are performing one action (turning pins off).

What, exactly, is connected to the Proxy pin? You obviously have a different interpretation of term proxy than I do.

What are connected to the Redin, Greenin, Bluein, and Yellowin pins?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

all of the in pins including the proxy are connected to a relay which is switching them between ground and 5 V  the problem is that the board tends to produce random results regardless of the situation.   I tested it on two different boards to i have to wonder if the problem is code based
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

also when i connect proxy high and also connect one of the inputs high the alarm continues to sound and the outputs for that color stay low
Logged

UK
Offline Offline
Tesla Member
***
Karma: 100
Posts: 6784
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not recognising the way you use terms like proxy.

Perhaps it would be best if you describe the bahaviour you want to achieve in terms of the Arduino's inputs and outputs, and then explain how the current behaviour differs from that.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok proxy and redin, bluein, etc are just the names i have given the pins to remind me what i have put in there.

proxy is connected to a relay which gives out 5v or ground based on the readings of a proximity sensor

all outputs should be low if the proxy is zero

once proxy goes high, the alarm should sound, unless one of the other inputs (redin, bluein, greenin, yellowin) is high

if one of those are high then the corresponding output to that input should go high and light the led attached to it.   

the problem is that regardless of proxy's state the alarm goes off.   sometimes it appears to function in that when proxy is low the alarm doesn't come on, however as soon as proxy goes high, the alarm goes off regardless of whether or not one of the other inputs is high and then even grounding the proxy again will not shut off the alarm
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 108
Posts: 6607
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The sketch seems to say what you want it to say.  I suspect that your wiring is the problem.

Exactly how are your relays connected to your inputs?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is a basic hand drawn schematic.... sorry about the poor drawing ability

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 108
Posts: 6607
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1M is too week for a pull-down resistor, particularly on an analog input.  Change it to 10k.

In the bottom diagram, what is that thing between the relay contacts and the LED?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so a 10 k will work better than a 1 megaohm?    oh that thing in between there is a buzzer..... i use it to make an audible alarm
Logged

Pages: [1]   Go Up
Print
 
Jump to: