problem is int min redeclared as a different kind of symbol;
Try to check the code and change the whole number, but I have the same problem, regardless of whether I change it to 0 1 2 3 4. (Sorry, I'm not good at English and just started to learn coding from education through Google and YouTube.)
This is my code:
int int_count;
int sec=0;
int min=1;
int flag_One_Time_Send_Old = 100;
void timer0_ISR(void) // the RTCC (timer0) overflows (255->0).
if(--int_count==0) // per second.
sec++;
int_count=INTS_PER_SECOND;
if (sec==60)
{
min++;
sec=0;
Serial.print("min: ");
Serial.println(min);
}
Serial.print("sec: ");
Serial.println(sec);
}
Thank for help
I hope this works for you. min has been renamed as minute as min is used as min() macro function by Arduino.
int int_count; int sec=0; int minute=1; //min renamed as minute
int flag_One_Time_Send_Old = 100;
void timer0_ISR(void) // the RTCC (timer0) overflows (255->0).
if(--int_count==0) // per second.
sec++;
int_count=INTS_PER_SECOND;
if (sec==60)
{
minute++;
sec=0;
Serial.print("minutes: ");
Serial.println(minute);
}
Serial.print("sec: ");
Serial.println(sec);
}
Related
I would like to stop DC motors after the counter reaches a value e,g num=3 (HIGH's) as shown in the following image:
White line is 3cm thick, black box 30cm squared!
The two IR sensors, int rightSens=30; and int leftSens=32; are working and give a 1 on sensing a black surface, and 0 on any other, i.e. white and in the absence of anything.
I would like the robot to count as in my loop, 3 LOWs incase counting white or 2 HIGHs incase of counting black,then do something e.g. stop, as shown with the while loop.
Something must be wrong, it seems like not counting because it doesn't stop after passing 3 boxes or 3 white lines. any help very appreciated!
int pwm=2; //initializing pin 2 as pwm
int leftMotorin_1=22;//right
int leftMotorin_2=24;
int pwm2=3;
int rightMotorin_3=26;
int rightMotorin_4=28;
//sensor
int rightSens=30;
int leftSens=32;
int rightSensState;
int leftSensState;
//Variables for counting lines
int previousState=0;
int currentState=0;
void setup ()
{
pinMode(pwm,OUTPUT); // set pwm pin as output
pinMode(leftMotorin_1,OUTPUT); //logic pins are also set as output
pinMode(leftMotorin_2,OUTPUT);
pinMode(pwm2,OUTPUT); // set pwm pin as output
pinMode(rightMotorin_3,OUTPUT); //logic pins are also set as output
pinMode(rightMotorin_4,OUTPUT);
//pinMode(obSens,INPUT);
pinMode(rightSens,INPUT);
pinMode(leftSens,INPUT);
}
void readSensors()
{
rightSensState = digitalRead(rightSens);
leftSensState = digitalRead(leftSens);
}
void moveForward()
{
digitalWrite(leftMotorin_1,HIGH);
digitalWrite(leftMotorin_2,LOW);
analogWrite(pwm,150);
digitalWrite(rightMotorin_3,HIGH);
digitalWrite(rightMotorin_4,LOW);
analogWrite(pwm2,150);
}
void stopWheels()
{
//For brake
digitalWrite(leftMotorin_1,LOW);
digitalWrite(leftMotorin_2,LOW);
digitalWrite(rightMotorin_3,LOW);
digitalWrite(rightMotorin_4,LOW);
}
void loop()
{
int count=0;
int num=3;
moveForward();
readSensors();
(currentState=(leftSensState && rightSensState));
if (currentState != previousState );
{
if (currentState==HIGH)
count++;
previousState=currentState;
}
while (count!=num) {
moveForward();
readSensors();
(currentState=(leftSensState && rightSensState));
if (currentState != previousState );
{
if (currentState==HIGH)
count+=1;
previousState=currentState;
}
if (counter == num)
{
stopWheels();
delay(20);
break;
}
}
}
I was trying to write items to the EEPROM and later read them out. I was finding the reading back I was not getting the same as I put in at times. I narrow down to an example I can show you. Below I read into variables 2 address.
const int start_add_type = (EEPROM.length() - 10);
const int start_add_id = (EEPROM.length() - 4);
I then look at the value (via RS232)
Serial.begin(9600);
Serial.println(start_add_type);
Serial.println(start_add_id);
of them at the start of the setup() and see I get
1014
1020
I then look again at the end
Serial.println(start_add_type);
Serial.println(start_add_id);
and I get
1014
818
I cannot see why this should change. I did try calling them const e.g. const
const int start_add_type = (EEPROM.length() - 10);
const int start_add_id = (EEPROM.length() - 4);
but this gave the same result. So here I sit very puzzled at what I must have missed. Anyone got any idea?
#include "EEPROM.h"
int start_add_type = (EEPROM.length() - 10);
int start_add_id = (EEPROM.length() - 4);
char ID[7] = "ENCPG2";
char Stored_ID[5];
char Input[10];
//String Type;
void setup()
{
Serial.begin(9600);
Serial.println(start_add_type);
Serial.println(start_add_id);
// start_add = (EEPROM.length() - 10); // use this method to be PCB independent.
for (int i = 0; i < 6; i++)
{
Stored_ID[i] = EEPROM.read(start_add_type + i); // Read the ID into the EEPROM.
}
if (Stored_ID != ID) // Check if the one we have got is the same as the one in this code ID[7]
{
for (int i = 0; i < 6; i++)
{
EEPROM.write(start_add_type + i, ID[i]); // Write the ID into the EEPROM.
}
}
Serial.println(start_add_type);
Serial.println(start_add_id);
}
void loop()
{
}
You are overwriting your memory in this loop:
for (int i = 0; i < 6; i++)
{
Stored_ID[i] = EEPROM.read(start_add_type + i);
}
Stored_ID array is 5 bytes long, so writing to Stored_ID[5] will rewrite also the start_add_id variable, thus the weird value 818, which equals to 0x0332 HEX and 0x32 is the '2' character of your ID
For fixing this issue, declare Stored_ID in this way:
char Stored_ID[6];
if (Stored_ID != ID)
This is nonsense: You compare two different addresses, which are never equal. If you want to compare the content, you should do it in a loop. (e.g. directly when reading the EEPROM value into Stored_ID[i] )
Alternatively, Stored_ID could be a 0-terminated text as well and you might use
if (strcmp(Stored_ID, ID) != 0)
just look at the code please , says it all
#include<stdio.h>
#include<string.h>
struct DOB
{
int y,m,d;
}typedef DOB_t;
struct Courses
{
char* NameofCourse;
char* Lec;
int CourseID;
}typedef Course_t;
struct Student
{
char Name[20];
float Grade;
DOB_t dob;
Course_t Courses;
}typedef Student_t;
void addstudent(Student_t* s);
void addDate(DOB_t* dob);
void addCourse(Course_t* c);
main()
{
int opt;
Student_t s;
printf("Please choose one of the options:\n");
printf("<<<<<1.add student>>>>>\n<<<<<2.print student>>>>>\n");
scanf("%d",&opt);
switch(opt)
case 1:
{
addstudent(&s);
break;
}
}
void addstudent(Student_t* s)
{
printf("Enter student's name: ");
scanf("%s",s->Name);
printf("Enter Grade: ");
scanf("%.2f",s->Grade);
printf("Enter DateOfBirth: ");
***addDate(&(s->dob));***
addCourse(&(s->Courses));
}
void addDate(DOB_t* dob)
{
***scanf("%d %d %d", dob->d,dob->m,dob->y);***
}
void addCourse(Course_t* c)
{
printf("Enter Course Name And Lec Name: ");
scanf("%s %s",c->NameofCourse,c->Lec);
printf("Enter Course ID: ");
scanf("%d",c->CourseID);
}
it's not finished yet , but look at the highlighted row - i get an error when i'm triyng to scan values of DateofBirth using pointers into (s->DOB ) to keep the data in the value after function closes.
hope you understand my problem.
scanf requires you to pass the address of the memory space you want to store the result in. In your functions, wherever you are passing pointer to the structure,you should use scanf like this.
void addDate(DOB_t* dob)
{
scanf("%d %d %d", &dob->d,&dob->m,&dob->y); //This would read an integer into the address of pointer plus the offset of member into the structure.
}
You have make this change in your code at every place where you are reading values into structure pointer.
I am trying to keep track of a set of data by declaring a class.
The class is initialized with a unique ID but then fills the rest of the variables out later in the code after some calculations.
First, is that even an acceptable way to do this?
Second, I'm trying to pass it a char array but it does not want to take the value. Is this the correct way to define the fileName and call it back when creating the file?
Here's the example, I define a variable from the Customer class then try to store its filename:
#ifndef customer_h
#define customer_h
class Customer
{
public:
Customer (char *number);
char *ID;
double current;
double voltage;
double powerConsumption;
double remainingCredit;
int relay;
char *lastName;
char *firstName;
char *fileName;
private:
};
Customer::Customer(char *number)
{
ID = number;
}
#endif
void setup()
{
cust1.fileName = getFileName(cust1.ID);
}
char *getFileName(char *customerID)
{
char *charID;
String newID;
for (int i = strlen(customerID)-4; i<= strlen(customerID)-1; i++)
{
newID += customerID[i];
}
newID += ".csv";
int lenID = newID.length() + 1;
char fileName[lenID];
newID.toCharArray(fileName,lenID);
return fileName;
}
Thanks a lot in advance for any help and info you can provide!
Be better to use a structured rather than a class. They are pretty similar but in this case seems like a structure will be much better go and read about the differences and you'll see
I have a QTimeEdit widget on my dialog and I want to provide some kind of autochange - if the cursor is on minutes section and the time is 04:59, the next click on the arrow up would lead the time to change to 5:00.
How to do that?
I saw some mention of AutoAdvance property but I suppose it's obsolete because I cannot find it in Qt 4.7.
I noticed there is a signal called void timeChanged ( const QTime & time ). You can connect it to a slot and call function void QAbstractSpinBox::stepBy ( int steps ) in the slot function.
EDIT1:
Sorry for the misleading. In fact, we don't really need void timeChanged ( const QTime & time ).
See the code below:
class myTime : public QTimeEdit
{
Q_OBJECT
public:
virtual void stepBy(int steps)
{
if (this->time().minute()==59 && steps>0){
setTime(QTime(time().hour()+1,0,time().second(),time().msec()));
}else if(this->time().minute()==00 && steps<0){
setTime(QTime(time().hour()-1,59,time().second(),time().msec()));
}else{
QTimeEdit::stepBy(steps);
}
}
};
Keep in mind, you need to setWrapping(true) yourself.
I don't know whether it's still interesting, but I found another solution:
class myTime : public QTimeEdit {
Q_OBJECT public:
virtual void stepBy(int steps)
{
long lFactor=1;
if (currentSection()==QDateTimeEdit::MinuteSection)
lFactor=60;
else if (currentSection()==QDateTimeEdit::HourSection)
lFactor=3600;
long lDateTime = (dateTime().toMSecsSinceEpoch()/1000)+(steps*lFactor);
QDateTime dt = QDateTime::fromMSecsSinceEpoch(1000*(qint64)(lDateTime));
setDateTime(dt);
} };
If some one is wondering how to automatically change time (because I did), and needs easy, rather clean solution, here's what I came up with:
class TimeWidget : public QTimeEdit {
Q_OBJECT
public:
void stepBy(int steps) {
//Grab the current time
QTime t = time();
//So we edit the time as in raw milliseconds. Larger type would be better in this case.
//But 32bits is more than enough for day amount of milliseconds.
int raw = (t.hour()*360000 + t.minute()*60000 + t.second() * 1000 + t.msec());
//Let's find out what section time widget is changing
//And edit the raw millisecond data accordingly.
switch(currentSection()) {
case QDateTimeEdit::MSecSection:
raw += steps;
break;
case QDateTimeEdit::SecondSection:
raw += steps*1000;
break;
case QDateTimeEdit::MinuteSection:
raw+= steps*60000;
break;
case QDateTimeEdit::HourSection:
raw += steps*3600000;
break;
}
//Now we just split the "raw" milliseconds into
int hrs = (raw/ 36000000) % 24; //... hours
int mins = (raw/ 60000) % 60; //... minutes
int secs = (raw / 1000) % 60; //... seconds
int mills = raw % 1000; //... milliseconds
//Update & save the current time
t.setHMS(hrs, mins, secs, mills);
setTime(t);
}
};