Case structure problem .... DUE

Hi everybody,

Iam doing a little project to communicate between labview and arduino, which worked fine so far.
labview sends a character which starts a case function on the arduino. Now I want to enlarge my project to multiple pins... But the programming is still as simple as before....

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

pinMode(19, OUTPUT);
pinMode(17, OUTPUT);
pinMode(15, OUTPUT);
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);

digitalWrite(19, LOW);
digitalWrite(17, LOW);
digitalWrite(15, LOW);
digitalWrite(0, LOW);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(7, LOW);
}

void loop() {

if (Serial.available() > 0) {

int inByte = Serial.read();
switch (inByte) {

case 'w':
pinMode(19, HIGH);
pinMode(17,HIGH);
pinMode(15, HIGH);
pinMode(0, HIGH);
pinMode(2, HIGH);
pinMode(4, HIGH);
pinMode(7, HIGH);
Serial.write('W');

break;

case 'q':
digitalWrite(19, LOW);
digitalWrite(17, LOW);
digitalWrite(15, LOW);
digitalWrite(0, LOW);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(7, LOW);

delay(100);

Serial.write('C');
break;

}
}
//yield();
}

Somehow it doesen´t work at all. Even the "Serial.wirte" doesn´t work.
Do I use too many pins at a time or is there another problem which i didn´t see ?
Next problem will be that the code will be much longer with even more pins....

I like the case function as the communication is very simple... if there is a better solution please give me a hint :slight_smile:

goal: easy communictaion between arduino and labview and controlling as much as possible pins at once.

   case 'w':
   pinMode(19, HIGH);
    pinMode(17,HIGH);
       pinMode(15, HIGH);
     pinMode(0, HIGH);
   pinMode(2, HIGH);
     pinMode(4, HIGH);
  pinMode(7, HIGH);
        Serial.write('W');

OUTPUT or INPUT are more usually used with pinMode.

Please use code tags when posting code.

Oh my fault ... one of my countless "try and fail" codes....
changed everything but still doesen´t work.

I canceld out everythin instat of just one single pin ... works fine ... but not with all at once ...

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 
pinMode(19, OUTPUT);
  /*  pinMode(17, OUTPUT);
   pinMode(15, OUTPUT);
   pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
   pinMode(4, OUTPUT);
  pinMode(7, OUTPUT);
  */
  digitalWrite(19, LOW);
    /*digitalWrite(17, LOW);
  digitalWrite(15,  LOW);
   digitalWrite(0,  LOW);
  digitalWrite(2,  LOW);
  digitalWrite(4,  LOW);
    digitalWrite(7,  LOW);
    */
}

