Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Why this Arduino code doesn't work as expected?

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
I have to receive data from UART and turn on Relays but they are not functioning as expected. The relays turn ON and then turn OFF after 2 or 3 seconds.

Here is my Mega 2560 code. phasex, neurtralx are the relays.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <avr/interrupt.h> 
#include <avr/io.h>
 
String inputString;
boolean stringComplete = false;
 
int phase1 = 22;
int neutral1 = 23;
 
int phase2 = 24;
int neutral2 = 25;
 
int phase3 = 26;
int neutral3 = 27;
 
const int analogInPin1 = A0;
int sensorValue1 = 0;
 
const int analogInPin2 = A1;
int sensorValue2 = 0;
 
const int analogInPin3 = A2;
int sensorValue3 = 0;
 
int led1 = 28;
int led2 = 29;
int led3 = 30;
 
int PIR = 48;
int BEEPER = 49;
 
int emergencyStop = 42;
 
int UartLed = 13;
 
void setup() {                
  pinMode(phase1, OUTPUT);   
  pinMode(neutral1, OUTPUT);   
  pinMode(phase2, OUTPUT);   
  pinMode(neutral2, OUTPUT); 
  pinMode(phase3, OUTPUT);   
  pinMode(neutral3, OUTPUT);
 
  pinMode(BEEPER, OUTPUT);
  pinMode(PIR, INPUT);
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  
  pinMode(emergencyStop, INPUT);
  
  pinMode(UartLed, OUTPUT);
  
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(UartLed, LOW);
  digitalWrite(phase1, LOW);
  digitalWrite(phase2, LOW);
  digitalWrite(phase3, LOW);
  digitalWrite(neutral1, LOW);
  digitalWrite(neutral2, LOW);
  digitalWrite(neutral3, LOW);
  
  UBRR0H = 0; // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 
  UBRR0L = 8; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 
  UCSR0C |= (1 << UCSZ00) | (1 << UCSZ10); // Use 8-bit character sizes 
  UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);   // Turn on the transmission, reception, and Receive interrupt      
  interrupts();
  
  Serial.begin(9600);
  inputString.reserve(10);
}
 
void loop() {
  
  sensorValue1 = analogRead(analogInPin1);            
  // map it to the range of the analog out:
  sensorValue1 = sensorValue1 / 2.046;
  
  
  sensorValue2 = analogRead(analogInPin2);            
  // map it to the range of the analog out:
  sensorValue2 = sensorValue2 / 2.046;
  
  sensorValue3 = analogRead(analogInPin3);            
  // map it to the range of the analog out:
  sensorValue3 = sensorValue3 / 2.046;
  
  Serial.println(sensorValue1);
  Serial.println(sensorValue2);
  Serial.println(sensorValue3);
  
  if(sensorValue1 >= 32.0) {
        digitalWrite(led1, HIGH);
        digitalWrite(phase1, LOW);
        digitalWrite(neutral1, LOW);    
  }
  else if (sensorValue1 < 32.0) {
        digitalWrite(led1, LOW);
  }
  
  if(sensorValue2 >= 32.0) {
        digitalWrite(led2, HIGH);
        digitalWrite(phase2, LOW);
        digitalWrite(neutral2, LOW);    
  }
  else if (sensorValue2 < 32.0) {
        digitalWrite(led2, LOW);
  }
  
  if(sensorValue3 >= 32.0) {
        digitalWrite(led3, HIGH);
        digitalWrite(phase3, LOW);
        digitalWrite(neutral3, LOW);    
  }
  else if (sensorValue3 < 32.0) {
        digitalWrite(led3, LOW);
  }
  
  
  int reading = digitalRead(PIR);
 
  if(reading == HIGH) {
        beep(500);
        beep(500);
        beep(500);                
  }
  
  
  int reading1 = digitalRead(emergencyStop);
 
  if(reading1 == HIGH) {
        digitalWrite(phase1, LOW);
        digitalWrite(neutral1, LOW);
        digitalWrite(phase2, LOW);
        digitalWrite(neutral2, LOW);
        digitalWrite(phase3, LOW);
        digitalWrite(neutral3, LOW);    
  }
  
  
 if(stringComplete) {          
      if(inputString == "1") {
            digitalWrite(phase1, HIGH);
            digitalWrite(neutral1, HIGH);      
            delay(50);
      }
      else if(inputString == "2") {
            digitalWrite(phase1, LOW);
            digitalWrite(neutral1, LOW);      
            delay(50);
      }
      else if(inputString == "3") {
            digitalWrite(phase2, HIGH);
            digitalWrite(neutral2, HIGH);      
            delay(50);
      }
      else if(inputString == "4") {
            digitalWrite(phase2, LOW);
            digitalWrite(neutral2, LOW);      
            delay(50); 
      }
      else if(inputString == "5") {
            digitalWrite(phase3, HIGH);
            digitalWrite(neutral3, HIGH);      
            delay(50);
      }
      else if(inputString == "6") {
            digitalWrite(phase3, LOW);
            digitalWrite(neutral3, LOW);      
            delay(50);              
      }
      
      digitalWrite(UartLed, HIGH); 
      delay(300);
      digitalWrite(UartLed, LOW);
      delay(300);
      
      stringComplete = false;
      inputString = "";
 
      digitalWrite(phase1, HIGH); 
      delay(300);
      digitalWrite(phase1, LOW);
      delay(300);      
  }
       
}
 
 
 
//ISR(USART0_RX_vect) {
void serialEvent() {
  if (Serial.available()) {
    
    char inChar = (char)Serial.read();
   
    if ((inChar == '\r') || (inChar == '\n')) {
      inputString += '\0';
      stringComplete = true;
    }
    else inputString += inChar;
    
    
    
     
  }
}
 
void beep(unsigned char delayms) {
  analogWrite(BEEPER, 20);      // Almost any value can be used except 0 and 255
                           // experiment to get the best tone
  delay(delayms);          // wait for a delayms ms
  analogWrite(BEEPER, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms   
}



- - - Updated - - -

Now it is working for UNO. I modified the code for UNO R3. Now it only fires once on interrupt. How to clear the serial interrupt in Arduino?
 

Milan.Rajik,

Which code line turn on the delay? I do not understand it.

Thanks.
 

The delays are working properly. There is no delay to turn OFF Relays. The relays are getting auto turned OFF after 2 or 3 seconds after they Turrn ON when "1", "3", "5" etc... are sent through UART.
 

Sorry, I do not want to write DELAY, I want to write RELAY.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top