Go Down

Topic: Serial interrupt (Read 3154 times) previous topic - next topic

Arrch


here is my whole code. this is killing me. i just migrated from PIC to arduino.


You missed this part:

Quote
what you're expecting it to do, and what it is doing.


...And never use the phrase "it's not working" on the forums.

Rolandchok

i just want to switch a led on and off using a "switch case" statement. but its not switching at all. 1st time to encounter this kind of problem.

in this part when i use the digitalwrite works fine but with switch case statement  led not switching state.

else if (inchar == '*')
        {
          pcbuffer = '*';
         // digitalWriteTWVDC1,!digitalRead(TWVDC1));
          portswitch();
        }



Code: [Select]
byte TWVDC1 = 35;              // 12VDC 1
byte TWVDC2 = 36;              // 12VDC 2
byte TWVDC3 = 37;              // 12VDC 3
byte TFVDC1 = 38;              // 24VDC 1
byte TFVDC2 = 39;              // 24VDC 2
byte TFVDC3 = 40;              // 24VDC 3
byte TFVDC4 = 41;              // 24VDC 4
int pcbuffer[2];               // data receive from PC
boolean stopreceive = false;
int i = 0;
int sonarPIN1 = A0;
short sonardata1;
int TxDataBuffer[50];

void setup()
{
   pinMode(TWVDC1, OUTPUT);
   pinMode(TWVDC2, OUTPUT);
   pinMode(TWVDC3, OUTPUT);
   pinMode(TFVDC1, OUTPUT);
   pinMode(TFVDC2, OUTPUT);
   pinMode(TFVDC3, OUTPUT);
   Serial.begin(1200);
   TxDataBuffer[0] = '$';
   interrupts();
   Serial.write("USART initiallize");
   digitalWrite(pc,HIGH);
   delay(500);
   digitalWrite(pc,LOW);
}

void loop()
{
 int n = 0;
 ultrasonic1();
 while (Serial.available())
     {
       pcreceivedata();        // if data is receive on the serial buffer check it
       
     }
 if ( pcbuffer[2] == '*')
   {
      do
         {
           Serial.write(pcbuffer[n]);
           n++;
         }while(n != 3);
      i = 0;
    }
}

void pcreceivedata()
{
    char inchar = Serial.read();
    if (inchar == '$')
        {
          i = 1;
          pcbuffer[0] = '$';
        }
    else if (inchar == '*')
        {
          pcbuffer[i] = '*';
         // digitalWrite(pc,!digitalRead(pc));
          portswitch();
        }
    else
        {
          pcbuffer[i] = inchar;
          i++;
        }
}

void portswitch()
{
  switch (pcbuffer[1])
    {      
        case 0xB0:
          digitalWrite(TWVDC1,!digitalRead(TWVDC1));
          break;
       case 0xB1:
          digitalWrite(TWVDC2,!digitalRead(TWVDC2));
          break;        
       case 0xB2:
          digitalWrite(TWVDC3,!digitalRead(TWVDC3));
          break;
       case 0xB6:
          digitalWrite(TFVDC1,!digitalRead(TFVDC1));
          break;
       case 0xB7:
          digitalWrite(TFVDC2,!digitalRead(TFVDC2));
          break;
       case 0xB8:
          digitalWrite(TFVDC3,!digitalRead(TFVDC3));
          break;
       case 0xB9:
          digitalWrite(TFVDC4,!digitalRead(TFVDC4));
          break;
    }
}

void ultrasonic1()
{    
 sonardata1 = analogRead(sonarPIN1);
 sonardata1 = map(sonardata1, 0, 1023, 0, 255);
 TxDataBuffer[1] = sonardata1;
}

Arrch


i just want to switch a led on and off using a "switch case" statement. but its not switching at all. 1st time to encounter this kind of problem.


Are your sure those case values are correct? Those are some pretty high ASCII values. What are you putting in between your '$' and '*'?

Try adding a serial print in there so you can see the hex value of the character you are checking:

Code: [Select]
void portswitch()
{
  Serial.print("pcbuffer[1]: "); Serial.println(pcbuffer[1], HEX);
  switch (pcbuffer[1])
  ...
}

Rolandchok

its not jumping to the switch case statement. not printing anything.

Arrch


its not jumping to the switch case statement. not printing anything.

Then move the Serial prints to where you have the digitalwrite that was working.

Rolandchok

i'm getting 0 on the pcbuffer[1] why is that but on the serial its sending it reads 0xA0 it does not make any sense.

Rolandchok

got it figured out.

pcbuffer is delared as "int" it should be a "byte".

Rolandchok

@ARRch   THank you so much for your effort on helping me out. i really appreciate it.  XD   i ow you one.

Go Up