Incremental index doesn't change at end of for loop - arduino

I want to interface Arduino with PLC to pull some information.
My problem is at Function 3: Set alarm flag / reset flag. This function is used to compare history value and present value. I tried to process some integer number (test_number) and process like binary 16 bits data for finding 1 at some bit. I found the for loop in Findbit function, which should repeat 16 times, runs infinitely. It does not change the incremental index (variable name bit_1) which is still stuck at 1.
This is my code :
int test_number_array[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int test_number = 0;
int bit_1 = 0;
int Andbit = 0;
const char* message;
int flagAlarm[2][16] = {};
int flagReset[2][16] = {};
void setup()
{
// put your setup code here, to run once:
Serial.begin( 9600 );
}
void loop()
{
// put your main code here, to run repeatedly:
for (int j = 1; j <= 2; j++)
{
for (int i = 1; i <= 2; i++) // Example with 2 modbus address
{
unsigned int address = 40000 + i;
Serial.print ("Modbus address = ");
Serial.println(address, DEC);
pull_data(i);
Serial.print("Test number is ");
Serial.println(test_number);
Findbit(i);
Serial.println("------------------------------------------------- ");
}
}
while (1)
{
}
}
// ---------------Function 1 : Function finding alarm bit-----------------//
void Findbit(int i)
{
for (bit_1 = 0; bit_1 <= 15; bit_1++)
{
Andbit = test_number & 1;
Serial.print("Test number (BINARY) is ");
Serial.println(test_number, BIN);
Serial.print("Check at bit number ");
Serial.println(bit_1);
Serial.print("And bit is ");
Serial.println(Andbit, BIN);
Serial.print("flagAlarm(Before1) = ");
Serial.println(flagAlarm[i][bit_1]);
Serial.print("flagreset(Before1) = ");
Serial.println(flagReset[i][bit_1]);
if (Andbit == 1) //found "1" pass into loop
{
flagAlarm[i][bit_1] = 1;
}
else
{
}
Serial.print("flagAlarm(Before2) = ");
Serial.println(flagAlarm[i][bit_1]);
Serial.print("flagreset(Before2) = ");
Serial.println(flagReset[i][bit_1]);
Set_reset_flag(i,bit_1);
test_number = test_number >> 1;
Serial.print("flagAlarm(After) = ");
Serial.println(flagAlarm[i][bit_1]);
Serial.print("flagreset(After) = ");
Serial.println(flagReset[i][bit_1]);
Serial.println(" ");
}
}
// -----------------------Function 2 : Pull data------------------------- //
int pull_data(int i)
{
i = i - 1;
test_number = test_number_array[i];
return test_number;
}
// -------------Function 3 : Set alarm flag / reset flag ---------------- //
void Set_reset_flag(int i, int bit_1)
{
Serial.print("i = ");
Serial.println(i);
Serial.print("bit_1 = ");
Serial.println(bit_1);
if (flagAlarm[i][bit_1] == 1 && flagReset[i][bit_1] == 0)
{
Serial.print("Alarm at bit ");
Serial.println(bit_1);
flagAlarm[i][bit_1] = 0;
flagReset[i][bit_1] = 1;
}
else if (flagAlarm[i][bit_1] == 0 && flagReset[i][bit_1] == 1)
{
Serial.print("Reset Alarm at bit ");
Serial.println(bit_1);
flagReset[i][bit_1] = 0;
}
else if (flagAlarm[i][bit_1] == 1 && flagReset[i][bit_1] == 1)
{
Serial.print("Alarm still active at bit ");
Serial.println(bit_1);
flagAlarm[i][bit_1] = 0;
flagReset[i][bit_1] = 1;
}
else
{
}
}

Could it be that your bit_1 variable is modified from some other code not mentioned here, or get optimized at all? Also, is it necessary to make a loop counter a global variable? Can you declare it inside the Findbit function?

Related

Arduino read big amount of serial data without losses

In Arduino Serial I need to read those kind of lines <1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16> As fast
as possible, without blocking.
The goal is to set values of array compensation[] without delay.
In this example <1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16> Would display 1000 (as 100% have been read) But I don't get 100% but periodique losses at 96% and then 95% … So I need my code to display 1000 (for 100%) every time.
This is what I have already :
static int compensastion[64];
int passage=1;
const byte numChars = 1000;
char receivedChars[numChars];
boolean newData = false;
int score = 0;
int mal_score = 0;
void setup() {
Serial.begin(115200);
Serial.println();
}
void loop() {
read_inputs();
// get the percentage of success for this simple message from 1 to 16
Serial.print(double(double(score) / double(mal_score+score))*10000);
Serial.println();
}
void read_inputs(){
recvWithStartEndMarkers();
check();
}
// Get the full line <1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16>
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
if (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0';
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
// Parse the line in digits
void check(){
if (newData == true) {
newData = false;
int marge = 0;
passage=1;
for(int j = 0; j < numChars; j=j+1){
int my_int = -1;
char buf[4];
if(isDigit(receivedChars[j]) &&marge == 0 ){
if(!isDigit(receivedChars[j+1])){
my_int = receivedChars[j] - '0';
marge=marge+1;
}
if(isDigit(receivedChars[j+1]) && !isDigit(receivedChars[j+2]) ){
my_int = receivedChars[j] - '0';
my_int = my_int*10 + (receivedChars[j+1] - '0');
marge=marge+2;
}
if(isDigit(receivedChars[j+1]) && isDigit(receivedChars[j+2]) && !isDigit(receivedChars[j+3]) ){
my_int = receivedChars[j] - '0';
my_int = my_int*10 + (receivedChars[j+1] - '0');
my_int = my_int*10 + (receivedChars[j+2] - '0');
marge=marge+3;
}
}
if (isDigit(receivedChars[j]) && my_int != -1){
if(passage == my_int){
score=score+1;
}else{
mal_score= mal_score+1;
}
compensastion[(passage-1)*2] = my_int;
passage=passage+1;
if(passage+1 >= 16+2){
passage=1;
}
}
if(marge > 0){
marge=marge-1;
}
}
}
}
In my experience, one of the most effective ways to handle huge amounts of serial data is using Interrupts and Finite State Machines (FSM).
Please carefully read this guide by the amazing Nick Gammon. FSMs are the most versatile approach when dealing with intensive processing tasks. (Not to be confused with RTOS, though)
Regarding the serial communication, I would implement the serial reading through the serialEvent() interrupt along the lines of the following example:
char serialString[] = " "; // Empty serial string variable
bool stringFinished = false; // Flag to indicate reception of a string after terminator is reached
void setup(){
previousEncoderTime = 0;
}
void loop(){
unsigned long now = millis();
if (stringFinished){ // When the serial Port has received a command
stringFinished = false;
// Implement your logic here
}
}
void serialEvent()
{
int idx = 0;
while (Serial.available())
{
char inChar = (char)Serial.read();
if (inChar == '\n') // The reading event stops at a new line character
{
serialTail = true;
serialString[idx] = inChar;
}
if (!serialTail)
{
serialString[idx] = inChar;
idx++;
}
if (serialTail)
{
stringFinished = true;
Serial.flush();
serialTail = false;
}
}
}
Finally, try to compartmentalize all the necessary data processing in single-purpose functions. This way your code will have a proper flow and you will reduce the risk of blocking the system with heavy tasks.
EDIT: I forgot to mention that the Serial.available() is regarded as a polling method which is highly unreliable and time-consuming. Stick to interrupts and you won't have massive issues

How can I get out of the loop?

I'd like to get out of the loop in my Arduino project. Currently I am programming a digital watch, everything works just fine but I wanted to add options menu by clicking a button, but after clicking it nothing pops up even if I have something inside the code. Take a look at it. I don't know how to write it better. If you have some ideas, please you could rewrite some parts of the code and explain why you did so. Thanks forwardly.
Please do not mind s = s + 1, I wanted it like that.
#include "LiquidCrystal.h"
#include <EEPROM.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int h = 0;
int m = 0;
int s = 0;
int right = 8;
int left = 9;
int buttonStateLeft = 0;
String when;
uint8_t EEPROMaddress_sec = 1;
uint8_t EEPROMaddress_min = 2;
uint8_t EEPROMaddress_hour = 3;
bool clockShown = true;
bool menuShown = false;
void setup()
{
lcd.begin(16,2);
pinMode(right, INPUT);
pinMode(left, INPUT);
}
void loop()
{
if(menuShown)
{
lcd.setCursor(0,0);
lcd.print("jozo je kkt");
delay(200);
}
if(clockShown) {
lcd.setCursor(0,0);
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}
s = EEPROM.read(EEPROMaddress_sec);
m = EEPROM.read(EEPROMaddress_min);
h = EEPROM.read(EEPROMaddress_hour);
s = s + 1;
if(h > 12)
when = "PM";
if(h < 12)
when = "AM";
if(h == 12)
when = "PM";
lcd.print("Cas: ");
if(h<10)lcd.print("0");
lcd.print(h);
lcd.print(":");
if(m<10)lcd.print("0");
lcd.print(m);
lcd.print(":");
if(s<10)lcd.print("0");
lcd.print(s);
lcd.print(" ");
lcd.print(when);
if(s == 60)
{
s = 0;
m = m+1;
}
if(m == 60)
{
s = 0;
m = 0;
h = h+1;
}
if(h == 24)
{
m = 0;
s = 0;
h = 0;
}
EEPROM.write(EEPROMaddress_sec, s);
EEPROM.write(EEPROMaddress_min, m);
EEPROM.write(EEPROMaddress_hour, h);
delay(1000);
}
}
In order to do that you will have to use Interrupts, note that you must connect your button to an interrupt pin (not every pin is an interrupt pin) you can google "what are the interrupt pins of 'your_card_name' ", the code would have to change, you can follow these :
In the setup function replace :
pinMode(left, INPUT);
by :
attachInterrupt(digitalPinToInterrupt(left), switchMode, RISING);
add this function before setup(){...}
int lastPressTime=millis();
void switchMode(){ // function called when the button is pressed
if((millis()-lastPressTime)>60){ // for debouncing
clockShown = false;
menuShown = true;
lcd.clear();
lastPressTime=millis();
}
}
and remove this part from your code : (the one in the loop() function)
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}

