element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Rate my arduino code
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 26 replies
  • Subscribers 384 subscribers
  • Views 4306 views
  • Users 0 members are here
Related

Rate my arduino code

scottiebabe
scottiebabe over 2 years ago

I have never used an actual arduino board and have only tried the arduino framework on non-arduino hardware a few times.

I am putting together a little test box for an ADS1115 ADC module (or clone of  New clone on the block  ) 

Quite stuff inside the box, noisy stuff outside the diecast box, nothing fancy.

image

I am using a RPI Pico running the arduino framework using platformio

image

Here is the code I put together using Adafruits arduino library for the ADS1115

// Sample an ADS1115 ADC via i2c with a RPI Pico
// PICO GPIO20 - I2C_SDA
// PICO GPIO21 - I2C_SCL
// PICO GPIO16 - ADS1115_DRDY
//
// Inspired by Adafruit demo, use as you like.
// scottiebabe/sstobbe@2n3904blog.com

#include <Wire.h>
#include <Adafruit_ADS1X15.h>

// GPIO16 ISR
void ADSInterrupt();

Adafruit_ADS1115 ads1115;
MbedI2C i2c(20,21);  // Use GPIO Pins 20,21 on Pico for i2c

void setup(void)
{
  Serial.begin(9600);
  //while (!Serial);
  delay(1000);
  Serial.println("Hello!");
  Serial.println("Getting single-ended readings from AIN0..3");
  Serial.println("ADC Range: +/- 6.144V (1 bit = 187.5uV)");
  ads1115.begin(0x48,&i2c);

  // Enable Pico's onboard LED
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN,1);

  // Setup ISR on GPIO16
  pinMode(16,INPUT_PULLUP);
  attachInterrupt(16, ADSInterrupt, FALLING );
  gpio_pull_up(16);

  Serial.println("Hello, pt2!");
  Serial.println("Configured ADS1115 for single channel continuous conversion.");
  Serial.println("ADC Range: +/- 256mV (1 bit = 7.8125 uV)");
  Serial.println("Fs: 860 Sa/s");
  Serial.println("Ain: Ain0-single ended");
  //ads1115.setGain(GAIN_TWOTHIRDS); // SET FSR +-6V
  ads1115.setGain(GAIN_SIXTEEN); // Set FSR +- 256 mV
  ads1115.setDataRate(RATE_ADS1115_860SPS);
  //ads1115.startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_0_1,true);
  Serial.println("Starting Acquisition! ...");
  ads1115.startADCReading(ADS1X15_REG_CONFIG_MUX_SINGLE_0,true);
}

#define ADS_BUF_LEN (4096)
volatile int16_t  samples[ADS_BUF_LEN] = {0};
volatile uint32_t tstamps[ADS_BUF_LEN] = {0};
volatile int adsidx = 0;
volatile int adsinterrupt = 0;

// GPIO16 ISR
void ADSInterrupt() {
  if( adsidx < ADS_BUF_LEN ){
    tstamps[adsidx] = time_us_32();
  }
  adsinterrupt = 1;
}

/* Adafruit Demo Loop
void loop(void)
{
  int16_t adc0, adc1, adc2, adc3;

  adc0 = ads1115.readADC_SingleEnded(0);
  adc1 = ads1115.readADC_SingleEnded(1);
  adc2 = ads1115.readADC_SingleEnded(2);
  adc3 = ads1115.readADC_SingleEnded(3);
  Serial.print("AIN0: "); Serial.println(adc0);
  Serial.print("AIN1: "); Serial.println(adc1);
  Serial.print("AIN2: "); Serial.println(adc2);
  Serial.print("AIN3: "); Serial.println(adc3);
  Serial.println(" ");
  
  delay(1000);
}
*/

void loop(void){

  if(adsinterrupt){
    if( adsidx < ADS_BUF_LEN ){
      samples[adsidx] = ads1115.getLastConversionResults();
      adsidx++;
    }
    adsinterrupt = 0;
  }

  if(adsidx >= ADS_BUF_LEN){
    //Print out the sample buffer to serial console
    Serial.println("Acquisition Buffer:");
    for(int i=0; i<ADS_BUF_LEN; i++){
      Serial.print(tstamps[i]);
      Serial.print(",");
      Serial.println(ads1115.computeVolts(samples[i]),7);
      delay(1);
    }

    
    // dummy read to re-arm interrupt
    ads1115.getLastConversionResults();
    adsidx = 0;
    adsinterrupt = 0;
    Serial.println("Starting Acquisition! ...");
  }
}

Thumbsup / Thumbsdown

  • Sign in to reply
  • Cancel

Top Replies

  • Jan Cumps
    Jan Cumps over 2 years ago +4
    I'd move the defines and global variables to the begin, under the includes. That's the only nitpick.
  • kmikemoo
    kmikemoo over 2 years ago +3
    Not that this will help any at all but... if it runs, it's always a .
  • scottiebabe
    scottiebabe over 2 years ago in reply to Jan Cumps +3
    Using this decorator puts my function in sram
Parents
  • phoenixcomm
    phoenixcomm over 2 years ago

     scottiebabe As a confirmed C programmer and just getting into JAVA(I like it as the syntax is almost the same) I and most of the folks I know always put globals, #define, structs, and whatnot in my headers or file.h  either main.h or file.h and they are always in a subdir called inc or include.  I will now rub more folks the wrong way by saying I never use the void loop{} construction as you can't return var, error codes, and things like that to the above program and you can't have a program with command line args, so I always use the standard invocation of int main( argv, argc) {}... 
    BTW in your void setup(){} my code is normally this:

    void setup(){
    #include "inc/setup.h"
    }

    
    

    The reason for not using the void loop(){} construction it does not provide me with a return value! In my sim and the most thing I write before I do anything I always test my results, so lets say you have two cpus closely coupled by some sort of communication link. ( the following is agnostic.) CPU1 expects a start sequence from CPU2, so what happens when CPU2 is late CPU 1 loops for 10~ times, if it never sees the message, CPU1 com fails with an error code stating that CPU2 is mute, then the program can exit cleanly with a error, maybe even into a log oh my so now you are back to test case maybe loop back, and the beat goes on.  

    Also please stop using the really cruddy ArduinoIDE as It is buggy and for system work it does not work well grab a copy of Ecipse C/C++ and If you must get the Arduino add-on and you get with this the GCC cross compiler. which really crosses to almost all compilers, and this way you have a constant IDE that is globally accepted.  ~~CAH

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to phoenixcomm

     > always put globals, in my headers

    Noooooooooooooooooooo Slight smile

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to phoenixcomm

     > always put globals, in my headers

    Noooooooooooooooooooo Slight smile

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube