I'm working on project with several components. But now I'm with big problem.
The RTC and the Ethernet Shield are using the same analogic pin: 2
Is there a way to modify it on the Ethernet Shield library????
Just a part of the RTC:
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
#include <Ultrasonic.h>
#include <Wire.h>
#include <RTClib.h>
Serial.begin(57600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
The Ethernet Shield is not using pin 2, it uses pins 10 (SS for Ethernet), 4 (SS for SD card) and MISO, MOSI and SCKL of the ICSP header (pins 11, 12 and 13 on the UNO).
And don't post snippets, post full sketches. If you code is too big try to reduce the sketch to the smallest part that still shows the problem. We usually don't comment on code snippets because the surrounding code may influence it more than you might think.
The observation is Portuguese, but now I'm "killing" the ethernet shield code. The program cause of it.
The code is a Pet Feeder, he gets time from RTC, check the weight on the balance, quantity of food on sotck by ultrasonic and after send an e-mail with status.
// INCLUSÃO DAS BIBLIOTECAS
#include <SPI.h> //Biblioteca geral
#include <Ethernet.h> // Bibiblioteca Ethernet Shield
#include <Servo.h> //Biblioteca para Servo Motor
#include <Ultrasonic.h> //Biblioteca para o sensor ultrasonico
#include <Wire.h> // Biblioteca para o Real Time Clock
#include <RTClib.h> //Biblioteca para o Real Time Clock
RTC_DS1307 RTC;
// CONFIGURAÇÃO DO SENSOR ULTRASSOM
#define echoPin 9 //Pino 1 recebe o pulso do echo
#define trigPin 8 //Pino 2 envia o pulso para gerar o echo
Ultrasonic ultrasonic(8,9);
// CONFIGURAÇÃO ETHERNET
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Endereço MAC do controlador
byte ip[] = { 192, 168, 0, 4 }; // IP usado pelo Arduino na rede - VERIFICAR!!!
byte server[] = { 200, 98, 199, 90 }; // IP do servidor SMTP UOL Host para envio de emails - VERIFICAR!!!
// CONFIGURAÇÃO DE HORÁRIO INICIAL PARA ALIMENTAÇÃO
int hconst = 3; // constante para definição de hora 00 hrs - 23 hrs (08 = octal constant)
int mconst = 29; // constante para definição de minuto 00 min - 59 min
// VARIÁVEIS DO SISTEMA
int horas = 0; // Variável de armazenamento horas recebidas
int minutos = 0; // Variável de armazenamento minutos recebidas
int distancia; // Variável de armazenamento distância do sensor ultrassom
int pos = 0; // Variavel para armazenar a posição do servo
int time = 5000; // Constante de tempo 1
int wait = 2000; // Constante de tempo 2
Servo myservo; // Cria um objeto do tipo myservo para acessar os metodos
EthernetClient client; // Ethernet shield como cliente
// VARIÁVEIS DO SENSOR RESISTIVO
int fsrPin = 0; // Sensor Resistivo e resistor de pulldown conectados ao a0
int fsrReading; // Leitura analógica do divisor FSR - resistor
int fsrVoltage; // Leitura analógica convertido em voltagem
unsigned long fsrResistance; // Voltagem convertida para resistância
unsigned long fsrConductance; // Voltagem convertida para condutância
long fsrForce; // Finalmente, a resistência convertida em força
// CONFIGURAÇÕES DE SETUP
void setup()
{
// CONFIGURAÇÃO SENSOR ULTRASSOM E SERVO MOTOR
pinMode(echoPin, INPUT); // define o pino 9 como entrada (recebe)
pinMode(trigPin, OUTPUT); // define o pino 8 como saida (envia)
myservo.attach(5); // atribui o pino 5 do arduino para ser controlado pelo objeto
// ABRIR COMUNICAÇÃO SERIAL
Serial.begin(57600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// INICIANDO ETHERNET
/* if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}*/
}
// CONFIGURAÇÃO LOOP DA PARTE LÓGICA DE PROGRAMAÇÃO
void loop()
{
DateTime now = RTC.now();
horas = now.hour();
Serial.print(horas, DEC);
Serial.print("h:");
minutos = now.minute();
Serial.print(minutos, DEC);
Serial.println("m");
// COMPARAÇÃO DE HORÁRIO ADQUIRIDO COM O PROGRAMADO NAS VARIÁVEIS HCONST E MCONST
if (horas == hconst)//Primeiro verifica a hora
{
if (minutos == mconst)//Em seguida é feito a verificação dos minutos
{
// APÓS COMPARAÇÃO DO HORARIO, É INICIADA A ALEITURA DO NÍVEL DE RAÇÃO NO RESERVATÓRIO
//seta o pino 6 com um pulso baixo "LOW" ou desligado ou ainda 0
digitalWrite(trigPin, LOW);
// delay de 2 microssegundos
delayMicroseconds(2);
//seta o pino 6 com pulso alto "HIGH" ou ligado ou ainda 1
digitalWrite(trigPin, HIGH);
//delay de 10 microssegundos
delayMicroseconds(10);
//seta o pino 6 com pulso baixo novamente
digitalWrite(trigPin, LOW);
// função Ranging, faz a conversão do tempo de
//resposta do echo em centimetros, e armazena
//na variavel distancia
distancia = (ultrasonic.Ranging(CM));
Serial.print("Distancia em CM: ");//Exibe mensagem da distancia
Serial.println(distancia);//Exibe o valor da distancaia
Serial.flush();
delay(1000);
About the flush i'm using to clean the serial. Is not good to avoid any information that I don't need?
Depending on which version of the IDE you are using flush() either blocks until the outgoing serial buffer is empty OR it dumps random amounts of unread data from the serial port.
Neither sound like they are helpful in your situation. If you are using 0023 or earlier, explain how throwing away random amounts of unread data could possibly help. If you are using 1.0+, explain how blocking until the outgoing buffer is empty helps. If the outgoing buffer gets full, print(), println(), and write() will block, anyway, until there is room in the outgoing buffer.
You didn't specify the type of the RTC module you're using. Can you post a link? In your wiring diagram you don't have I2C pull-ups so they might be integrated into the RTC module (bad design, no I2C module should include the pull-ups because the bus must have them only once). You also don't show the power supply. Do you drive the whole setup from the Arduino? Where does the Arduino's power come from? USB bus? External power supply?
Did you measure the voltage you have on your breadboard power pins? Is it exactly 5V? Or above? Or below?