Pages: [1]   Go Down
Author Topic: Writing to Arduino from Excel Using PLX-DAQ  (Read 1163 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I'm attempting to use PLX-DAQ using the println(CELL, GET, F2) command to use either a 1 or 0 placed in cell
F2 to turn on and off the inbuilt led on pin 13. I think my code is almost there however when changing the cell
value from 0 to 1 or visa versa the led remains on. Please refer code below any help appreciated.

/*
  Ebb and Flow Vertical Garden
 */
 //Variables
int led = 13;
int pump = 2;
int lights = 3;
int lightlevel;
int pumprun;
boolean pumpon;
boolean lighton;
int cellval;
int USER1;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  Serial.begin(128000);
  Serial.println("CLEARDATA"); //PLX-DAQ COMMAND
  Serial.println("LABEL, Time, Light, Pump Count, Pump On, Light On"); //PLX-DAQ COMMANDS
  pinMode(led, OUTPUT);
  pinMode(pump, OUTPUT);
  pinMode(lights, OUTPUT);
 
   Serial.println("CELL,GET,F2"); //PLX-DAQ COMMAND retreiving value from F2.
   unsigned long sentTime = millis();
   while(millis() - sentTime < 3000UL) // Wait up to 60 seconds for a reply
    {
       if(Serial.available() > 0)
       {
          USER1 = Serial.read();
          digitalWrite(led, USER1);
          break;
       }
    }     
}

void loop() {

lightlevel = analogRead(A0);
Serial.println(lightlevel, DEC);

  if (lightlevel == 0)
     {
       digitalWrite(lights, LOW);
       Serial.print("CELL,SET,E2,1"); //PLX-DAQ COMMANDS
     }
  else
     {
       digitalWrite(lights, HIGH);
       Serial.print("CELL,SET,E2,0"); //PLX-DAQ COMMANDS
     }
     
 for (pumprun=0; pumprun<=100; pumprun++)
 {
   digitalWrite(pump, HIGH);
   lightlevel = analogRead(A0);
   pumpon == 0;
   delay(9000);
   Serial.print("DATA,TIME,"); //PLX-DAQ COMMANDS
   Serial.print(lightlevel, DEC);
   Serial.print(",");
   Serial.println(pumprun, DEC);
   Serial.print(",");
   Serial.println(pumpon, DEC);
   Serial.print(",");
   Serial.println(lighton, DEC);       
       
   }
 
 for (pumprun=100; pumprun>=0; pumprun--)
 {
   digitalWrite(pump, LOW);
   delay(110);
   Serial.print("DATA,TIME,"); //PLX-DAQ COMMANDS
   Serial.print(lightlevel, DEC);
   Serial.print(",");
   Serial.println(pumprun, DEC);
   Serial.print(",");
   Serial.println(pumpon, DEC);
   Serial.print(",");
   Serial.println(lighton, DEC);
 }
 
}     
Logged

Offline Offline
Edison Member
*
Karma: 48
Posts: 1628
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this and fix up your posted code first: How to post code properly

Then ponder what this statement does:
Code:
pumpon == 0;

Pete
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Yeah i had accidentally left that in from a previous attempt to get it to work.  Even with it removed i still cant get
the led to change state.  I think it may be some thing with this section of code:

Code:
int USER1;

void setup() {               
  // initialize the digital pin as an output.
  Serial.begin(128000);
  Serial.println("CLEARDATA");
 
 
   Serial.println("CELL,GET,F2");
   unsigned long sentTime = millis();
   while(millis() - sentTime < 3000UL) // Wait up to 60 seconds for a reply
    {
       if(Serial.available() > 0)
       {
          USER1 = Serial.read();
          digitalWrite(led, USER1);
          break;
       }
    }     
}
Logged

Offline Offline
Edison Member
*
Karma: 48
Posts: 1628
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
          USER1 = Serial.read();
          digitalWrite(led, USER1);

I presume that USER1 will have the ASCII value of whatever is in the cell (assuming that part works).
Whether it is 0 or 1 it will be non-zero and writing either of them as a digital value will turn the LED on.
You need to test for one of them and act accordingly:
Code:
    if(USER1 == '1')digitalWrite(led,1);
    else digitalWrite(led,0);

Pete
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Pete,

Thank you your last piece of code has fixed the problem. I think using ' ' identifies a CHAR in arduino which was the problem.
I'm now able to control the arduino from excel and also send data to excel.

Ive moved the code into the loop section of the code so the control can occur at any stage as opposed to only
during the first scan.

The entire code is pasted below for anyone how want to use PLX-DAQ and write from excel to Arduino.


Much appreciated i owe you one.

Russell
Code:
/*
  Pump
  Turns on an LED and pump for eleven seconds, then off for 10 minutes repeatedly.
 */
 
 //Variables
int led = 13;
int pump = 2;
int lights = 3;
int lightlevel;
int pumprun;
boolean pumpon;
boolean lighton;
int cellval;
int USER1;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  Serial.begin(128000);
  Serial.println("CLEARDATA");
  Serial.println("LABEL, Time, Light, Pump Count, Pump On, Light On");
  pinMode(led, OUTPUT);
  pinMode(pump, OUTPUT);
  pinMode(lights, OUTPUT);
   
}
// the loop routine runs over and over again forever:
void loop() {

lightlevel = analogRead(A0);
Serial.println(lightlevel, DEC);

  if (lightlevel == 0)
     {
       digitalWrite(lights, LOW);
       Serial.print("CELL,SET,E2,1");
     }
  else
     {
       digitalWrite(lights, HIGH);
       Serial.print("CELL,SET,E2,0");
     }
     
 for (pumprun=0; pumprun<=100; pumprun++)
 {
   digitalWrite(pump, HIGH);
   lightlevel = analogRead(A0);
   
   Serial.println("CELL,GET,F2"); //PLX-DAQ command to put state of excel cell into serial buffer.
   unsigned long sentTime = millis();
   while(millis() - sentTime < 3000UL) // Give it up to 3 seconds for a reply
    {
       if(Serial.available() > 0)
       {
         USER1 = Serial.read();
         if(USER1 == '1') digitalWrite(led,HIGH); //important ot identify a CHAR '1' and to use either 0 or 1 in the cell.
         else digitalWrite(led,LOW);
         break;
       }
    }   
   delay(9000);
   Serial.print("DATA,TIME,");
   Serial.print(lightlevel, DEC);
   Serial.print(",");
   Serial.println(pumprun, DEC);
   Serial.print(",");
   Serial.println(pumpon, DEC);
   Serial.print(",");
   Serial.println(lighton, DEC); 
       
   }
 
 for (pumprun=100; pumprun>=0; pumprun--)
 {
   digitalWrite(pump, LOW);
   delay(110);
   Serial.print("DATA,TIME,");
   Serial.print(lightlevel, DEC);
   Serial.print(",");
   Serial.println(pumprun, DEC);
   Serial.print(",");
   Serial.println(pumpon, DEC);
   Serial.print(",");
   Serial.println(lighton, DEC);
 }
 
}     
Logged

Pages: [1]   Go Up
Jump to: