Show Posts
Pages: 1 2 [3] 4 5 ... 23
31  Using Arduino / Programming Questions / Re: 2WD Electric Car on: December 02, 2013, 02:16:52 pm
Quote
I am curious because once I send '1 ' to the arduino, then the serial.read() will always read it as '1' and perform what it is in the loop isn't ?
No. If you send '1', then one character will become available. You read that character, and set the flag to say show this data on the LCD.

Then, you use the value read and show the data on the LCD. When you show the data, you clear the flag that says to show the data.

But, you keep using that value, on every pass through loop, as though you had received a new value.

You have two choices. First is to add state = 0; to the end of loop. If you do this, you can get rid of flag, as you will only be showing data when the data changes. The second is to move all the code into the if(Serial.available() > 0) block, so that you only read, use, and show the data when a new value arrives. If you do this, you can get rid of flag, as you will only be showing data when the data changes.

Note that whichever method you choose, flag is useless.
Code:
else if (state == '6') {

    digitalWrite(f_b_light,HIGH);
    delay(100);
   
    state = 0;
    }
  }

I tried with this. Not working.

But,
Code:
else if (state == '6') {

    digitalWrite(f_b_light,HIGH);
    delay(100);
    digitalWrite(f_b_light,LOW);
   
    state = 0;
    }
  }

This code will cause the LED to light up once the button 6 is released.Something similar to what I want.But I do not understand why the first one not working.

Any help ?

Thanks !
32  Using Arduino / Programming Questions / Re: 2WD Electric Car on: November 30, 2013, 11:07:41 am
Okay,  I will try out your suggestion.

Thank you . )
33  Using Arduino / Programming Questions / Re: 2WD Electric Car on: November 30, 2013, 10:44:30 am
I thought the flag just to make sure the character are not printed few times on the LCD ?

Correct me if I am wrong.

Thanks !
34  Using Arduino / Programming Questions / Re: 2WD Electric Car on: November 30, 2013, 10:23:31 am
Currently, I received the byte from my phone, then it will perform what it received and display the current status to LCD.

For example, when I pressed '1',  then the car will move forward. It will continue to move forward until it received another character byte maybe like '2' , then it will reverse.

What I want it to be now is :
I need to send a continuous '1' to make the car move forward continuously. When I don't press anything on the phone, then it will STOP.

How is it possible for me to do that ? I am curious because once I send '1 ' to the arduino, then the serial.read() will always read it as '1' and perform what it is in the loop isn't ?

Please help.

Thanks !
35  Using Arduino / Programming Questions / Re: 2WD Electric Car on: November 30, 2013, 10:11:30 am
If never received any data, then the car won't be moving smiley-grin

Current code :

Code:
int motorPin1 = 7;
int motorPin2 = 8;
int enablePin_1 = 6;
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int buzzer = 11;
int f_b_light = 12 ;
int state;
int flag=0; //makes sure that the serial only prints once the state
boolean buzzerstatus = 0;
boolean LEDstatus = 0;

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 10, 9, A0, A1, A2);

void setup() {
  // sets the pins as outputs:
  lcd.begin(16, 2);
  pinMode(buzzer,OUTPUT);
  pinMode(f_b_light,OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin_1, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(enablePin_2, OUTPUT);
  // sets enablePin high so that motor can turn on:
  digitalWrite(enablePin_1, HIGH);
  digitalWrite(enablePin_2, HIGH);
  pinMode(13,OUTPUT);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  //if some date is sent, reads it and saves in state
  if(Serial.available() > 0){
    state = Serial.read();
    flag=0;
  }
  // if the state is '0' the DC motor will turn off
  if (state == '0') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
    if(flag == 0){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("STOP");
      flag=1;
    }
  }
  // if the state is '1' the motor will forward
  else if (state == '1') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high
    if(flag == 0){
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("FORWARD");
      flag=1;
    }
  }
  // if the state is '2' the motor will backward
  else if (state == '2') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
    digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
    if(flag == 0){
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("REVERSE");
      flag=1;
    }
  }

  else if (state == '3') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
    if(flag == 0){
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("RIGHT");
      flag=1; //Left wheel forward, Right wheel static
      flag=1;
    }
  }
  else if (state == '4') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
    if(flag == 0){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("LEFT");
      flag=1;
    }
  }
36  Using Arduino / Programming Questions / Re: 2WD Electric Car on: November 30, 2013, 09:55:31 am
Is that ok ?

Thanks ! smiley-grin
37  Using Arduino / Programming Questions / Re: 2WD Electric Car on: November 30, 2013, 09:49:29 am
OK, you've found the quote tags, well, the code tags are to the left (#)
I tried and at arduino IDE, I copied to forum then paste. It does not work.

Thanks !
38  Using Arduino / Programming Questions / Re: 2WD Electric Car on: November 30, 2013, 09:16:18 am
Quote
Current coding :
Is not accessible to everyone. There is a mechanism for you to attach your code to your post. Use the Additional Options link below the text entry box. Do that only after using Tools + Auto Format.

If you can't figure out that Tools + Auto Format means to go to the Tools menu in the IDE, and then select Auto Format, I don't think that there is much chance of your robot ever working.
Current coding
Quote
int motorPin1 = 7;
int motorPin2 = 8;
int enablePin_1 = 6;
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int buzzer = 11;
int f_b_light = 12 ;
int state;
int flag=0; //makes sure that the serial only prints once the state
boolean buzzerstatus = 0;
boolean LEDstatus = 0;

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 10, 9, A0, A1, A2);

void setup() {
  // sets the pins as outputs:
  lcd.begin(16, 2);
  pinMode(buzzer,OUTPUT);
  pinMode(f_b_light,OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin_1, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(enablePin_2, OUTPUT);
  // sets enablePin high so that motor can turn on:
  digitalWrite(enablePin_1, HIGH);
  digitalWrite(enablePin_2, HIGH);
  pinMode(13,OUTPUT);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  //if some date is sent, reads it and saves in state
  if(Serial.available() > 0){
    state = Serial.read();
    flag=0;
  }
  // if the state is '0' the DC motor will turn off
  if (state == '0') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
    if(flag == 0){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("STOP");
      flag=1;
    }
  }
  // if the state is '1' the motor will forward
  else if (state == '1') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high
    if(flag == 0){
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("FORWARD");
      flag=1;
    }
  }
  // if the state is '2' the motor will backward
  else if (state == '2') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
    digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
    if(flag == 0){
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("REVERSE");
      flag=1;
    }
  }

  else if (state == '3') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
    if(flag == 0){
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("RIGHT");
      flag=1; //Left wheel forward, Right wheel static
      flag=1;
    }
  }
  else if (state == '4') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
    if(flag == 0){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("LEFT");
      flag=1;
    }
  }

39  Using Arduino / Programming Questions / Re: 2WD Electric Car on: November 30, 2013, 06:00:25 am
At the moment, when I press forward and release, the car will move forward. while moving forward, when I click left button and release, then it will change to always turn left(which can be a 360 degree rotate).

I want it to be like when I press the left button it will go left slightly until I release the left button.

Thanks !
40  Using Arduino / Programming Questions / Re: 2WD Electric Car on: November 30, 2013, 04:01:15 am
I mean I want the car to move like this :

Once I press the button moving forward, then it will move forward. While moving forward, I pressing the 'turn left' button then the car will turn left until I released from the 'turn left'button. It seems to be like I program the virtual button to act like a tact switch.

Thanks !
41  Using Arduino / Programming Questions / 2WD Electric Car on: November 30, 2013, 03:52:03 am
Dear all,

I would like to build a simple 2WD electric car.  I am using a serial bluetooth module and I am controlling them through android smart phone.

