Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Trivial questions on: January 24, 2013, 07:49:12 am
Thank you all!
2  Using Arduino / Programming Questions / Re: Trivial questions on: January 23, 2013, 07:36:17 pm
Quote
Here's an idea.  There are a million and one books which teach C++ programming,  some of them free.

Read one.

 smiley-razz I'm actually trying to get one!

So, #define, just operates a substitution: #define var 11, means that every time i want to call a specific connection to "11" (pins number or just a number inside an equation) i'll insert var...
3  Using Arduino / Programming Questions / Re: Trivial questions on: January 23, 2013, 07:54:22 am
Quote
But that would suck if you want to blink a different pin. You have to change pin number in multiple lines and pray not to make a mistake. But this will work much better.

why not simply define a variable int led=13...? memory usage?

Quote
A char is a variable that can hold one character. A char * is a pointer to a memory location that can hold one or more chars. It must be made to actually point to some memory before it can be used.

could you please make an example in using char*...?
4  Using Arduino / Programming Questions / Trivial questions on: January 22, 2013, 07:22:32 pm
I apologize for the topic...
I have two questions for you:
1) what does
Quote
# define...
means!?
is it a non-ordinary manner in defining variables?

2) what is the difference between char and char* in defining a variable?...

thank you for the help...i wasn't able to find answers anywhere else... smiley-sad 
5  Using Arduino / Programming Questions / Re: four digit 7-segment display on: January 18, 2013, 03:24:36 pm
Quote
I think the last few lines are missing

guess what!
those lines were on the next page...!...
I'm sorry for my carelessness...!
Anyway i found a bug, however...defining the showDigit function, in the For loop, the counter must start from zero, isn't it?...
i have a four digit 7-segment common anode display. The rightmost bit in each element of the bitmap array coincides with the DP led in the display, that has to be always off (i don't have to represent decimal values), and if i put "int segment=1" in the for loop conditions, that led is always lit.

thank you so much for the help!
6  Using Arduino / Programming Questions / four digit 7-segment display on: January 18, 2013, 11:18:31 am
Hi everybody!
i found this recipe on the arduino cookbook. It pretend to drive a four digit 7-segment display, but i'm not shure about that, not at all...i mean, i built the circuit...but as i attempt to represent on the display a more-than-one-digit number...it stops working, and all segments of the digit i need are lit. I think that the point is that through the digit pins, i enable all the segments of a digit to be illuminated, but the bitmap i can send to the digits is the same for all of them...!isn't it?
Here's the sketch:
Code:

const int numeral[10] = {

//ABCDEFG /dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B00111110, // 6
B11100000, // 7
B11111110, // 8
B11100110, // 9
};

// pins for decimal point and each segment

const int segmentPins[] = { 4,7,8,6,5,3,2,9};

const int nbrDigits= 4;

const int digitPins[nbrDigits] = { 10,11,12,13};

void setup() {

for(int i=0; i < 8; i++)
pinMode(segmentPins[i], OUTPUT);

for(int i=0; i < nbrDigits; i++)
pinMode(digitPins[i], OUTPUT);
}

void loop() {

int value = analogRead(0);
showNumber(value);
}


void showNumber( int number) {


if(number == 0)
showDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit
else {

for( int digit = nbrDigits-1; digit >= 0; digit--) {
if(number > 0) {

showDigit( number % 10, digit) ;
number = number / 10;
}
}
}
}
 void showDigit( int number, int digit) {
digitalWrite( digitPins[digit], HIGH );
for(int segment = 1; segment < 8; segment++) {
boolean isBitSet = bitRead(numeral[number], segment);
 isBitSet = ! isBitSet;
 digitalWrite( segmentPins[segment], isBitSet);
} delay(5);
7  Using Arduino / Programming Questions / Re: I need help with States and buttons in my code on: January 11, 2013, 09:20:49 am
Well i tested it and for the stated purpose, it works...
can i have a feedback by you, guys?
what do you think about?
8  Using Arduino / Programming Questions / Re: I need help with States and buttons in my code on: January 10, 2013, 06:23:47 pm
Hi!
if i got the target you're trying to reach...
You want to switch among five choices, using a button.
I wrote down the following sketch, providing a second pushbutton (startButton), whose aim is to start the acquisition time for data from the other switch (commandButton).
In this way you tell arduino "pay attention i'm sending you the number of the program i want you to run, and i will tell you that number, by the number of clicks i will perform on the commandButton."

here is the sketch:
Code:
int startButton=12;
int commandButton=11;
int i=0;
int count;
int program;
int state=0;
int state2=0;
int prevState=0;
int prevState2=0;

long prevDebTime;
long prevDebTime2;
long debDelay=200;

void setup(){
  Serial.begin(9600);
  pinMode(startButton,INPUT);
  digitalWrite(startButton,HIGH);
  pinMode(commandButton, INPUT);
  digitalWrite(commandButton,HIGH);
}

void loop(){
  state=!digitalRead(startButton);
  if((millis()-prevDebTime)>debDelay){
   if (state!=prevState && state==HIGH) {
     if(i==0){
    i=1;
    prevDebTime=millis();
  }
  else if(i==1){
    i=0;
    Serial.println("Program number: ");
    Serial.println(count);
    program=count;
    switch(program){
       case 1:
       //insert program 1 commands
       break;
  
       case 2:
       //insert program 2 commands
       break;
      
       //and so on....
    }
    count=0;
    prevDebTime=millis();
}
   }

}
if(i==1){
  state2=!digitalRead(commandButton);
  if((millis()-prevDebTime2)>debDelay){
    if(state2!=prevState2 && state2==HIGH){
      count++;
      prevDebTime2=millis();
    }
  }
}
}



you have to fill the switch function with the actions your led matrix is expected to show.
the pushbuttons are already debuonced.

hope this could help you!
And I apologize for my English!...
9  Using Arduino / Programming Questions / Re: lags in serial communication on: January 10, 2013, 03:14:10 pm
yes!
it must be the delay function lagging all the program!
Thank u!
You've been very helpful!
10  Using Arduino / Programming Questions / lags in serial communication on: January 09, 2013, 10:32:02 am
Hi everyone!

the following sketch should work, accepting three values entered by the utilizer, blinking three different leds, with delay times equal to the values entered with serial communication.
It works! But the problem is that, every time i enter three new values and press start, several seconds pass before the command is received by arduino, and the leds start blinking withe the new delay times. What is the cause? Serial communication baud rate?

Here is the sketch:
Code:
const int NUMBER_OF_FIELDS=3;
int fieldIndex=0;
int values[NUMBER_OF_FIELDS];

int ledDelays[]={0,0,0};

void setup(){
  Serial.begin(9600);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
}

void loop(){
 
 blinkIt(2,ledDelays[0]);
 blinkIt(3,ledDelays[1]);
 blinkIt(4,ledDelays[2]);
 
  
 if(Serial.available()){
   char ch=Serial.read();
   if(isDigit(ch)){
     if (fieldIndex<NUMBER_OF_FIELDS){
       values[fieldIndex]=(values[fieldIndex]*10)+(ch-'0');
     }
   }
   else if(ch==','){
     fieldIndex++;
   }
   else if(ch==10){
     for(int i=0; i<min(NUMBER_OF_FIELDS, fieldIndex+1); i++){
       Serial.println(values[i]);
       ledDelays[i]=values[i];
       values[i]=0;
     }
     fieldIndex=0;
   }
 }
 
}

void blinkIt (int led, int delayTime){
  digitalWrite(led,HIGH);
  delay(delayTime);
  digitalWrite(led,LOW);
  delay(delayTime);
}

11  Using Arduino / Programming Questions / Re: Time between button press on: December 25, 2012, 12:19:58 pm
it could help?

Code:
const int inPin = 2;
const int outPin= 13;
int elapsedtime=0;

int ledstate =LOW;
int switchstate = LOW;
int previous = LOW;

long prevtime = 0;
long debounce = 200;
long dur_on = 0; //duration between on/off

void setup(){
  pinMode(inPin,INPUT);
  pinMode(outPin,OUTPUT);
  Serial.begin(9600);
}

void loop(){

  switchstate=digitalRead(inPin);
  if ((millis()-prevtime)>debounce){
    if(switchstate!=previous && switchstate==HIGH){
      if (ledstate==LOW){
        digitalWrite(outPin,HIGH);
        prevtime=millis();
        ledstate=HIGH;
      }
      else if(ledstate==HIGH){
        digitalWrite(outPin,LOW);
        ledstate=LOW;
        Serial.print("time between On and off state");
        elapsedtime=((millis()-prevtime)/1000);
        Serial.println(elapsedtime);
         prevtime=millis();
      }
    }
  }
}
     
       
 

it works, but the value printed in the serial monitor, is always an integer, and even initializing the "elpasedttime" as a float, always ineger values are returned.
12  Using Arduino / Programming Questions / Re: Serial.available() on: December 23, 2012, 03:54:41 pm
At the end:
data does not occupy the serial buffer on Arduino, until I hit "enter".
and the function that extracts data from the buffer is the Serial.read function. And when i call it, one digit (or character, depending on the variables initialization), comes out from the buffer and the "available elements" in the buffer decrease by one unit?

 
13  Using Arduino / Programming Questions / Re: Serial.available() on: December 23, 2012, 11:24:10 am
Quote
I asked a very simple question. What option have you selected at the bottom of the Serial Monitor dialog? I'm not going to participate in this discussion any more until you answer that question.

At the bottom of the serial monitor there are:
-Autoscroll (flagged)
-No line ending
-9600 baud rate
14  Using Arduino / Programming Questions / Re: Serial.available() on: December 23, 2012, 10:30:16 am
Quote
Keep in mind that you still have not answered the question about what option you have chosen for the Serial Monitor, so if you type three characters and hit enter, there may be 3, 4, or 5 characters actually sent to the Arduino.

after uploading the program on Arduino, I open the serial monitor, digit characters inside the communication field, and hit enter. That's it.

Referring to the following code:
Code:
int input;

void setup(){
  Serial.begin(9600);
}

void loop(){
  if(Serial.available()>=3){
  char input=Serial.read(); 
  Serial.println(input);
 
  Serial.println("what serial.available contains:");
  Serial.println(Serial.available());
}
 
}

 

if i enter "567", the Serial.print function returns "5"; that because, characters are sent to Arduino one by one, so when the first digit is sent, the if statement goes like that: "the first char enter the Arduino buffer. Now the Arduino buffer contains >= 3 elements? = NO! and so on, until i enter"7".
Now Arduino buffer contains 3 or more elements so 'if loop' is triggered, and Serial.read reads the first element stored in the buffer, that is "5"! Printing the element (Serial.available) left in the buffer, the answer is "2", and that is trivial...
But going on in the same serial monitor, typing other three char, for example "890": this is the signal returned:

Code:
6
what serial.available contains:
2
7
what serial.available contains:
3
8
what serial.available contains:
2

why after "7" character has been read, there are 3 elements left in Arduino buffer!?
 
15  Using Arduino / Programming Questions / Re: Serial.available() on: December 23, 2012, 05:35:57 am
Quote
When 3 or more char are entered, serial data began to flow from Arduino to pc.
No, it does not. No serial data is sent until you press the Send button or hit the enter key.

Yep, sorry, i meant "after pressing" enter key...

Quote
I don't see anything in your observations that I don't expect.
What are you expectations?

i was trying to get the meaning of Serial.available function, and what seem to be obvious statements,
for my experience, are enormous obstacles!

Thanks foor helping me, Your all advices have been precious for me!


Pages: [1] 2