I have uploaded program in Atmega328P-PU by using Arduino Uno which stores the flex sensor readings( Four Sensors) and i want to send these readings to Nxp Ucode Ic using I2C protocol. I have assigned pin no. 23,24,25,26 for flex sensors and pin no. 27,28 for i2c protocol. Finally i will be using only atmega328P-PU with Nxp ic to send the readings to reader and then to PC or mobile. So please help me with my code. This is my current code:-
#include <EEPROM.h>
#include <Wire.h>
int addr = 0;
int sensorValue0=0;
int sensorValue1=0;
int sensorValue2=0;
int sensorValue3=0;
int sensorUSB=0;
int sensorMSB=0;
int sensorLSB=0;
char t;
int a=0;
int i1=0;
char b[4];
String str;
void setup()
{
//Wire.begin(2);
Serial.begin(9600);
}
void loop()
{
if(i1==0)
{sensorValue0 = analogRead(A0);}
if(i1==1)
{
sensorValue0 = analogRead(A1);
}
if(i1==2)
{
sensorValue0 = analogRead(A2);
}
if(i1==3)
{
sensorValue0 = analogRead(A3);
}
a=sensorValue0;
a=sensorValue0;
str=String(a);
str.toCharArray(b,4);
if(sensorValue0 > 1000)
{Serial.write('N');
EEPROM.write(addr, 'N');
addr=addr+1;}
else{
if(sensorValue0 < 100)
{Serial.write('N');
EEPROM.write(addr, 'N');
addr=addr+1;}
else
{Serial.write(b);
EEPROM.write(addr, b);
addr=addr+1;} // respond with message of 6 bytes
}//a=a+1;
delay(100);
// Wire.write(t);
//Wire.onRequest(requestEvent);
//delay(100);
//Wire.onRequest(requestEvent);
delay(3000);
i1=i1+1;
if(i1==5)
{i1=0;}
}
//void requestEvent()
// {
//}
will this code work for transmitting the sensor readings to NXp IC… Please help me…
Moderator edit: </mark> <mark>[code]</mark> <mark>
In case the first assignment didn't work, do it again. What if the second one doesn't work?
EEPROM.write(addr, b);
b is an array. How is this function supposed to know how many bytes to write?
addr=addr+1;} // respond with message of 6 bytes
Nonsense. That is NOT what this code does.
The Tools menu contains an item, Auto Format, for a reason. Learn what that reason is, and use the function. Your code looks like crap.
will this code work for transmitting the sensor readings to NXp IC
You're the one with the hardware. You test it. But, I'd guess that no it won't work. Saving the data in EEPROM as strings with variable numbers of characters is not the best use of EEPROM.
Hey guys thank you for supporting me i have modified my code and i am successful in transferring data to NXP and i just wanted some minor changes in the code. I am using four flex sensor so can you please help how can i transfer its reading to NXP ucode.And i want to set a limit means if it exceed certain value the message POSSIBLE UCLERS should be display on reader
Guys can you help with this? smiley
Code:
#include <Wire.h>
#define NXP 0x51 //NXP chip address is 101 0001
const int numberOfSensors = 4;
int sensorPins [numberOfSensors] = {
0, 1, 2, 3}; //Analog input pins 0-3
int pullupPins [numberOfSensors] = {
23, 24, 25, 26}; //Digital names for analog input pins
void setup() {
for(int i = 0; i < numberOfSensors; i++) // set analog pins as inputs, although this is also the default
pinMode(sensorPins, INPUT);
for(int i = 0; i < numberOfSensors; i++) // set internal pullup resistors for all analog pins in use
digitalWrite(pullupPins, HIGH);
Serial.begin(9600);
Wire.begin();
}
void loop() {
unsigned int address = 0x6000; // The address access for I2C and RFID are different for the same bank. For user memory
// I2C address starts from 6000h
for (int count = 0; count < numberOfSensors; count++) {
//int sensorReading = analogRead(sensorPins[count]); // read each sensor as deffined in sensorPins array
int sensorReading = 111;
if(sensorReading >1000)
{Wire.write('N');}
else{
if(sensorReading <100)
{Wire.write('N');}
else
{
byte sensorReading_H = highByte (sensorReading); //High 8-bit of sensorReading
byte sensorReading_L = lowByte (sensorReading); //Low 8-bit of sensorReading
Wire.beginTransmission(NXP);
Wire.write((int)(address>>smiley-cool); //MSB
Wire.write((int)(address&0xFF)); //LSB
//Send two bytes of data
Wire.write (sensorReading_H);
Wire.write (sensorReading_L);
Wire.endTransmission();}
Serial.print(sensorReading, DEC); // print its value out decimally
if (count < numberOfSensors - 1) Serial.print(","); // if this isn't the last sensor to read then print a comma after it
}
Serial.println(); // after all the sensors have been read print a newline and carriage return
delay(10); // delay by # milliseconds
//int sensorReading = analogRead(sensorPins[count]); // read each sensor as deffined in sensorPins array
int sensorReading = 111;
if(sensorReading >1000)
Wire.write has to be after Wire.beginTransmission, which it isn’t here.
for(int i = 0; i < numberOfSensors; i++) // set analog pins as inputs, although this is also the default
pinMode(sensorPins, INPUT);
for(int i = 0; i < numberOfSensors; i++) // set internal pullup resistors for all analog pins in use
digitalWrite(pullupPins, HIGH);
Gives:
sketch_sep26a.ino: In function ‘void setup()’:
sketch_sep26a:12: error: invalid conversion from ‘int*’ to ‘uint8_t’
sketch_sep26a:12: error: initializing argument 1 of ‘void pinMode(uint8_t, uint8_t)’
sketch_sep26a:14: error: invalid conversion from ‘int*’ to ‘uint8_t’
sketch_sep26a:14: error: initializing argument 1 of ‘void digitalWrite(uint8_t, uint8_t)’
OK, now post your real code. This is missing some stuff like:
This was the code i have tested and was able to send data to NXP CODE
#include <Wire.h>
#define NXP 0x51 //NXP chip address is 101 0001
const int numberOfSensors = 4;
int sensorPins [numberOfSensors] = {
0, 1, 2, 3}; //Analog input pins 0-3
int pullupPins [numberOfSensors] = {
14, 15, 16, 17}; //Digital names for analog input pins 0-3
void setup() {
for(int i = 0; i < numberOfSensors; i++) // set analog pins as inputs, although this is also the default
pinMode(sensorPins[i], INPUT);
for(int i = 0; i < numberOfSensors; i++) // set internal pullup resistors for all analog pins in use
digitalWrite(pullupPins[i], HIGH);
Serial.begin(9600);
Wire.begin();
}
void loop() {
unsigned int address = 0x6000; // The address access for I2C and RFID are different for the same bank. For user memory
// I2C address starts from 6000h
for (int count = 0; count < numberOfSensors; count++) {
int sensorReading = analogRead(sensorPins[count]); // read each sensor as deffined in sensorPins array
if(sensorReading >1000)
{Wire.write('N');}
else{
if(sensorReading <100)
{Wire.write('N');}
else
byte sensorReading_H=highByte(sensorReading); //High 8-bit of sensorReading
byte sensorReading_L=lowByte(sensorReading); //Low 8-bit of sensorReading
Wire.beginTransmission(address);
Wire.write((int)(address>>8)); //MSB
Wire.write((int)(address&0xFF)); //LSB
//Send two bytes of data
Wire.write(sensorReading_H);
Wire.write(sensorReading_L);
Wire.endTransmission();
}
Serial.print(sensorReading, DEC); // print its value out decimally
if (count < numberOfSensors - 1) Serial.print(","); // if this isn't the last sensor to read then print a comma after it
}
Serial.println(); // after all the sensors have been read print a newline and carriage return
delay(10); // delay by # milliseconds
}
unsigned int address = 0x6000; // The address access for I2C and RFID are different for the same bank. For user memory
...
Wire.beginTransmission(address);
I2C addresses are a single byte.
Each device needs to have a unique address in the range 8 to 119.
Guys This code is working i can see the readings of four flex sensors using nxp ucode ic on reader. Finally i just want to show ulcer message on reader when the readings exceeds 1000 or is less then 100 can someone please suggest me the changes i need to make in this program?
#include <Wire.h>
#define NXP 0x51 //NXP chip address is 101 0001
const int numberOfSensors = 4;
int sensorPins [numberOfSensors] = {
0, 1, 2, 3}; //Analog input pins 0-3
int pullupPins [numberOfSensors] = {
23, 24, 25, 26}; //Digital names for analog input pins
void setup() {
for(int i = 0; i < numberOfSensors; i++) // set analog pins as inputs, although this is also the default
pinMode(sensorPins[i], INPUT);
for(int i = 0; i < numberOfSensors; i++) // set internal pullup resistors for all analog pins in use
digitalWrite(pullupPins[i], HIGH);
Serial.begin(9600);
Wire.begin();
}
void loop() {
unsigned int address = 0x6000; // The address access for I2C and RFID are different for the same bank. For user memory
// I2C address starts from 6000h
for (int count = 0; count < numberOfSensors; count++) {
//int sensorReading = analogRead(sensorPins[count]); // read each sensor as deffined in sensorPins array
int sensorReading = 111;
if(sensorReading >1000)
{Wire.write('N');}
else{
if(sensorReading <100)
{Wire.write('N');}
else
{
byte sensorReading_H = highByte (sensorReading); //High 8-bit of sensorReading
byte sensorReading_L = lowByte (sensorReading); //Low 8-bit of sensorReading
Wire.beginTransmission(NXP);
Wire.write((int)(address>>8)); //MSB
Wire.write((int)(address&0xFF)); //LSB
//Send two bytes of data
Wire.write (sensorReading_H);
Wire.write (sensorReading_L);
Wire.endTransmission();}
Serial.print(sensorReading, DEC); // print its value out decimally
if (count < numberOfSensors - 1) Serial.print(","); // if this isn't the last sensor to read then print a comma after it
}
Serial.println(); // after all the sensors have been read print a newline and carriage return
delay(10); // delay by # milliseconds
}}