void loop() {
   
  

  if (Serial.available() > 0) {

    int inByte = Serial.read();
    switch (inByte) {

        
      case 'w':
   digitalWrite(19, HIGH);
   /*   digitalWrite(17,HIGH);
       digitalWrite(15, HIGH);
      digitalWrite(0, HIGH);
     digitalWrite(2, HIGH);
       digitalWrite(4, HIGH);
      digitalWrite(7, HIGH);
*/
        Serial.write('W');
        
        break;
    
    case 'q':
 digitalWrite(19, LOW);
  /*
    digitalWrite(17, LOW);
  digitalWrite(15,  LOW);
   digitalWrite(0,  LOW);
  digitalWrite(2,  LOW);
  digitalWrite(4,  LOW);
    digitalWrite(7,  LOW);
 */
  delay(100);
  
           Serial.write('C');
        break;

}
  }
   //yield(); //
}
    int inByte = Serial.read();
    switch (inByte) {


      case 'w':

Is inByte a byte, as implied by its name, an int as implied by its declaration or a char as implied by the case statement ?

To be honest i took the sketch from the example page...

The whole thing works up to 3 digital.write.... why ?
really annoying ....

The whole thing works up to 3 digital.write..

What do you mean by this ?

digitalWrite(0, HIGH);This is the third digital.Write() in each case but pin 0 is used by serial comms so why are you writing to it ?

Hi me again,
i found out that the arduino had some problems i exchanged it at it worked fine ... sorry for that ...
but now here comes the next problem....
here is my final code

#include <Wire.h>
#include <Scheduler.h>


//Pump Frequenz

int F1 = 53;
int F2 = 51;
int F3 = 49;
int F4 = 47;
int F5 = 41;
int F6 = 43;
int F7 = 45;

//Für I²C Druck
int c = 0;
char dat[2];


void setup() {
  
  Wire.begin();
  Serial.begin(115200);
  

pinMode(19, OUTPUT); 
pinMode(17, OUTPUT);
pinMode(15, OUTPUT);
pinMode(12, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);

pinMode(18, OUTPUT); 
pinMode(16, OUTPUT);
pinMode(14, OUTPUT);
pinMode(1, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);

pinMode(42, OUTPUT); 
pinMode(46, OUTPUT);
pinMode(50, OUTPUT);
pinMode(40, OUTPUT);
pinMode(36, OUTPUT);
pinMode(32, OUTPUT);
pinMode(26, OUTPUT);

pinMode(44, OUTPUT); 
pinMode(48, OUTPUT);
pinMode(52, OUTPUT);
pinMode(38, OUTPUT);
pinMode(34, OUTPUT);
pinMode(28, OUTPUT);
pinMode(24, OUTPUT);

pinMode(F1, OUTPUT);
pinMode(F2, OUTPUT);
pinMode(F3, OUTPUT);
pinMode(F4, OUTPUT);
pinMode(F5, OUTPUT);
pinMode(F6, OUTPUT);
pinMode(F7, OUTPUT);


digitalWrite(19,LOW); 
digitalWrite(17,LOW); 
digitalWrite(15,LOW); 
digitalWrite(12,LOW); 
digitalWrite(2,LOW); 
digitalWrite(4,LOW); 
digitalWrite(7,LOW);

digitalWrite(18,LOW); 
digitalWrite(16,LOW); 
digitalWrite(14,LOW); 
digitalWrite(1,LOW); 
digitalWrite(3,LOW); 
digitalWrite(5,LOW); 
digitalWrite(6,LOW);

digitalWrite(42,LOW);
digitalWrite(46,LOW);
digitalWrite(50,LOW);
digitalWrite(40,LOW);
digitalWrite(36,LOW);
digitalWrite(32,LOW);
digitalWrite(26,LOW);

digitalWrite(44,LOW);
digitalWrite(48,LOW);
digitalWrite(52,LOW);
digitalWrite(38,LOW);
digitalWrite(34,LOW);
digitalWrite(28,LOW);
digitalWrite(24,LOW);
  
 
  
  
  Scheduler.startLoop(loop2);
  Scheduler.startLoop(loop3);
  //Scheduler.startLoop(loop4);
  // Scheduler.startLoop(loop5);
  //Scheduler.startLoop(loop6);
  //Scheduler.startLoop(loop7);
  //Scheduler.startLoop(loop8);
  //Scheduler.startLoop(loop9);
  //Scheduler.startLoop(loop10);




void loop3() {



  Wire.requestFrom(120,2);
  c=0;
while (Wire.available())
{
c=c*256+Wire.read();
}
dat[0] = byte(lowByte(c));
dat[1] = byte(highByte(c));
Serial.println(dat);
delay(10);
}



void loop() {
   
  
  

  if (Serial.available() > 0) {

    int inByte = Serial.read();
    switch (inByte) {

        
      case 'a':
       digitalWrite(19, HIGH);
       digitalWrite(17, HIGH);
       digitalWrite(15, HIGH);
       digitalWrite(12, HIGH);
       digitalWrite(2, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(7, HIGH);
       delay(10);
     
        Serial.write('A');
        
        break;
    
     
       
    case 'b':
       digitalWrite(19, LOW);
       digitalWrite(17, LOW);
       digitalWrite(15, LOW);
       digitalWrite(12, LOW);
       digitalWrite(2, LOW);
       digitalWrite(4, LOW);
       digitalWrite(7, LOW);
delay(10);
        Serial.write('B');
        
        break;
    
    case 'c':
       digitalWrite(18, HIGH);
       digitalWrite(16, HIGH);
       digitalWrite(14, HIGH);
       digitalWrite(1, HIGH);
       digitalWrite(3, HIGH);
       digitalWrite(5, HIGH);
       digitalWrite(6, HIGH);
       delay(10);
     
        Serial.write('C');
        
        break; 
        
     case 'd':
       digitalWrite(18, LOW);
       digitalWrite(16, LOW);
       digitalWrite(14, LOW);
       digitalWrite(1, LOW);
       digitalWrite(3, LOW);
       digitalWrite(5, LOW);
       digitalWrite(6, LOW);
delay(10);
        Serial.write('D');
        
        break;
        
        
             case 'e':
digitalWrite(42,HIGH);
digitalWrite(46,HIGH);
digitalWrite(50,HIGH);
digitalWrite(40,HIGH);
digitalWrite(36,HIGH);
digitalWrite(32,HIGH);
digitalWrite(26,HIGH); 
delay(10);
        Serial.write('E');
        
        break;
        
      case 'f':
digitalWrite(42,LOW);
digitalWrite(46,LOW);
digitalWrite(50,LOW);
digitalWrite(40,LOW);
digitalWrite(36,LOW);
digitalWrite(32,LOW);
digitalWrite(26,LOW); 
delay(10);
        Serial.write('F');
        
        break;
        
                     case 'g':
digitalWrite(44,HIGH);
digitalWrite(48,HIGH);
digitalWrite(52,HIGH);
digitalWrite(38,HIGH);
digitalWrite(34,HIGH);
digitalWrite(28,HIGH);
digitalWrite(24,HIGH); 
delay(10);
        Serial.write('G');
        
        break;
        
      case 'h':
digitalWrite(44,LOW);
digitalWrite(48,LOW);
digitalWrite(52,LOW);
digitalWrite(38,LOW);
digitalWrite(34,LOW);
digitalWrite(28,LOW);
digitalWrite(24,LOW); 
delay(10);
        Serial.write('H');
        
        break;
  }
  }
   yield(); // 
}

// Arbeitsfrequenz Pumpen

void loop2() {
  digitalWrite(F1, HIGH);
  digitalWrite(F2, HIGH);
  digitalWrite(F3, HIGH);
  digitalWrite(F4, HIGH);
  digitalWrite(F5, HIGH);
  digitalWrite(F6, HIGH);
  digitalWrite(F7, HIGH);
  delay(5);
  digitalWrite(F1, LOW);
  digitalWrite(F2, LOW);
  digitalWrite(F3, LOW);
  digitalWrite(F4, LOW);
  digitalWrite(F5, LOW);
  digitalWrite(F6, LOW);
  digitalWrite(F7, LOW);
  delay(5);                    // 100Hz
} 


/*
void loop4() {
   
  
  

  if (Serial.available() > 0) {

    int inByte = Serial.read();
    switch (inByte) {

      case 'c':
        digitalWrite(44, HIGH);
  digitalWrite(13, HIGH);
        
        break;
    
     
 case 'd':
        digitalWrite(44, LOW);
  digitalWrite(13, LOW);
        
        break;
    
    
       
  }
  }
   yield(); // 
}
*/

This code should control all of the named pins .... (works)
Should give an answer Serial write if certain case is chosen.... (doesn´t work)
Should write sensorvalues as low and high byte .... (doesn´t work)

again the problem appears if too many ports are programmed.... for example if i reduce the pin number to 12 it works.... more than 12 doesn´t work ....

is this usual or is the arduino too slow ?`or did i do any programming mistakes ?

pinMode(1, OUTPUT);

Serial not working, you say?

while (Wire.available())
{
c=c*256+Wire.read();
}

so, you apparently ask the slave device for some data, and then immediately proceed without necessarily giving it time to respond.

You also assume that 2 characters are going to be available at the same time.

You need to rethink the timing there.

You also seem to get two bytes, put them into a 2-byte int, and then take them out again, which seems to be moderately pointless, and probably won't facilitate debugging.

Having delays in a program with multiple loops also seems like a bad idea.

i changed pin1 and it wors fine ... sorry didn´t know that ..

iam also curisous about the multiple loops ... but the sheduler promisses that the loops are able to work indepent from each other.

the i2c communication needs time ? so i add a delay but as iam very new on this field i didn´t get the point with the amount of bytes.
i recive 2 byte from my slave device and want to send them via high and low byte to my computer ...
how to manage that ? btw what do you mean ?

void loop3() {



  Wire.requestFrom(120,2);
  c=0;
while (Wire.available())
{
c=c*256+Wire.read();
delay(10);
}
//dat[0] = byte(lowByte(c));
//dat[1] = byte(highByte(c));
Serial.println(dat);
delay(10);
}

Guys, now everything works fine.... thank you soooo much ...
Sorry for the trouble but i still need to get used to it how to solve programming problems ....
i had troubles with the ic2 connection so that effected the whole program...

Thank you anyway! :slight_smile:

while (Wire.available())
{
c=c*256+Wire.read();
delay(10);
}
//dat[0] = byte(lowByte(c));
//dat[1] = byte(highByte(c));
Serial.println(dat);

Well this is nonsense. Asking Serial.println( ) to print an array is not going to work.

If you want to read two bytes and print them, try

int i=0 ;
byte dat[2] ;
while (Wire.available() && i<2 )
{
dat [ i ] = Wire.read() ;
i++ ;
}
for ( i=0 ; i< 2 ; i++ )
{
Serial.print( dat ); // or Serial.print( dat*, HEX );*
}
Serial.println() ;
[/quote]

If you want to read two bytes and print them, try

using code tags

Serial.println(dat);

I am somewhat curious that the compiler even allows this.

Did you change the declaration of dat[2] from type byte to type char ? Your code snippet in reply #10 does not show the type.

The compiler may be interpreting this line as printing a char array, in which case you are asking for trouble as it won't be null-terminated. If it appears to be working, you may be "just lucky" that the byte after the 2 bytes in dat[] contains a zero.

Try

#include <Wire.h>

//Pump Frequenz

int F1 = 53;
int F2 = 51;
int F3 = 49;
int F4 = 47;
int F5 = 41;
int F6 = 43;
int F7 = 45;

//Für I²C Druck
int c = 0;
char dat[2];


void setup() {

  Wire.begin();
  Serial.begin(115200);


  pinMode(19, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(7, OUTPUT);

  pinMode(18, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  pinMode(42, OUTPUT);
  pinMode(46, OUTPUT);
  pinMode(50, OUTPUT);
  pinMode(40, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(26, OUTPUT);

  pinMode(44, OUTPUT);
  pinMode(48, OUTPUT);
  pinMode(52, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(24, OUTPUT);

  pinMode(F1, OUTPUT);
  pinMode(F2, OUTPUT);
  pinMode(F3, OUTPUT);
  pinMode(F4, OUTPUT);
  pinMode(F5, OUTPUT);
  pinMode(F6, OUTPUT);
  pinMode(F7, OUTPUT);


  digitalWrite(19, LOW);
  digitalWrite(17, LOW);
  digitalWrite(15, LOW);
  digitalWrite(12, LOW);
  digitalWrite(2, LOW);
  digitalWrite(4, LOW);
  digitalWrite(7, LOW);

  digitalWrite(18, LOW);
  digitalWrite(16, LOW);
  digitalWrite(14, LOW);
  digitalWrite(3, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);

  digitalWrite(42, LOW);
  digitalWrite(46, LOW);
  digitalWrite(50, LOW);
  digitalWrite(40, LOW);
  digitalWrite(36, LOW);
  digitalWrite(32, LOW);
  digitalWrite(26, LOW);

  digitalWrite(44, LOW);
  digitalWrite(48, LOW);
  digitalWrite(52, LOW);
  digitalWrite(38, LOW);
  digitalWrite(34, LOW);
  digitalWrite(28, LOW);
  digitalWrite(24, LOW);




  //Scheduler.startLoop(loop2);
  //Scheduler.startLoop(loop3);
  //Scheduler.startLoop(loop4);
  // Scheduler.startLoop(loop5);
  //Scheduler.startLoop(loop6);
  //Scheduler.startLoop(loop7);
  //Scheduler.startLoop(loop8);
  //Scheduler.startLoop(loop9);
  //Scheduler.startLoop(loop10);

}
void loop() {
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    switch (inByte) {
      case 'a':
        digitalWrite(19, HIGH);
        digitalWrite(17, HIGH);
        digitalWrite(15, HIGH);
        digitalWrite(12, HIGH);
        digitalWrite(2, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(7, HIGH);
        delay(10);
        Serial.println('A');
        break;
      case 'b':
        digitalWrite(19, LOW);
        digitalWrite(17, LOW);
        digitalWrite(15, LOW);
        digitalWrite(12, LOW);
        digitalWrite(2, LOW);
        digitalWrite(4, LOW);
        digitalWrite(7, LOW);
        delay(10);
        Serial.println('B');
        break;
      case 'c':
        digitalWrite(18, HIGH);
        digitalWrite(16, HIGH);
        digitalWrite(14, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        delay(10);
        Serial.println('C');
        break;
      case 'd':
        digitalWrite(18, LOW);
        digitalWrite(16, LOW);
        digitalWrite(14, LOW);
        digitalWrite(1, LOW);
        digitalWrite(3, LOW);
        digitalWrite(5, LOW);
        digitalWrite(6, LOW);
        delay(10);
        Serial.println('D');
        break;
      case 'e':
        digitalWrite(42, HIGH);
        digitalWrite(46, HIGH);
        digitalWrite(50, HIGH);
        digitalWrite(40, HIGH);
        digitalWrite(36, HIGH);
        digitalWrite(32, HIGH);
        digitalWrite(26, HIGH);
        delay(10);
        Serial.println('E');
        break;
      case 'f':
        digitalWrite(42, LOW);
        digitalWrite(46, LOW);
        digitalWrite(50, LOW);
        digitalWrite(40, LOW);
        digitalWrite(36, LOW);
        digitalWrite(32, LOW);
        digitalWrite(26, LOW);
        delay(10);
        Serial.println('F');
        break;
      case 'g':
        digitalWrite(44, HIGH);
        digitalWrite(48, HIGH);
        digitalWrite(52, HIGH);
        digitalWrite(38, HIGH);
        digitalWrite(34, HIGH);
        digitalWrite(28, HIGH);
        digitalWrite(24, HIGH);
        delay(10);
        Serial.write('G');
        break;
      case 'h':
        digitalWrite(44, LOW);
        digitalWrite(48, LOW);
        digitalWrite(52, LOW);
        digitalWrite(38, LOW);
        digitalWrite(34, LOW);
        digitalWrite(28, LOW);
        digitalWrite(24, LOW);
        delay(10);
        Serial.write('H');
        break;
    }
  }
}

This is simplified without all 10 loops, but the serial comms should work.

As you mentioned ... it workds just sometime ... It would be better if it would work more reliable... Hence i need to turn my device on and off to get the data....
This is the code the company send me ... but i guess it is not made for the arduino....

byte byte_msb, byte_lsb;     // 8bit values
int16 pressure;                    // 
16bit value
// Set I2C unit to I2C master mode, clock speed 100 kHz and 7 bit addressing
configureI2C (I2C_MASTER | CLK_SPEED_100KHZ | ADDRESSING_7BIT);
// Set the target address of the sensor (0x78 = 120dec)
I2C_set_target(0x78);
// Send start condition for reading from sensor (slave)
I2C_send_start_read();
// Read first (MSB) data byte and answer with ACK (continue communication)
I2C_read (&byte_msb, SEND_ACK);
// Read second (LSB) data byte and answer with NACK (end communication)
I2C_read (&byte_lsb, SEND_NACK);
// Send Stop condition
I2C_send_stop();
// Put both values together
pressure = ((int16)byte_msb << 8) | byte_lsb

Could anybody help me to translate ? ....
Iam curious about the "send" commands ... seems to be something like handshaking .... but i have no clue if its important or if the arduino does it by itself?....

My First Try .... but doesn´t work of course :smiley:

#include <Wire.h>
int msb=0;
int lsb=0;
int i=0;
char ACK;
char NACK;

void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  Serial.begin(115200);
}

void loop() {
{
Wire.requestFrom(123,1);
  msb = Wire.read();
Wire.beginTransmission(123);
Wire.write(ACK);
Wire.endTransmission();
Wire.requestFrom(123,1);
lsb= Wire.read();
Wire.beginTransmission(123);
Wire.write(NACK);
Wire.endTransmission();
}
Serial.print(msb);
Serial.println(lsb);

}

Where do ACK and NACK get values?

I found the following to this acknowledgements .... seems like i have to manipulate the SDA SCL pulses ?
Is this possible ?

2.7 Acknowledge (ACK or A)
Each byte sent on the bus has to be followed by
an acknowledge bit issued from the receiver for
correct receipt of data. The acknowledge also
means that the device is ready to continue the
data transfer.
The master must generate an extra clock pulse
for this purpose. The transmitter releases the
SDA line (HIGH) during the acknowledge clock
pulse. The receiver must pull down the SDA line
so that it is stable LOW from the beginning
(rising edge) of the acknowledge clock pulse
until its end (LOW level after falling edge) (see
Fig. 7).
If the receiver does not want to receive a further
byte it leaves the SDA line HIGH and a not
acknowledge (NACK or A) will be sent. Now the
master can terminate the transfer with a STOP
condition.