arduino program runs only at start up - arduino

My arduino program runs only when on start up or when i press the reset button, i dont know the problem as i am a new bee into this. Please need help.
i have some constants here for the remote control, and i am just trying to replicate what i am sending through the arduino ir led, every thing works fine except that the program runs only once.
#include <IRremote.h> // use the library
#define PanasonicAddress 0x4004 // Panasonic address (Pre data)
#define PanasonicPower 0x100BCBD // Panasonic Power button
#define PanasonicZero 0x1009899 // Panasonic button
#define PanasonicOne 0x1000809 // Panasonic button
#define PanasonicTwo 0x1008889 // Panasonic button
#define PanasonicThree 0x1004849 // Panasonic button
#define PanasonicFour 0x100C8C9 // Panasonic button
#define PanasonicFive 0x1002829 // Panasonic button
#define PanasonicSix 0x100A8A9 // Panasonic button
#define PanasonicSeven 0x1006869 // Panasonic button
#define PanasonicEight 0x100E8E9 // Panasonic button
#define PanasonicNine 0x1001819 // Panasonic button
#define PMute 0x1004C4D
#define PCPlus 0x1002C2D
#define PCMinus 0x100ACAD
#define PVMinus 0x1008485
#define PVPlus 0x1000405
#define PRTune 0x100ECED
#define JVCPower 0xC5E8
int receiverpin = 15; // pin 1 of IR receiver to Arduino digital pin 15
IRsend irsend;
IRrecv irrecv(receiverpin); // create instance of irrecv
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // start the IR receiver
// pinMode(7, OUTPUT);
for (int z = 11 ; z < 14 ; z++) // set up digital pins
{
pinMode(z, OUTPUT);
}
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
for (int z = 0 ; z < 2 ; z++) // ignore the repeated codes
{
irrecv.resume(); // receive the next value
}
}
}
void translateIR()
// takes action based on IR code received
// uses Sony IR codes
{
switch(results.value)
{
case PanasonicFour: pinOnTriple(1, 0, 0);
irsend.sendPanasonic(PanasonicAddress,PanasonicFour); // This should turn your TV on and off
break; // 2
case PanasonicTwo: pinOnTriple(0, 1, 0);
irsend.sendPanasonic(PanasonicAddress,PanasonicTwo);
break; // 3
case PanasonicFive: pinOnTriple(HIGH, LOW, HIGH);
irsend.sendPanasonic(PanasonicAddress,PanasonicFive);
break; // 5
case PanasonicSix: pinOnTriple(HIGH, HIGH, LOW);
irsend.sendPanasonic(PanasonicAddress,PanasonicSix);
break; // 6
case PanasonicSeven: pinOnTriple(HIGH, HIGH, HIGH);
irsend.sendPanasonic(PanasonicAddress,PanasonicSeven);
break; // 7
case PanasonicZero: pinOnTriple(LOW, LOW, LOW);
irsend.sendPanasonic(PanasonicAddress,PanasonicZero);
break; // 8
case PanasonicOne: pinOnTriple(LOW, LOW, HIGH);
irsend.sendPanasonic(PanasonicAddress,PanasonicOne);
break; // 9
case PanasonicThree: pinOnTriple(LOW, HIGH, HIGH);
irsend.sendPanasonic(PanasonicAddress,PanasonicThree);
break; // 10
case 0x100BCBD: pinOnTriple(LOW, LOW, LOW);
irsend.sendPanasonic(PanasonicAddress,PanasonicPower); // This should turn your TV on and off
irsend.sendJVC(JVCPower, 16,0); // hex value, 16 bits, no repeat
delay(50); // see http://www.sbprojects.com/knowledge/ir/jvc.php for information
irsend.sendJVC(JVCPower, 16,1); // hex value, 16 bits, repeat
delay(50);
break;
case 0x1004C4D:
irsend.sendPanasonic(PanasonicAddress,PMute);
break; // 11
case 0x1002C2D:
irsend.sendPanasonic(PanasonicAddress,PCPlus);
break; // 11
case 0x100ACAD:
irsend.sendPanasonic(PanasonicAddress,PCMinus);
break; // 11
case 0x1008485:
irsend.sendPanasonic(PanasonicAddress,PVMinus);
break; // 11
case 0x1000405:
irsend.sendPanasonic(PanasonicAddress,PVPlus);
break; // 11
case 0x100ECED:
irsend.sendPanasonic(PanasonicAddress,PRTune);
break; // 11
}
}
void pinOnTriple(int pin, int pino, int pini) // turns on digital pins for 1 second
{
digitalWrite(11, pin);
digitalWrite(12, pino);
digitalWrite(13, pini);
delay(1000);
digitalWrite(11, 0);
digitalWrite(12, 0);
digitalWrite(13, 0);
}

