Go Down

Topic: Serial Code is not working as expected  (Read 506 times) previous topic - next topic

pbhagwat22

Aug 19, 2019, 04:04 pm Last Edit: Aug 19, 2019, 04:07 pm by pbhagwat22
Hi,
following is the Piece of Code:

Code: [Select]
char incomingByte = 0;  
int psett[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int stat_p[16]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int chars[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int ardpins[16]={35,37,39,41,43,45,47,49,34,36,38,40,42,44,46,48};
int sett[16];
int i=0;
int d=0;
void setup()
{
  // put your setup code here, to run once:
        Serial.begin(9600);
        for(i=2;i<=12;i++)
        {
           analogWrite(i,0);  
        }
        for(i=34;i<=49;i++)
        {
           analogWrite(i,255);  
        }
}
void loop()
{
  
  delay(100);
  if (Serial.available() > 0)
       {
        incomingByte = Serial.read();
        //Serial.println(incomingByte);
        switch(incomingByte)
        {
          
          case 'O':
          analogWrite(7,255);
          analogWrite(5,255);
          delay(100);
          analogWrite(7,0);
          analogWrite(5,0);
          break;
          
          case 'X':
          analogWrite(6,255);
          analogWrite(4,255);
          delay(100);
          analogWrite(6,0);
          analogWrite(4,0);
          break;
          
          case 'K':
          analogWrite(9,255);
          delay(100);
          analogWrite(9,0);
          break;

          case 'k':
          analogWrite(8,255);
          delay(100);
          analogWrite(8,0);
          break;

          case 'L':
          analogWrite(3,255);
          delay(100);
          analogWrite(3,0);
          break;
                
          case 'l':      
          analogWrite(2,255);
          delay(100);
          analogWrite(2,0);
          break;
                  
          case 'M':    
          analogWrite(7,255);
          delay(100);
          analogWrite(7,0);
          break;
      
          case 'm':      
          analogWrite(6,255);
          delay(100);
          analogWrite(6,0);
          break;
          
          case 'N':    
          analogWrite(5,255);
          delay(100);
          analogWrite(5,0);
          break;
      
          case 'n':
          analogWrite(4,255);
          delay(100);
          analogWrite(4,0);
          break;
      
          case 'c':
          Serial.println("Arduino Communicating");
          break;

          case 'v':
          st=""
          for(i=0;i<=sizeof(chars);i++)
          {
            
          }
          break;
          
        }
        for(i=0;i<=sizeof(chars);i++)
        {
           if(incomingByte==chars[i])
          {
             if(psett[i]==0)
             {
              analogWrite(ardpins[i],0);
              sett[i]=1;
             }
             if(psett[i]==1)
             {
              analogWrite(ardpins[i],255);
              sett[i]=0;
             }
             break;
          }
        }
        for(i=0;i<=sizeof(psett);i++)
        {
          psett[i] = sett[i];
        }
       }

}


After flashing this in serial terminal, when i enter "v", I am not getting the output and I had to enter is twice. Also if I uncomment "Serial.println(incomingByte);" line Code is executing weirdly.

Board: Arduino Mega
Version of Arduino Software: 1.8.5

Please help
Thank you

TheMemberFormerlyKnownAsAWOL

Don't you mean 
Code: [Select]
for(i=0;i<sizeof(chars);i++)

?

uxomm

Or maybe:

Code: [Select]
for (i=0; i<sizeof(chars) / sizeof(chars[0)]; i++)

:)
Always decouple electronic circuitry.

pbhagwat22

ok I modified it and now my code looks something like this,

Code: [Select]


char incomingByte = 0; 
int psett[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int stat_p[16]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int chars[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int ardpins[16]={35,37,39,41,43,45,47,49,34,36,38,40,42,44,46,48};
int sett[16];
int i=0;
int d=0;
void setup()
{
  // put your setup code here, to run once:
        Serial.begin(9600);
        for(i=2;i<=12;i++)
        {
           analogWrite(i,0); 
        }
        for(i=34;i<=49;i++)
        {
           analogWrite(i,255); 
        }
}
void loop()
{
 
  delay(100);
  if (Serial.available() > 0)
       {
        incomingByte = Serial.read();
        Serial.println(incomingByte);//Changed line##############################
        switch(incomingByte)
        {
         
          case 'O':
          analogWrite(7,255);
          analogWrite(5,255);
          delay(100);
          analogWrite(7,0);
          analogWrite(5,0);
          break;
         
          case 'X':
          analogWrite(6,255);
          analogWrite(4,255);
          delay(100);
          analogWrite(6,0);
          analogWrite(4,0);
          break;
         
          case 'K':
          analogWrite(9,255);
          delay(100);
          analogWrite(9,0);
          break;

          case 'k':
          analogWrite(8,255);
          delay(100);
          analogWrite(8,0);
          break;

          case 'L':
          analogWrite(3,255);
          delay(100);
          analogWrite(3,0);
          break;
               
          case 'l':     
          analogWrite(2,255);
          delay(100);
          analogWrite(2,0);
          break;
                   
          case 'M':     
          analogWrite(7,255);
          delay(100);
          analogWrite(7,0);
          break;
     
          case 'm':     
          analogWrite(6,255);
          delay(100);
          analogWrite(6,0);
          break;
         
          case 'N':     
          analogWrite(5,255);
          delay(100);
          analogWrite(5,0);
          break;
     
          case 'n':
          analogWrite(4,255);
          delay(100);
          analogWrite(4,0);
          break;
     
          case 'c':
          Serial.println("Arduino Communicating");
          break;

          case 'v':
          d=digitalRead(35);
          Serial.print(d);
          break;
         
        }
        for(i=0;i<sizeof(chars);i++)//Changed line##############################
        {
           if(incomingByte==chars[i])
          {
             if(psett[i]==0)
             {
              analogWrite(ardpins[i],0);
              sett[i]=1;
             }
             if(psett[i]==1)
             {
              analogWrite(ardpins[i],255);
              sett[i]=0;
             }
             break;
          }
        }
        for(i=0;i<sizeof(psett);i++)//Changed line##############################
        {
          psett[i] = sett[i];
        }
       }

}



refer to "//Changed line##############################" for changed lines.
Now, according to the code Every time i pass a character, Serial terminal should display it.
But, I have to pass any character twice for it to display.

I am not sure why is that?

cattledog

Code: [Select]
for(i=0;i<sizeof(chars);i++)
for(i=0;i<sizeof(psett);i++)


You did not follow the advice of reply #2

Your arrays are defined as int, and sizeof is returning 32. You are overrunning your array bounds. The array indexes are 0-15.


TheMemberFormerlyKnownAsAWOL

...and then ask yourself why you decided to define array containing chars as an array of ints.

pbhagwat22

I am sorry,

It was my mistake.

My problem is solved. Thank you Guys for your support.

Go Up