Sending message to the extracted number

Please someone let me know why this code is not working. I want to extract message sender's number and then forward message to it using AT commands. It extracts the number of sender and stores it in a variable but why won't it send a message to that number?
#include <GSM.h>
GSM_SMS sms;
char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;
char number1[12] = "xxxxxxxxxx";
String number;
char inchar;
char outString[22];
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial1.print("ATE0\r");
Serial1.print("AT\r");
Serial1.print("AT+CMGF=1\r");
Serial1.print("AT+CNMI=1,2,0,0,0\r");
delay(1000);
}
void loop()
{
recSms();
}
void recSms()
{
if(Serial1.available())
{
char data = Serial1.read();
if(data == '+'){RcvdCheck = 1;}
if((data == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
if((data == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
if((data == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
if(RcvdCheck == 4){RcvdConf = 1; RcvdCheck = 0;}
if(RcvdConf == 1)
{
if(data == '\n'){RcvdEnd++;}
if(RcvdEnd == 3){RcvdEnd = 0;}
RcvdMsg[index] = data;
index++;
if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
if(RcvdConf == 0)
{
Serial.print("Mobile Number is: ");
for(int x = 4;x < 17;x++)
{
number+=RcvdMsg[x];
MsgMob[x-4] = RcvdMsg[x];
}
Serial.print(number);
Serial.println();
Serial.print("Message Text: ");
for(int x = 46; x < MsgLength; x++)
{
MsgTxt[x-46] = RcvdMsg[x];
inchar=MsgTxt[x-46];
}
Serial.print(inchar);
Serial.println();
RcvdCheck = 0;
RcvdConf = 0;
index = 0;
RcvdEnd = 0;
MsgMob[15];
MsgTxt[50];
MsgLength = 0;
Serial.flush();
Serial1.flush();
if(inchar == '#')
{
sendInfo();
}
}
}
}
}
void sendInfo()
{
Serial1.print("AA");
delay(1000); //delay of 1
Serial1.println("AT");
delay(1000);
Serial1.write("AT+CMGF=1\r\n"); //set GSM to text mode
delay(1000);
Serial1.write("AT+CPMS=\"SM\"\r\n"); //Preferred SMS Message Storage
delay(1000);
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
delay(1000);
Serial1.print("HI");
delay(1000);
Serial1.write(0x1A); // sends ctrl+z end of message
delay(1000);
Serial.println("sms sent ");
} //end sendInfo()
Okay so the problem seems to be with these lines:
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
But if we write the lines written below, program works just fine!
Serial1.write("AT+CMGS=\"");
Serial1.print(number);
Serial1.write("\"\r");

Unexpected behavior in my RGB-strip driver code

I'm getting wrong output on the pins 9, 10 and 11. They are meant to be inputs for my RGB strip driver circuit. Which is basically an array of NPN transistors pulling current from the strip.
The basic idea is to get only 2 controls to set R, G, B and brightness. I'm using a button and a potenciometer. Potenciometer is used to set the value and the button to skip to next value setting. There is one setting state which is like the default one. It is the one to set the brightness and I will be using this most of the time. The othe ones are for setting the colors and when in one of those setting the strip will be blinking in between only the color I'm currently setting and the result with alle three colors set. The whole code was working just fine until I added the brightness setting and I think that I am kinda lost in my own code.
I even added a serial to read the outputs but I don't understand why are the numbers the way they are :(
int pinR = 9;
int pinG = 10;
int pinB = 11;
int potPin = A0;
const int buttonPin = 2;
int brightR = 0;
int brightG = 0;
int brightB = 0;
int brightness = 50; //
int R;
int G;
int B;
int potValue = 0;
int blinky = 0;
boolean blinking = false;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
int setting = 0; //0=R 1=G 2=B 3=Brightness
void setup() {
// put your setup code here, to run once:
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
Serial.begin(9600);
}
void RGBset(int r, int g, int b){
analogWrite(pinR, r);
analogWrite(pinG, g);
analogWrite(pinB, b);
}
void loop() {
// put your main code here, to run repeatedly:
potValue = analogRead(potPin);
potValue = map(potValue, 0, 1023, 0, 255); //read pot --> map to values from 0 - 255
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
setting++;
}
}
}
lastButtonState = reading;
if(setting > 3){ // 0=R 1=G 2=B 3=Brightness
setting = 0; // cant get 4 cause there is no 4
}
if(setting == 0){
brightR = potValue;
if(blinking){
RGBset(brightR, brightG, brightB);
}else{
RGBset(brightR, 0, 0);
}
}
if(setting == 1){
brightG = potValue;
if(blinking){
RGBset(brightR, brightG, brightB);
}else{
RGBset(0, brightG, 0);
}
}
if(setting == 2){
brightB = potValue;
if(blinking){
RGBset(brightR, brightG, brightB);
}else{
RGBset(0, 0, brightB);
}
}
if(setting == 3){
brightness = potValue;
brightness = map(brightness, 0, 255, 1, 100); //mapping brightness to values from 1 - 100
R = brightR * brightness / 100; //set value * brightness / 100
G = brightG * brightness / 100; //that leads to get % of set value
B = brightB * brightness / 100; //255 * 50 / 100 = 127,5 ==> 128
RGBset(R, G, B); //it wont blink in thiss setting
}
if(setting != 3){
blinky++;
if(blinky > 1000){
blinking = !blinking;
blinky = 0;
}
}
String output = (String(brightR) + " " + String(R) + " " + String(brightG) + " " + String(G) + " " + String(brightB) + " " + String(B) + " " + String(brightness) + " " + String(potValue) + " " + String(blinking));
Serial.println(output);
delay(1);
}
First, in setup:
pinMode(buttonPin , INPUT);
Second, what are you expected when setting==3? You aren't reloading/updating the variables for brightR brightG brightB. So, when you change setting, you will lost the change of brightness

arduino nothing printing to serial

I've built an arduino sketch that attempts to do a couple of different lengths of windowing and some simple calculations (mean/variance) on the analog values from a couple of sensors. Previously I had the same code for 1 sensor working as intended but the code below has been expanded with a bunch of little for loops so that everything should run for both sensors now.
I simply can't get anything at all to print to serial - even the two serial prints i put in setup and start of loop just to debug - yet the code compiles and uploads without any errors or warnings.
I apologise for including the whole sketch, I couldn't think how to break it apart to show.
long int currentTime = 0;
long int stopTime[2] = {0,0};
long int shortWindowTime = 0;
int shortVal[2][40];
int reflexWindowStart = 0;
int reflexWindowTime = 0;
int reflexVal[2][500];
int mean[2] = {0,0};
unsigned int variance[2] = {0,0};
int lowVal[2] = {0,0};
int peakVal[2] = {0,0};
int lowIndex[2] = {0,0};
int peakIndex[2] = {0,0};
int stopIndex[2] = {0,0};
boolean stopped[2] = {false,false};
void setup(){
Serial.begin(9600);
Serial.println("wtf?");
for(int i=0;i<2;i++){
for(int j=0;j<40;j++){
shortVal[i][j] = 0;
}
for(int j=0;j<500;j++){
reflexVal[i][j] = 1023;
}
}
}
void loop() {
Serial.println("wtf?");
currentTime = micros();
if(currentTime - shortWindowTime > 500){
shortWindowTime = currentTime;
writeShortWindow();
meanVariance();
if(reflexWindowStart == 0){
reflexWindow();
}
reflexWindowStart++;
if(reflexWindowStart > 9){
reflexWindowStart = 0;
}
}
}
void writeShortWindow(){
for(int i=0;i<2;i++){
for(int j=39; j>0; j--){
shortVal[i][j] = shortVal[i][j-1];
}
int ground = analogRead(A5);
shortVal[0][0] = analogRead(A1);
analogRead(A5);
shortVal[i][0] = analogRead(A2);
}
}
void meanVariance(){
for(int i=0;i<2;i++){
for(int j=0; j<39; j++){
mean[i] = mean[i] + shortVal[i][j];
}
mean[i] = mean[i] / 40;
for(int j=0; j<39; j++){
variance[i] = variance[i] + sq(mean[i] - shortVal[i][j]) ;
}
variance[i] = variance[i] / 40;
}
}
void reflexWindow(){
for(int i=0;i<2;i++){
if(stopped[i] == true){
if((millis() - stopTime[i] > 20) && (peakVal[i] - shortVal[i][0] > 20) && (variance[i] <= 1)){
stopped[i] = false;
stopIndex[i] = 0;
Serial.println("................................NOTstopped");
}
}
}
for(int i=0;i<2;i++){
if(stopped[i] == false){
lowVal[i] = 1023;
peakVal[i] = 0;
for(int j=stopIndex[i]; j>0; j--){
reflexVal[i][j] = reflexVal[i][j-1];
if(reflexVal[i][j] < lowVal[i]){
lowVal[i] = reflexVal[i][j];
lowIndex[i] = j;
}
}
reflexVal[i][0] = shortVal[i][0];
for(int j=lowIndex[i]; j>=0; j--){
if(reflexVal[i][j] > peakVal[i]){
peakVal[i] = reflexVal[i][j];
}
}
}
}
for(int i=0;i<2;i++){
if(stopped[i] == false){
if(peakVal[i] - lowVal[i] >= 50){
Serial.print(i);
Serial.println("...................................stopped");
stopTime[i] = millis();
stopped[i] = true;
}
}
}
for(int i=0;i<2;i++){
if(stopIndex[i] < 499){
stopIndex[i]++;
}
}
Serial.print(shortVal[0][0]);
Serial.print(" ... ");
Serial.print(lowVal[0]);
Serial.print(" ... ");
Serial.print(peakVal[0]);
Serial.print(" ........ ");
Serial.print(shortVal[1][0]);
Serial.print(" ... ");
Serial.print(lowVal[1]);
Serial.print(" ... ");
Serial.println(peakVal[1]);
}
If you have a Leonardo board you will most likely not see the Serial.print in the setup function.
Try changing your setup to this (notice the extra while loop waiting for the Serial)
void setup(){
Serial.begin(9600);
while (!Serial);
Serial.println("wtf?");
for(int i=0;i<2;i++){
for(int j=0;j<40;j++){
shortVal[i][j] = 0;
}
for(int j=0;j<500;j++){
reflexVal[i][j] = 1023;
}
}
}
The reason behind this you can read in the Arduino docs for Leonardo http://arduino.cc/en/Guide/ArduinoLeonardo#toc3 but in short is that Leonardo doesnt resets the serial port when opening the serial stream.
Have you checked your BAUD rate in serial monitor, because if it is different, it will not show anything.
be sure the one in serial monitor and in Serial.begin(<BAUD rate here>) is the same

Resources