Go Down

Topic: Plot Variable taken from arduino nano with respect to time. (Read 779 times) previous topic - next topic

xtron

I am getting wind data from an anemometer that is connected to a master arduino nano acting as transmitter. Using SPI, I am transmitting this data to a slave arduino nano acting as receiver.

Now, I want to plot the variable "windspeed" from the slave arduino with respect to time, continuously.

Can the plotting also be done using MATLAB? I do not need to use MATLAB necessarily. I just need to store the data.

Thank you in advance.

MASTER ARDUINO NANO:
Code: [Select]

// SimpleTxAckPayload - the master or the transmitter

#include <SPI.h>                //  Libraries needed to communicate with another arduino
#include <nRF24L01.h>           //  using the SPI communication protocol
#include <RF24.h>               //  obtained from https://github.com/nRF24/RF24


#define ANEMOMETER_PIN 0        //  Output from anemometer to pin A0 in MASTER arduino.


#define CE_PIN  7               //  CE & CSN pin locations
#define CSN_PIN 8               //  CE (Chip Enable) & CSN (Chip Select Not)


float windspeed = 0;            //  Initializing variable "windspeed"

const byte slaveAddress_1[5] = {'R','x','A','A','A'};   //This must be same in the Master and the Slave

RF24 radio(CE_PIN, CSN_PIN);    // Create a Radio when not using default pins for CE & CSN

char dataToSend[12] = "Windspeed 0";    // this must match dataToSend in the TX
char txNum = '0';
int ackData[2] = {-1, -1};      // to hold the two values coming from the slave
bool newData = false;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 30;                           // Send one transmission per 30ms




//===============

void setup() {

    Serial.begin(9600);     

    pinMode(10, OUTPUT);      //  Digital pin 10 must be set as output for SPI communication
    digitalWrite(10, HIGH);   //  Pin 10 set as HIGH for device to be a MASTER
   
    Serial.println("WindspeedTx Starting");

    radio.begin();

    // Data Transmission Speed options: RF24_250KBPS, RF24_1MBPS, RF24_2MBPS
    radio.setDataRate( RF24_2MBPS );    // Fastest data speed possible

    // Power Amplifier level options: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
    radio.setPALevel(RF24_PA_MAX);      //  Maximum tx/rx power

    // Use a channel unlikely to be used by Wifi, Microwave ovens etc (1-125)
    radio.setChannel(124);            // 124 is towards the end of 2.4GHz band

    radio.enableAckPayload();   // Acknowledgement payload

    radio.setRetries(3,5);      // delay, count
    radio.openWritingPipe(slaveAddress_1);    //  Opening pipeline for communication between MASTER and SLAVE
}





//==============

void loop() {

    // Read value from ADC @ ANEMOMETER_PIN
    // Arduino Nano ADC converts 0-5V from analog pin to a value from 0-1023 (10-bit resolution)
    windspeed = analogRead(ANEMOMETER_PIN);
   
    // Convert wind speed from ADC back to voltage
    windspeed = windspeed / 1023 * 5;

    //Convert voltage to m/s   
    windspeed = windspeed * 8.94;    //8.94 before

    //windspeed = windspeed / 22.89;  (1023 / 44.7 = 22.885)

    currentMillis = millis();
    if (currentMillis - prevMillis >= txIntervalMillis) {
        send();
    }
    showData();
}





//================

void send() {

    bool result;
    result = radio.write( &windspeed, sizeof(windspeed) );
        // Always use sizeof() as it gives the size as the number of bytes.
        // For example if dataToSend was an int sizeof() would correctly return 2

    Serial.print("Data Sent ");
    Serial.print(dataToSend);
    if (result) {
        if ( radio.isAckPayloadAvailable() ) {
            radio.read(&ackData, sizeof(ackData));
            newData = true;
        }
        else {
            Serial.println("  Acknowledge but no data ");
        }
        updateMessage();
    }
    else {
        Serial.println("  Tx failed");
    }

    prevMillis = millis();
 }






//=================

void showData() {
    if (newData == true) {
        Serial.print("  Acknowledge data ");
        Serial.print(ackData[0]);
        Serial.print(", ");
        Serial.println(ackData[1]);
        Serial.println();
        newData = false;
    }
}





//================