I am suck in the programming now.
This is what I want my car to do :
When moving forward, then once I click the left button, it will turn left for a period of time and then continue to move straight. We consider the move to left as interrupt.

Current coding :

https://www.dropbox.com/s/ooplrpxizdp0z3v/Electric%20Car.html

I do not know how to use the auto format tools. >.<

Thanks for help
42  Using Arduino / Networking, Protocols, and Devices / Bluetooth Module Serial Port in windows8 on: November 04, 2013, 09:41:40 am
http://dx.com/p/jy-mcu-arduino-bluetooth-wireless-serial-port-module-104299

Dear friend, I puchased this bluetooth module and wanna try to control a LED through bluetooth. The problem is that when I use teraterm, I can't connect to the bluetooth serial port.

I am using window 8. I don't have such problem in window 7.

Anyone experience in this ?

Thank you smiley
43  Using Arduino / Programming Questions / Re: Attiny 85 with SoftwareSerial library on: November 04, 2013, 05:46:59 am
Ahh, I used USBasp to upload the code to my attiny85. Then I use an android apps 'BlueTerm'' to send serial data to my attiny 85. Bad thing is that it does not display anything on the android apps.

My code :
Quote
#include <SoftwareSerial.h>

#define RxD 0
#define TxD 1
SoftwareSerial mySerial(0, 1); 
// Pin 13 - LED
int led = 3;
int state;

void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
pinMode(RxD, INPUT);
pinMode(TxD,OUTPUT);

  // initialize serial communication at 9600 bits per second:
  mySerial.begin(9600);
}

void loop() {
  if(mySerial.available() > 0){     
      state = mySerial.read();}

  if (state == '1') {
    mySerial.println(state);
      
    }   // turn the LED on (HIGH is the voltage level)
 
  else if (state == '2') {
    mySerial.println(state);
     
    }    // turn the LED off by making the voltage LOW
      
  delay(100);               // wait for 100ms
  
  //For debugging purpose
  //Serial.println(state);
}


Any idea ?

Thanks !
44  Using Arduino / Programming Questions / Re: Attiny 85 with SoftwareSerial library on: November 03, 2013, 12:12:09 pm
Quote
My sketch directory is in Document.
Then, the only other thing I can think of is that you are using a pre-1.0 core for the ATTiny85. You need to find a post-1.0 core to use with the post-1.0 IDE.
Code:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(1, 2);
//
int led = 4;
int state;

void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
  // initialize serial communication at 9600 bits per second:
  mySerial.begin(9600);
}

void loop() {
  if(mySerial.available() > 0){    
      state = mySerial.read();}

  if (state == '1') {
    mySerial.println(state);
      digitalWrite(led, HIGH);
    }   // turn the LED on (HIGH is the voltage level)
 
  else if (state == '2') {
    mySerial.println(state);
      digitalWrite(led, LOW);
    }    // turn the LED off by making the voltage LOW
      
  delay(100);               // wait for 100ms
  
  //For debugging purpose
  //Serial.println(state);
}

This is my current code. I want to light up and LED wirelessly through bluetooth.

I do not know why I can't get the LED to light up smiley-sad

Sigh. Any ideas ?

Thanks !
45  Using Arduino / Programming Questions / Re: Attiny 85 with SoftwareSerial library on: November 03, 2013, 10:52:13 am
Quote
My sketch directory is in Document.
Then, the only other thing I can think of is that you are using a pre-1.0 core for the ATTiny85. You need to find a post-1.0 core to use with the post-1.0 IDE.
I searchedfor newer version : https://github.com/TCWORLD/ATTinyCore

However, when I try to change the board to attiny 85(8MHz internal). it give me error Serial was not declared.

So how to solve that ?

Thanks !

Wait, it seems to be working !

Will update soon ! smiley

Thanks so much !
Pages: 1 2 [3] 4 5 ... 23