I'm doing a code where the timer gets triggered by a pushbutton. The problem is that nothing happens, the serial monitor doesn't show anything. I've checked all of the connections and there is no errors in the electronic components. I'm not sure what is wrong.
Thankyou
Here's the code:
int timer;
const int starttrigger = 2;
const int endtrigger = 4;
void setup() {
pinMode(starttrigger, INPUT);
pinMode(endtrigger, INPUT);
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
}
void loop() {
int start_button_state = digitalRead(starttrigger);
int end_button_state = digitalRead(endtrigger);
if (start_button_state == HIGH) {
while (end_button_state == LOW){
for(timer = 0; end_button_state == HIGH; timer ++) {
Serial.println(timer);
}
}
}
}
You do not see anything in the serial monitor because you have not activating Serial communication in the first place. It needs to be done in the void setup() function.
Please add this line to your void setup function
Serial.begin(9600);
I'm in a pickle regarding concepts relating to timers. How can I can I operate a "delay" inside a timer? This is the best way I can frame the question knowing full well what I'm trying to do is nonsense. The objective is: I wish to test the pinState condition 2 times (once initially and then 4 seconds later) but this all needs to happen periodically (hence a timer).
The platform is NodeMCU running a WiFi (ESP8266 chip) and coding done inside Arduino IDE.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
char auth[] = "x"; //Auth code sent via Email
char ssid[] = "x"; //Wifi name
char pass[] = "x"; //Wifi Password
int flag=0;
void notifyOnFire()
{
int pinState = digitalRead(D1);
if (pinState==0 && flag==0) {
delay(4000);
int pinStateAgain = digitalRead(D1);
if (pinStateAgain==0) {
Serial.println("Alarm has gone off");
Blynk.notify("House Alarm!!!");
flag=1;
}
}
else if (pinState==1)
{
flag=0;
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(D1,INPUT_PULLUP);
timer.setInterval(1000L,notifyOnFire);
}
void loop()
{
//Serial.println(WiFi.localIP());
Blynk.run();
timer.run();
}
an easy fix would be to set the periodicity of the timer to be 4000L timer.setInterval(4000L,notifyOnFire); and in notifyOnFire use a static variable and toggle its value whenever notifyOnFire is called
void notifyOnFire()
{
static char state = 0;
if( state == 0)
{
/* Write here the code you need to be executed before the 4 sec delay */
state = 1;
}
else
{
/* Write here the code you need to be executed after the 4 sec delay */
state = 0;
}
}
The nice thing about static variables is that they are initialized only once at compile time and they retain their values after the scope of code changes (In this case function notifyOnFire exits).
i'm trying to make a live tracking for car. i used GPS Shield and GPRS Shield. it works to get GPS latitude and longatitude, and it works to store the data to my database. But, the problem is, when it get the gps coordinates, then my GPS Shield Turning off automatically when the GPRS Shield finished store the data in my database.
I use GPS/GSM/GPRS Shield from DFRobotcan and GPRS Shield from SeedStudio. i can't used my GPRS from DFRobot because i can't get signal. it's not the problem for me.
the problem is why my GPS shield shuting down when the GPRS Shield done to store the data? anyone help me
Here's my amateur code.
#include <SoftwareSerial.h>
#include <String.h>
double lati, longati, latitudefix, longatitudefix;
String latu, longatu, latdir, londir, latitudebener, longatitudebener;
SoftwareSerial mySerial(7, 8);
String id="1310220001";
void setup()
{
mySerial.begin(19200); // the GPRS baud rate
pinMode(3,OUTPUT);//The default digital driver pins for the GSM and GPS mode
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
digitalWrite(5,HIGH);
delay(1500);
digitalWrite(5,LOW);
digitalWrite(3,LOW);//Enable GSM mode
digitalWrite(4,HIGH);//Disable GPS mode
delay(2000);
Serial.begin(9600);
delay(5000);//GPS ready
Serial.println("AT");
delay(2000);
//turn on GPS power supply
Serial.println("AT+CGPSPWR=1");
delay(1000);
//reset GPS in autonomy mode
Serial.println("AT+CGPSRST=1");
delay(1000);
digitalWrite(4,LOW);//Enable GPS mode
digitalWrite(3,HIGH);//Disable GSM mode
delay(2000);
Serial.println("$GPGGA statement information: ");
}
void loop()
{
while(1)
{
Serial.print("UTC:");
UTC();
Serial.print("Lat:");
latitude();
Serial.print("Dir:");
lat_dir();
Serial.print("Lon:");
longitude();
Serial.print("Dir:");
lon_dir();
Serial.print("Alt:");
altitude();
convert();
//Serial.println(latitudebener);
//Serial.println(longatitudebener);
Serial.println(' ');
Serial.println(' ');
if(latitudebener!="0.00000"&&longatitudebener!="0.00000"){
sendlocation();
}
else{
};
}
}
void convert(){
char bufferlat[20];
latitudebener=dtostrf(latitudefix,5,5,bufferlat);
char bufferlon[20];
longatitudebener=dtostrf(longatitudefix,5,5,bufferlon);
}
void sendlocation(){
mySerial.println("AT+CSQ");
delay(100);
mySerial.println("AT+CGATT?");
delay(100);
mySerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
delay(1000);
mySerial.println("AT+SAPBR=3,1,\"APN\",\"AXIS\"");//setting the APN, the second need you fill in your local apn server
delay(4000);
mySerial.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
delay(2000);;
mySerial.println("AT+HTTPINIT"); //init the HTTP request
delay(2000);
mySerial.println("AT+HTTPPARA=\"URL\",\"www.hamasa.cu.cc/editlocation.php?id="+id+"&latitude="+latitudebener+"&longatitude="+longatitudebener+"&latdir="+latdir+"&londir="+londir+"\"");
delay(2000);
mySerial.println("AT+HTTPACTION=0");//submit the request
Serial.println("8");
delay(5000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
mySerial.println("AT+HTTPREAD");// read the data from the website you access
delay(300);
mySerial.println("");
delay(100);
}
//void sms()
//{
// mySerial.println("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
// delay(100);
// ShowSerialData();
// mySerial.println("AT + CMGS = \"+6283815535385\"");//send sms message,
// //be careful need to add a country code before the cellphone number
//
// delay(100);
// ShowSerialData();
// mySerial.println("bisa bil, latitudenya:"+latitudebener+""+latdir+", terus longatitudenya:"+longatitudebener+""+londir);//the content of the message
// delay(100);
// ShowSerialData();
// mySerial.println((char)26);//the ASCII code of the ctrl+z is 26
// delay(100);
// ShowSerialData();
// mySerial.println();
// ShowSerialData();
//}
void ShowSerialData()
{
while(mySerial.available()!=0)
Serial.write(mySerial.read());
}
double Datatransfer(char *data_buf,char num)//convert the data to the float type
{ //*data_buf:the data array
double temp=0.0; //the number of the right of a decimal point
unsigned char i,j;
if(data_buf[0]=='-')
{
i=1;
//process the data array
while(data_buf[i]!='.')
temp=temp*10+(data_buf[i++]-0x30);
for(j=0;j<num;j++)
temp=temp*10+(data_buf[++i]-0x30);
//convert the int type to the float type
for(j=0;j<num;j++)
temp=temp/10;
//convert to the negative numbe
temp=0-temp;
}
else//for the positive number
{
i=0;
while(data_buf[i]!='.')
temp=temp*10+(data_buf[i++]-0x30);
for(j=0;j<num;j++)
temp=temp*10+(data_buf[++i]-0x30);
for(j=0;j<num;j++)
temp=temp/10 ;
}
return temp;
}
char ID()//Match the ID commands
{
char i=0;
char value[6]={
'$','G','P','G','G','A' };//match the gps protocol
char val[6]={
'0','0','0','0','0','0' };
while(1)
{
if(Serial.available())
{
val[i] = Serial.read();//get the data from the serial interface
if(val[i]==value[i]) //Match the protocol
{
i++;
if(i==6)
{
i=0;
return 1;//break out after get the command
}
}
else
i=0;
}
}
}
void comma(char num)//get ','
{
char val;
char count=0;//count the number of ','
while(1)
{
if(Serial.available())
{
val = Serial.read();
if(val==',')
count++;
}
if(count==num)//if the command is right, run return
return;
}
}
void UTC()//get the UTC data -- the time
{
char i;
char time[9]={
'0','0','0','0','0','0','0','0','0'
};
double t=0.0;
if( ID())//check ID
{
comma(1);//remove 1 ','
//get the datas after headers
while(1)
{
if(Serial.available())
{
time[i] = Serial.read();
i++;
}
if(i==9)
{
i=0;
t=Datatransfer(time,2);//convert data
t=t+70000.00;//convert to the chinese time GMT+8 Time zone
Serial.println(t);//Print data
return;
}
}
}
}
void latitude()//get latitude
{
char i;
char lat[10]={
'0','0','0','0','0','0','0','0','0','0'};
if( ID())
{
comma(2);
while(1)
{
if(Serial.available())
{
lat[i] = Serial.read();
i++;
}
if(i==10)
{
i=0;
char buzz[20];
lati=Datatransfer(lat,5);
latu=dtostrf(lati,5,5,buzz);
String latijam=latu.substring(0,1);
String latimin=latu.substring(1,3);
String latidet=latu.substring(4,9);
double latijamd = latijam.toInt();
double latimind = latimin.toInt();
double latidetd = latidet.toInt();
double latmin = latimind+(latidetd*0.00001);
latitudefix= latijamd+(latmin/60);
Serial.println(latitudefix,5);
return;
}
}
}
}
void lat_dir()//get dimensions
{
char i=0,val;
if( ID())
{
comma(3);
while(1)
{
if(Serial.available())
{
val = Serial.read();
latdir=String(val);
Serial.print(latdir);
Serial.println();
i++;
}
if(i==1)
{
i=0;
return;
}
}
}
}
void longitude()//get longitude
{
char i;
char lon[11]={
'0','0','0','0','0','0','0','0','0','0','0' };
if( ID())
{
comma(4);
while(1)
{
if(Serial.available())
{
lon[i] = Serial.read();
i++;
}
if(i==11)
{
i=0;
//Serial.println(Datatransfer(lon,5),5);
char buffer[20];
longati=Datatransfer(lon,5);
longatu=dtostrf(longati,5,5,buffer);
String longatijam=longatu.substring(0,3);
String longatimin=longatu.substring(3,5);
String longatidet=longatu.substring(6,11);
double longatijamd = longatijam.toInt();
double longatimind = longatimin.toInt();
double longatidetd = longatidet.toInt();
double longmin = longatimind+(longatidetd*0.00001);
longatitudefix= longatijamd+(longmin/60);
Serial.println(longatitudefix,5);
return;
}
}
}
}
void lon_dir()//get direction data
{
char i=0,val;
if( ID())
{
comma(5);
while(1)
{
if(Serial.available())
{
val = Serial.read();
londir=String(val);
Serial.print(londir);
Serial.println();
i++;
}
if(i==1)
{
i=0;
return;
}
}
}
}
void altitude()//get altitude data
{
char i,flag=0;
char alt[8]={
'0','0','0','0','0','0','0','0'
};
if( ID())
{
comma(9);
while(1)
{
if(Serial.available())
{
alt[i] = Serial.read();
if(alt[i]==',')
flag=1;
else
i++;
}
if(flag)
{
i=0;
Serial.println(Datatransfer(alt,1),1);//print altitude data
return;
}
}
}
}
try to use external power. is that works?
actually it should be :
digitalWrite(3,LOW);//Enable GSM mode
digitalWrite(4,HIGH);//Disable GPS mode
The problem may be a software serial port conflict, because GPS and GSM shields both use serial ports. In your case you have coupled shields. While receiving a GPS signal, GSM is switched off, and when a signal is acquired, the GPS turns off and GSM begin to work.
I want to move my robot a set number of steps and then have it stop. However the loop just seems to run on infinitely. Is there a mistake in the way that I am using void loop() or perhaps in the way that I have written my 'for' loop?
// walkerForward.pde - Two servo walker. Forward.
// (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
// updated - Joe Saavedra, 2010
#include <Servo.h>
Servo frontServo;
Servo rearServo;
int centerPos = 90;
int frontRightUp = 75;
int frontLeftUp = 120;
int backRightForward = 45;
int backLeftForward = 135;
void moveForward(int steps)
{
for (int x = steps; steps > 0; steps--) {
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(100);
frontServo.write(frontRightUp);
rearServo.write(backLeftForward);
delay(100);
frontServo.write(centerPos);
rearServo.write(centerPos);
delay(100);
frontServo.write(frontLeftUp);
rearServo.write(backRightForward);
delay(100);
}
}
void setup()
{
frontServo.attach(2);
rearServo.attach(3);
}
void loop()
{
moveForward(5);
}
the loop() function is executed within an infinite loop (if you check the main cpp file that ships with the Arduino IDE, you'll see something like this:
int main()
{
setup();
for (;;) {
loop();
}
return 0;
}
So either put the call to your moveForward() function to setup() and make loop() an empty function, or call exit(0); from within loop() after moveForward(). The first approach looks like this:
void setup()
{
frontServo.attach(2);
rearServo.attach(3);
moveForward(5);
}
void loop()
{
}
And the second one looks like this:
void setup()
{
frontServo.attach(2);
rearServo.attach(3);
}
void loop()
{
moveForward(5);
exit(0);
}
Since you probably will want to eventually do more than move the robot just 5 steps, I'd suggest using a variable flag to hold the robot state. It only executes the movement routine when the flag has been set to true.
If you are using serial, when a move command is received (and the number of steps, direction perhaps?) you set the flag to true and then issue the move command. If you are using sensors or buttons, the same logic applies.
You will need some logic to handle an incoming movement command while a movement is occurring (though with your tight movement loop you actually won't be able to respond to incoming commands unless you use interrupts - but you want to consider this sort of thing if you are planning to build out a full movement bit of firmware).
boolean shouldMove;
void setup()
{
shouldMove = true;//set the flag
}
void loop()
{
if (shouldMove){
moveForward(5);
}
}
void moveForward(int steps)
{
shouldMove = false; //clear the flag
for (int x = steps; steps > 0; steps--) {
// tight loop controlling movement
}
}
}
Am I complicating things?
I'm architecting my code to talk from a 8051 micro to a peripheral device over UART. The peripheral responds to commands from the host and can only respond to one command at a time. It's a simple send and receive protocol. (tx1, rx1, tx2, rx2, tx3, rx3) Each TX message is terminated with a CR, each response is terminated with a >. I can't send a new message until I receive the response to the last one. Responses can also echo print the original TX message in the beginning if I enable that option (but this causes more traffic)
An example message would be:
TX: Hello
RX: World!>
Or with echo option...
TX: Hello
RX: Hello\rWorld!>
Option A
A function such as getHello would consist of both the send and receive. A parallel ISR routine would gather the incoming bytes and throw a flag when the '>' character is received.
char* getHello(char * buf){
sendMsg("Hello\r");
delay(10ms); //wait a little bit
//wait for receive to come in or timeout to occur
while(!receiveFlag || !timeoutFlag); //thrown by ISR
receiveMsg(buf);
//parse the message and do some other stuff
return buf;
}
Pros:
Everything is contained within one function.
Easier to debug
Cons:
This function is blocking and could hang if the peripheral never responds so a timeout must be implemented.
Messages can't be received out of order, must be in series (ie, tx1, rx1, tx2, rx2, tx3, rx3)
Option B
A parallel approach is taken. Two separate functions would created. one to send the message, and one that would vertexed upon receiving a response from the ISR.
void sendHello(){
sendMsg("Hello\r");
//do some other stuff if needed
}
char* receiveMsg(char * buf){
//figure out from echo print what the tx message was
//use a switch statement to decide which response parser to call
switch(txMessage){ //pseudo code
case "Hello":
receiveMsg(buf);
//parse the message and do some other stuff
break;
}
return buf;
}
Pros:
Can handle parallel messages coming back out of order because it relies on the echo printing of the tx message to figure out how to parse it. (ie, tx1, tx2, tx3, rx1,rx2,rx3)
Cons:
quite difficult to debug
spawns multiple threads
lots of extra code
not worth it since messages will definitely come back in order
Right now, I'm doing Option B, but as I continue on with the project, I begin to feel like this is getting overly complex. I'm curious what you guys think.
Thanks!
I tend to do this kind of stuff, though, Id tend to have a separate serial port "class" ( struct + functions ) and a protocol class that lives over the top of serial port. I used these all the time in my embedded systems. This gives you the best of both worlds, a blocking synchronous call and an async call so you can pseudo multitask.
typedef struct serial_port_s serial_port;
typedef void (*serial_on_recived_proc)(serial_port* p);
typedef struct serial_port_s{
bool timeoutFlag;
bool receiveFlag;
void* context;
serial_on_recived_proc response_handler;
};
void send_serial(serial_port* p, char* message)
{
//SendMsg?
}
void receive_serial(serial_port* p, char* response)
{
//receiveMsg?
}
bool has_data(serial_port* p)
{
return p->receiveFlag;
}
bool has_timed_out(serial_port* p)
{
return p->timeoutFlag;
}
bool is_serial_finished(serial_port* p)
{
return has_data(p) || has_timed_out(p);
}
bool serial_check(serial_port* p)
{
if(is_serial_finished(p) && p->response_handler != NULL)
{
p->response_handler(p)
p-> response_handler = NULL;
return true;
}
return false;
}
void send(serial_port* p, char* message, char* response)
{
p->response_handler=NULL;
send_serial(p, message);
while(!is_serial_finished(p));
receive_serial(p, response);
}
void sendAsync(serial_port* p, char* message, serial_on_recived_proc handler, void* context)
{
p->response_handler = handler;
p->context = context;
send_serial(p, message);
}
void pow_response(serial_port* p)
{
// could pass a pointer to a struct, or anything depending on what you want to do
char* r = (char*)p->context;
receive_serial(p, r);
// do stuff with the pow response
}
typedef struct
{
char text[100];
int x;
bool has_result;
} bang_t;
void bang_parse(bang_t* bang)
{
bang->x = atoi(bang->text);
}
void bang_response(serial_port* p)
{
bang_t* bang = (bang_t*)p->context;
receive_serial(p, bang->text);
bang_parse(bang);
bang->has_result=true;
}
void myFunc();
{
char response[100];
char pow[100];
bang_t bang1;
bang_t bang2;
serial_port p; //
int state = 1;
// whatever you need to do to set the serial port
// sends and blocks till a response/timeout
send(&p, "Hello", response);
// do what you like with the response
// alternately, lets do an async send...
sendAsync(&p, "Pow", pow_response, pow);
while(true)
{
// non block check, will process the response when it arrives
if(serial_check(p))
{
// it has responded to something, we can send something else...
// using a very simple state machine, work out what to send next.
// in practice I'd use enum for states, and functions for managing state
// transitions, but for this example I'm just using an int which
// I just increment to move to the next state
switch(state)
{
case 1:
// bang1 is the context, and will receive the data
sendAsync(&p, "Bang1", bang_response, &bang1);
state++;
break;
case 2:
// now bang2 is the context and will get the data...
sendAsync(&p, "Bang2", bang_response, &bang2);
state++;
break;
default:
//nothing more to send....
break;
}
}
// do other stuff you want to do in parallel
}
};
Take things simple. An ISR routine must be very fast so for me the best approch is to have a global RXBuffer like this:
#include <cstdint>
#include <deque>
#include <algorithm>
class RXBuffer {
public:
friend class ISR;
typedef std::deque<uint8_t>::const_iterator const_iterator;
RXBuffer();
size_t size() const { m_buffer.size(); }
// read from the buffer in a container in the range [first, last)
template <typename Iterator>
void read(Iterator first, Iterator last, Iterator to)
{
// how many bytes do you want to read?
size_t bytes_to_read = std::distance(first, last);
if (bytes_to_read >= size())
{
// read the whole buffer
std::copy(begin(), end(), first);
// empty the buffer
m_buffer.clear();
return size();
}
else
{
// copy the data
copy(begin(), begin() + bytes_to_read, firt);
// now enque the element
m_buffer.erase(begin(), begon() + bytes_to_read);
return bytes_to_read;
}
}
private:
void put(uint8_t data)
{
// check buffer overflow
m_buffer.push_back(data);
}
const_iterator begin() const { return m_buffer.begin(); }
const_iterator end() const { return m_buffer.end(); }
private:
std::deque<uint8_t> m_buffer; // buffer where store data
size_t m_size; // effective size of the container
};
class ISR {
public:
ISR(RXBuffer& buffer) : m_buffer(buffer) {}
// ISR Routine
void operator () (uint8_t data)
{
m_buffer.put(data);
}
private:
RXBuffer& m_buffer;
};
RXBuffer g_uart1_rx_buffer;
Now you have the ISR and a RXBuffer where search data, so you need something to wrap the UART functions. You can implement as follow:
class UART {
public:
UART(unsigned int uart_device, RXBuffer& rx_buffer) :
m_uart(uart_device), m_buffer(rx_buffer)
{
}
unsigned int uart_device() const { return m_uart; }
// set the timeout during a read operation
void timeout(unsigned ms) { m_timer.countdown(ms); }
template <typename InputIterator>
void read(InputIterator first, InputIterator last)
{
// start the timer
m_timer.start();
size_t size = std::distance(first, last);
size_t read_bytes = 0;
while (read_bytes != size && !m_timer.is_expired())
{
read_bytes += m_buffer.read(first + read_bytes, last);
}
if (read_bytes != size) throw std::exception("timeout");
}
template <typename OutputIterator>
void send(OutputIterator first, OutputIterator last)
{
size_t size = std::distance(first, last);
uart_send(m_uart, &(*first), size);
}
private:
unsigned int m_uart;
RXBuffer& m_buffer;
timer m_timer;
};