void updateMessage() {
        // so you can see that new data is being sent
    txNum += 1;
    if (txNum > '9') {
        txNum = '0';
    }
    dataToSend[10] = txNum;
}


SLAVE ARDUINO NANO:
Code: [Select]

// SimpleRxAckPayload- the slave or the receiver

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#include <SPI.h>      //  Libraries needed to communicate with another arduino
#include <nRF24L01.h> //  using the SPI communication protocol
#include <RF24.h>     //  obtained from https://github.com/nRF24/RF24

#define CE_PIN 7      //  CE & CSN pin locations
#define CSN_PIN 8     //  CE (Chip Enable) & CSN (Chip Select Not)
#define PWM_OUT 5     //  Digital pin 5 used for analog output

LiquidCrystal_I2C lcd(0x3F,20,4);  // set the LCD address to 0x3F for a 16 chars and 2 line display

float windspeed = 0;      //  Initializing variable "windspeed"
float windspeedmph = 0;   //  Initializing variable "windspeedmph"

int count = 0;            //  Initializing variable "count"

const byte thisSlaveAddress_1[5] = {'R','x','A','A','A'};   //This must be same in the Master and the Slave

RF24 radio(CE_PIN, CSN_PIN);    // Create a Radio when not using default pins for CE & CSN

char dataReceived[12]; // this must match dataToSend in the TX
int ackData[2] = {109, -4000}; // the two values to be sent to the master
bool newData = false;





//==============

void setup() {

    Serial.begin(9600);

    pinMode(PWM_OUT, OUTPUT);   //Setting digital pin PWM_OUT as output

    Serial.println("WindspeedRx Starting");
    radio.begin();

    // Data Transmission Speed options: RF24_250KBPS, RF24_1MBPS, RF24_2MBPS
    radio.setDataRate( RF24_2MBPS );    // Fastest data speed possible

    // Power Amplifier level options: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
    radio.setPALevel( RF24_PA_MAX );    //  Maximum tx/rx power

    // Use a channel unlikely to be used by Wifi, Microwave ovens etc (1-125)
    radio.setChannel( 124 );          // 124 is towards the end of 2.4GHz band
   
    radio.openReadingPipe(1, thisSlaveAddress_1);

    radio.enableAckPayload();       // Acknowledgement payload
   
    radio.startListening();

    radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data

    lcd.init();                      // initialize the lcd
   
    lcd.backlight();                 // turning on the LCD backlight
}





//=============

void loop() {
    getData();
    showData();
    windspeedmph = windspeed * 2.24;        // windspeed m/s to mph conversion (2.237)
   
    analogWrite(PWM_OUT, windspeed * 5.71); //  windspeed 0-44.7, PWM_OUT 0-255; 255/44.7=5.7045

    if ( count <= 12000 ) {                                   // Don't need to send data to LCD as frequently
                     
                      count++;
    }

    else {
                      lcd.setCursor(3,0);                     //Cursor location (column,row), LED is 20x4 matrix
                      lcd.print("Real Windspeed");
                      lcd.setCursor(6,1);
                      lcd.print("Outside:");
                      lcd.setCursor(6,2);
                      lcd.print(windspeed);
                      lcd.setCursor(10,2);
                      lcd.print("m/s");
                      lcd.setCursor(6,3);
                      lcd.print(windspeedmph);
                      lcd.setCursor(10,3);
                      lcd.print("mph");
                      count = 0;
    }
}





//===============

void getData() {
    if ( radio.available() ) {
        radio.read( &windspeed, sizeof(windspeed) );
        updateReplyData();
        newData = true;
    }
}





//================

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(windspeed);
        Serial.print(" ackPayload sent ");
        Serial.print(ackData[0]);
        Serial.print(", ");
        Serial.println(ackData[1]);
        newData = false;
    }
}





//================

void updateReplyData() {
    ackData[0] -= 1;
    ackData[1] -= 1;
    if (ackData[0] < 100) {
        ackData[0] = 109;
    }
    if (ackData[1] < -4009) {
        ackData[1] = -4000;
    }
    radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
}

spycatcher2k

Possibly, but asking in a MATLAB forum would probably be better.  :)
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

xtron

I don't need MATLAB, I just need to plot the value against time, to collect real wind data over a period of several days.

spycatcher2k

Your a twat for changing your original post!

This is a trivial process, but I'm out.

 >:(
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

Go Up