You should just check for receiving the repeat codes (0xFFFFFF) and just ignore this code. And just issue the resume command just once. You may be accidently skipping the valid codes.
Also, you need to issue a resume after transmitting IR (as tx disables rx).
This last one is probably the issue for you.
Hope it helps
may be worth putting in a delay before re-tx as the tx may overlap the repeat codes
For anyone interested in IR protocols - we have just launched a project for AnalysIR - IR Decoder & Analyzer GUI (Arduino & Raspberry Pi). Currently we support 17 IR protocols and are looking for more to add as part of the campaign. Suggestions Welcome!
If the project is successful, we hope to add support for Raspberry Pi !
You can find out more and support the Project by visiting http://igg.me/at/AnalysIR/x/3752156 or Screenshot via www.AnalysIR.com

Related

Issue with using the adafruit motorshield v1 library

I am new to microcontrollers programming. I just got my hands on the arduino uno board. I am trying to build the popular "bluetooth robot car" using the uno, adafruit motorshield v1 and HC-05 bluetooth module. My code attached below compiles and uploads without any error or warning. While not stacking the shield and not attaching the bluetooth module, instead seeing "Robot remote control mode" on the serial monitor, I see some "Rbnld tedisjvv shhsjj hshhs". When I attach and connect the bluetooth module and send a command 'R', instead of seeing 'R' on the monitor I see 'R' followed by an up-pointing arrow. I have searched for help on the web on similar problems but none has helped. I have tried other codes for serial communication(like blinking an led when 'B' is read from serial) and it worked perfectly.I need help.
// include the Adafruit motor v1 library
#include <AFMotor.h>
AF_DCMotor MotorFR(1); // Motor for drive Front Right on M1
AF_DCMotor MotorFL(2); // Motor for drive Front Left on M2
AF_DCMotor MotorBL(3); // Motor for drive Back Left on M3
AF_DCMotor MotorBR(4); // Motor for drive Back Right on M4
const int buzPin = 2; // set digital pin 2 as buzzer pin (use active buzzer)
const int ledPin = A5; // set digital pin A5 as LED pin (use super bright LED)
int valSpeed = 255;
void setup(){
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("*Robot Remote Control Mode*");
pinMode(buzPin, OUTPUT); // sets the buzzer pin as an Output
pinMode(ledPin, OUTPUT); // sets the LED pin as an Output
// Set the speed to start, from 0 (off) to 255 (max speed)
MotorFL.setSpeed(valSpeed);
MotorFR.setSpeed(valSpeed);
MotorBL.setSpeed(valSpeed);
MotorBR.setSpeed(valSpeed);
// turn off motor
MotorFL.run(RELEASE);
MotorFR.run(RELEASE);
MotorBL.run(RELEASE);
MotorBR.run(RELEASE);
}
void loop() {
while (Serial.available() > 0) {
char command = Serial.read(); // gets one byte from serial buffer
Serial.println(command);
switch(command){
case 'F': // move forward
SetSpeed(valSpeed);
MotorFL.run(FORWARD);
MotorFR.run(FORWARD);
MotorBL.run(FORWARD);
MotorBR.run(FORWARD);
break;
case 'B': // move backward
SetSpeed(valSpeed);
MotorFL.run(BACKWARD);
MotorFR.run(BACKWARD);
MotorBL.run(BACKWARD);
MotorBR.run(BACKWARD);
break;
case 'R': // turn right
SetSpeed(valSpeed);
MotorFL.run(FORWARD);
MotorFR.run(BACKWARD);
MotorBL.run(FORWARD);
MotorBR.run(BACKWARD);
break;
case 'L': // turn left
SetSpeed(valSpeed);
MotorFL.run(BACKWARD);
MotorFR.run(FORWARD);
MotorBL.run(BACKWARD);
MotorBR.run(FORWARD);
break;
case 'G': // forward left
MotorFL.setSpeed(valSpeed/4);
MotorBL.setSpeed(valSpeed/4);
MotorFL.run(FORWARD);
MotorFR.run(FORWARD);
MotorBL.run(FORWARD);
MotorBR.run(FORWARD);
break;
case 'H': // backward left
MotorFL.setSpeed(valSpeed/4);
MotorBL.setSpeed(valSpeed/4);
MotorFL.run(BACKWARD);
MotorFR.run(BACKWARD);
MotorBL.run(BACKWARD);
MotorBR.run(BACKWARD);
break;
case 'I': // forward right
MotorFR.setSpeed(valSpeed/4);
MotorBR.setSpeed(valSpeed/4);
MotorFL.run(FORWARD);
MotorFR.run(FORWARD);
MotorBL.run(FORWARD);
MotorBR.run(FORWARD);
break;
case 'J': // backward right
MotorFR.setSpeed(valSpeed/4);
MotorBR.setSpeed(valSpeed/4);
MotorFL.run(BACKWARD);
MotorFR.run(BACKWARD);
MotorBL.run(BACKWARD);
MotorBR.run(BACKWARD);
break;
case 'S': // stop
MotorFL.run(RELEASE);
MotorFR.run(RELEASE);
MotorBL.run(RELEASE);
MotorBR.run(RELEASE);
break;
case 'V': // beep buzzer
digitalWrite(buzPin, HIGH);
delay(150);
digitalWrite(buzPin, LOW);
delay(100);
digitalWrite(buzPin, HIGH);
delay(250);
digitalWrite(buzPin, LOW);
break;
case 'W': // turn light on
digitalWrite(ledPin, HIGH);
break;
case 'w': // turn light off
digitalWrite(ledPin, LOW);
break;
case '0': // set speed motor to 0 (min)
SetSpeed(0);
break;
case '1': // set speed motor to 30
SetSpeed(30);
break;
case '2': // set speed motor to 55
SetSpeed(55);
break;
case '3': // set speed motor to 80
SetSpeed(80);
break;
case '4': // set speed motor to 105
SetSpeed(105);
break;
case '5': // set speed motor to 130
SetSpeed(130);
break;
case '6': // set speed motor to 155
SetSpeed(155);
break;
case '7': // set speed motor to 180
SetSpeed(180);
break;
case '8': // set speed motor to 205
SetSpeed(205);
break;
case '9': // set speed motor to 230
SetSpeed(230);
break;
case 'q': // set speed motor to 255 (max)
SetSpeed(255);
break;
}
}
}
// function for setting speed of motors
void SetSpeed(int val){
valSpeed = val;
MotorFL.setSpeed(val);
MotorFR.setSpeed(val);
MotorBL.setSpeed(val);
MotorBR.setSpeed(val);
}```

Arduino: Why doesn't my stepper move in reverse direction?

I have verified that its not a wiring problem by using some non library example code, so it seems the problem is not with the circuit.
I'm expecting the following code to run the motor in one direction 200 steps then run 200 steps in the opposite direction. It seems to run the initial 200 steps but then stops, why?
In the setup function:
stepper.setCurrentPosition(0);
// Set the maximum speed in steps per second:
stepper.setMaxSpeed(1000);
In the loop function:
while(stepper.currentPosition() < 300){
stepper.moveTo(200);
stepper.setSpeed(200);
stepper.run();
if (stepper.distanceToGo() == 0) {
stepper.moveTo(-stepper.currentPosition());
stepper.setSpeed(200);
stepper.run();
}
}
Here's my entire code. The if/else at the bottom is where I want to run the motor. I've put in some non working example code in this block as an example:
#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
// Include the AccelStepper library:
#include <AccelStepper.h>
// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 2; // the number of the pushbutton pin
#define BLUE 7
#define GREEN 6
#define RED 3
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
#define dirPin 10
#define stepPin 11
#define motorInterfaceType 1
// Variables will change:
String authKeyFob = "123456789";
String card_ID="";
int ledState = 0; // remember current led state
int buttonState = 0;
int oldButtonState = 0;
bool toggle = false;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
// initialize the pushbutton pin as an pull-up input
// the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
pinMode(BUTTON_PIN, INPUT);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(BLUE, OUTPUT);
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(50);
// stepper.setSpeed(200);
// stepper.moveTo(200);
}
void loop() {
/*
// Change direction once the motor reaches target position
if (stepper.distanceToGo() == 0) {
stepper.moveTo(-stepper.currentPosition());
}
// Move the motor one step
stepper.run();
*/
buttonState = digitalRead(BUTTON_PIN);
// read the state of the switch/button:
oldButtonState = LOW; // NEED TO ADD A DECLARATION FOR THIS BEFORE SETUP
buttonState = digitalRead(BUTTON_PIN);
// Look for new cards, and select one if present
if (!mfrc522.PICC_IsNewCardPresent() ) {
}
if(mfrc522.PICC_ReadCardSerial() ){
card_ID="";
for (byte i = 0; i < mfrc522.uid.size; i++) {
card_ID += mfrc522.uid.uidByte[i];
}
if(card_ID == authKeyFob){
toggle = !toggle;
delay(200);
}
}
// if the button just became pressed...
if(buttonState == HIGH && oldButtonState==LOW){
toggle = !toggle; // same thing, toggle our variable.
delay(200);
}
if (toggle) {
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
stepper.moveTo(200);
stepper.setSpeed(200);
stepper.run();
} else {
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
stepper.moveTo(200);
stepper.setSpeed(200);
stepper.run();
}
oldButtonState = buttonState; // save the button state for next time
}
The run method doesn't block. It just takes one step if it is time. So let's step through your code. First we enter the while loop and tell it to step to 200 and set the speed and call run. We keep repeating that over and over. If we get to a point where we have 0 to go then you say moveTo the negative position, set the speed, call run once and then the while loop exits and repeats. On that repeat we say move to 200 and start calling run on that. So you're only calling run once with the moveTo set to the negative number.
You need to rethink the logic on this. I don't know what else is in your loop, so it's hard to say what exactly you want. If you really insist on it being blocking in the while loop then:
stepper.moveTo(200);
stepper.setSpeed(200);
while(stepper.distanceToGo() != 0){
stepper.run();
}
stepper.moveTo(-200);
while(stepper.distanceToGo != 0){
stepper.run();
}
This code moves the stepper in each direction.
if (toggle) {
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
while(stepper.currentPosition() <= 200){
stepper.moveTo(200);
stepper.setSpeed(200);
stepper.runSpeedToPosition();
if(stepper.currentPosition()==200){
break;
}
}
} else {
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
while(stepper.currentPosition() >= -200){
stepper.moveTo(-200);
stepper.setSpeed(200);
stepper.runSpeedToPosition();
if(stepper.currentPosition()==-200){
break;
}
}
}

SPI on ADE7953 board

So, I've been trying to emplement a SPI communication between the board and my arduino. In the board's manual, it says that it is required to send two bytes with the address I want to read/write and another byte to choose read or write (most significant bit tells you which will apply). However, my code does not seem to be running. Anyone care to help? Code
I've not used the actual Arduino SPI library, but I will offer you an Arduino snippet that will bitbang SPI instead. It will allow you to send some bytes just to see if everything is working. It might be useful to try another approach just to see if it works. You will need to change the pins and registers to match your target application.
#define PIN_SPIDATA 16
#define PIN_SPICLK 17
#define PIN_SPILOAD 18
#define REG_DECODEMODE 0x09
#define REG_INTENSITY 0x0A
#define REG_SCANLIMIT 0x0B
#define REG_SHUTDOWN 0x0C
#define REG_DISPLAYTEST 0x0F
void setup() {
// set ddr for sw spi pins
pinMode(PIN_SPICLK, OUTPUT);
pinMode(PIN_SPIDATA, OUTPUT);
pinMode(PIN_SPILOAD, OUTPUT);
setRegister(REG_INTENSITY, 0x04);
setRegister(REG_SCANLIMIT, 0x07);
setRegister(REG_SHUTDOWN, 0x01); // normal operation
setRegister(REG_DECODEMODE, 0x00); // pixels not integers
setRegister(REG_DISPLAYTEST, 0x00); // not in test mode
}
// sends a single byte by sw spi (no latching)
void putByte(uint8_t data)
{
uint8_t i = 8;
uint8_t mask;
while(i > 0) {
mask = 0x01 << (i - 1); // get bitmask
digitalWrite(PIN_SPICLK, LOW); // tick
if (data & mask){ // choose bit
digitalWrite(PIN_SPIDATA, HIGH); // set 1
}else{
digitalWrite(PIN_SPIDATA, LOW); // set 0
}
digitalWrite(PIN_SPICLK, HIGH); // tock
--i; // move to lesser bit
}
}
// sets register to the same byte value for all screens
void setRegister(uint8_t reg, uint8_t data)
{
digitalWrite(PIN_SPILOAD, LOW); // begin
for(uint8_t i = 0; i < numChips; ++i){
putByte(reg); // specify register
putByte(data); // send data
}
digitalWrite(PIN_SPILOAD, HIGH); // latch in data
digitalWrite(PIN_SPILOAD, LOW); // end
}

NRF24L01 with ATTiny and Uno not connecting

I have an ATTiny85 connected to an NRF24L01+ module using this wiring diagram: diagram. The ATTiny85 periodically goes in and out of sleep to send some value to a receiver, an Arduino Uno. If the ATTiny is running off the Arduino power supply (3.3v), everything works correctly. When I run the ATTiny off of a separate CR2032 coin cell that delivers around 3v, the Arduino never receives any data. I have a status LED hooked up to the ATTiny to ensure that the ATTiny is waking correctly, which it is. Here's the code for both:
EDIT:
Connecting it to an external 3.3v not from the Uno makes everything work - why wouldn't the coin cell's voltage work? I think everything is rated below 2.8v, the CR2032 minimum.
ATTiny Code
#include <avr/sleep.h>
#include <avr/interrupt.h>
// Routines to set and claer bits (used in the sleep code)
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define CE_PIN 3
#define CSN_PIN 3 //Since we are using 3 pin configuration we will use same pin for both CE and CSN
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openWritingPipe(address); // Write to device address 'SimpleNode'
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(1000);
setup_watchdog(6);
}
volatile int watchdog_counter = 0;
ISR(WDT_vect) {
watchdog_counter++;
}
void loop()
{
sleep_mode(); //Go to sleep!
if(watchdog_counter >= 5)
{
digitalWrite(4, HIGH);
watchdog_counter = 0;
payload = 123456;
radio.write( &payload, sizeof(unsigned long) ); //Send data to 'Receiver' ever second
delay(1000);
digitalWrite(4, LOW);
}
}
//Sleep ATTiny85
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System actually sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDIE);
}
Receiver Code
#define CE_PIN 7
#define CSN_PIN 8
#include <SPI.h>
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
while (!Serial);
Serial.begin(115200);
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openReadingPipe(1, address); // Write to device address 'SimpleNode'
radio.startListening();
Serial.println("Did Setup");
}
void loop(void){
if (radio.available()) {
radio.read( &payload, sizeof(unsigned long) );
if(payload != 0){
Serial.print("Got Payload ");
Serial.println(payload);
}
}
}
Is the problem here that the ATTiny and Uno need to be turned on at the same time to establish a connection, or is it something to do with the battery, or something else entirely? Any help would be appreciated.
I'm experiencing the same problem when running Arduino Nano from a battery.
Nano has a 3.3V pin that I'm using for powering the NRF24L01+ module.
When the voltage from my battery-pack drops under 3.3V, the 3.3V pin voltage also drops. After few minutes, the RF module is not sending any messages.
I fixed the problem temporarily by routing the battery through a 12V step-up regulator that I bought earlier for a different project. These 12V then go to the "UN" pin on Nano which accepts 6-20V. This setup works nicely but is definitely not optimal.
Therefore I'm planning to use a 3.3V step-up regulator such as Pololu 3.3V Step-Up Voltage Regulator U1V11F3 which, according to the supplier, can efficiently generate 3.3V from input voltages as low as 0.5V.
I think this might be helpful to your project as well.

How can I get rid of error: 'SerialUSB' was not declared in this scope in my Arduino Sketch?

The code is below, slightly modified from a tutorial. I am working on a Teensy3.1. I added #include <SoftwareSerial.h> but it did not help. I also tried SerialUSB.begin(9600); instead of WiredSerial.begin(9600); //use native port on Due
#include <SoftwareSerial.h>
//minimal sketch for connection to ADS129n family. Load this script and open Tools/SerialMonitor.
//You should see text like this
// Device Type (ID Control Register): 62 Channels: 8
//If you see "Channels: 0" then check your wiring
#include "ads1298.h"
#include "adsCMD.h"
#include <Arduino.h>
#include <SPI.h> // include the SPI library:
int gMaxChan = 0; //maximum number of channels supported by ads129n = 4,6,8
int gIDval = 0; //Device ID : lower 5 bits of ID Control Register
int activeSerialPort = 0; //data will be sent to serial port that last sent commands. E.G. bluetooth or USB port
const int kPIN_LED = 13;//pin with in-built light - typically 13, 11 for Teensy 2.0.
#if defined(__SAM3X8E__)
#define isDUE //Detect Arduino Due
#define WiredSerial SerialUSB //Use Due's Native port
#else
#define WiredSerial Serial
#endif
void setup(){
using namespace ADS1298;
//prepare pins to be outputs or inputs
//pinMode(PIN_SCLK, OUTPUT); //optional - SPI library will do this for us
//pinMode(PIN_DIN, OUTPUT); //optional - SPI library will do this for us
//pinMode(PIN_DOUT, INPUT); //optional - SPI library will do this for us
pinMode(IPIN_CS, OUTPUT);
pinMode(PIN_START, OUTPUT);
pinMode(IPIN_DRDY, INPUT);
//pinMode(PIN_CLKSEL, OUTPUT);//*optional
//pinMode(IPIN_RESET, OUTPUT);//*optional
//pinMode(IPIN_PWDN, OUTPUT);//*optional
//start small peripheral interface
SPI.begin();
SPI.setBitOrder(MSBFIRST);
#ifndef isDUE
SPI.setClockDivider(SPI_CLOCK_DIV4); //http://forum.pjrc.com/threads/1156-Teensy-3-SPI-Basic-Clock-Questions
#endif
SPI.setDataMode(SPI_MODE1);
//Start ADS1298
delay(500); //wait for the ads129n to be ready - it can take a while to charge caps
adc_send_command(SDATAC); // Send SDATAC Command (Stop Read Data Continuously mode)
delay(10);
// Determine model number and number of channels available
gIDval = adc_rreg(ID); //lower 5 bits of register 0 reveal chip type
switch (gIDval & B00011111 ) { //least significant bits reports channels
case B10000: //16
gMaxChan = 4; //ads1294
break;
case B10001: //17
gMaxChan = 6; //ads1296
break;
case B10010: //18
gMaxChan = 8; //ads1298
break;
case B11110: //30
gMaxChan = 8; //ads1299
break;
default:
gMaxChan = 0;
}
//start serial port
SerialUSB.begin(9600); //use native port on Due
//WiredSerial.begin(9600); //use native port on Due
while (WiredSerial.read() >= 0) {} //http://forum.arduino.cc/index.php?topic=134847.0
//while (!WiredSerial) ; //required by Leonardo http://arduino.cc/en/Serial/IfSerial (ads129n requires 3.3v signals, Leonardo is 5v)
delay(200); // Catch Due reset problem
pinMode(kPIN_LED, OUTPUT);
}
void loop()
{
WiredSerial.print("Device Type (ID Control Register): "); SerialUSB.print(gIDval); SerialUSB.print(" Channels: "); SerialUSB.println(gMaxChan);
digitalWrite(kPIN_LED, HIGH); // turn the LED on (HIGH is the voltage level)
if (gMaxChan > 0)
delay(500); //long pause if OK
else
delay(50); //rapid blink if error
digitalWrite(kPIN_LED, LOW); // turn the LED off by making the voltage LOW
delay(500);
}
Try using Serial instead of SerialUSB, I've tried it while using Arduino UNO.

Resources