3-wheel robot not moving back or left using voice commands - arduino-uno

I am working on 3-wheel robot. I want to move my robot front, back, right, left using voice commands. I am using arduino electronics as my mobile app. By using that app I want to control my robot. I am using an arduino uno board and L293D motor driver. Below is my code for 3-wheel robot:
String readvoice;
void setup() {
//BT.begin(9600);
Serial.begin(9600);
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
}
//-----------------------------------------------------------------------//
void loop() {
while (Serial.available()) { //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = Serial.read(); //Conduct a serial read
readvoice += c; //build the string- "forward", "reverse", "left" and "right"
}
if (readvoice.length() > 0) {
Serial.println(readvoice);
if (readvoice == "go")
{
//Serial.println("front"); //Writing values to motor driver
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(6,HIGH);
}
if (readvoice == "left")
{
//Serial.println("left");
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(7,HIGH);
digitalWrite(6,LOW);
}
if(readvoice == "right")
{
//Serial.println("right");
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(7,LOW);
digitalWrite(6,HIGH);
}
if (readvoice == "back")
{
//Serial.println("back");
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(7,HIGH);
digitalWrite(6,LOW);
}
if (readvoice == "stop")
{
//Serial.println("stop");
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(6,LOW);
}
readvoice = "";
}
}
I kept all the connections correctly, but my robot is able to move only right and front. I am not able to run my robot either left or back. Is there any problem in the code? Or any problem with hardware. If i had any problem with hardware, my robot should not move front or right either, but it is moving. Any idea what is wrong with back and left?

Related

Creating click to start screen for game

I am using Processing to create a basketball game. I have managed to create the basketball game but I want to have a click to start home screen. I have made the graphic for the home screen but I am not sure how to integrate it into the game code. Any ideas on how to go about this. Thanks!
I found something on the internet related to this which was...
if (started) {
//all the code for the game
} else {
// all the code for the start screen
if (keyDown("enter")) {
started = true;
}
}
Im not sure if this is leading me in the right direction or how I could necessarily use this.
Use keyPressed() outside the draw().
void keyPressed() {
println(int(key));
if (key==10) {
println("ENTER");
}
}
key - contains the value of the most recent key on the keyboard that was used (either pressed or released).
What is described in: https://processing.org/reference/keyPressed_.html
Here is a little demo program that may help you. The global variable started controls whether the game has begun or not. The function keyPressed sets it to true if you press the ENTER key.
In draw, put your game code in the first if-part and your waiting screen in the second.
boolean started = false;
void draw() {
background(0);
textAlign(CENTER);
if (started) {
// your game code here
text("gameplay", 50, 50);
} else {
// your start/launch screen here
text("waiting...", 50, 50);
}
}
void keyPressed() {
if (keyCode == ENTER) {
started = true;
}
}

Invalid characters Arduino, Bluetooth (HM10 ) and Xamarin.Forms

I am having an issue trying to send commands to my Arduino Mega from my Android Xamarin.Forms app. When i use Bluetooth terminal app from play store it works, but my code does not. In the Arduino terminal i am just getting squares as if it is a baud rate issue or something. but from what i can see the baud rate is always determined by the device, which defaults to 9600 for the HM10.
I am using this library for the bluetooth https://github.com/xabre/xamarin-bluetooth-le
Arduino Sketch (works with bt terminal app)
void loop()
{
// Read from the Bluetooth module and send to the Arduino Serial Monitor
if (Serial3.available())
{
byte y = Serial3.read();
//int x = (int)y;
Serial.write(y);
}
// Read from the Serial Monitor and send to the Bluetooth module
if (Serial.available())
{
c = Serial.read();
// do not send line end characters to the HM-10
if (c != 10 & c != 13)
{
Serial3.write(c);
}
// Echo the user input to the main window.
// If there is a new line print the ">" character.
if (NL) { Serial.print("\r\n>"); NL = false; }
Serial.write(c);
if (c == 10) { NL = true; }
}
}
Then my send code in my Xamarin.Forms app
private async Task Button_ClickedAsync(object sender, EventArgs e)
{
if (_charataristice != null && _charataristice.CanWrite)
{
var data = Convert.ToByte(16);
_charataristice.WriteType = CharacteristicWriteType.WithoutResponse;
await _charataristice.WriteAsync(new[] { data });
}
}
Would appreciate any help, the value i am trying to revive is alwasy a number and should fall within one byte.

Controlling two objects' movement in ping pong game in Processing

I'm trying to build a ping pong game with Processing language. For this two-player game I have two controllers at each end of the 'table'. I coded the movements of the players (up and down) by binding them to the keys:
- w and s for player 1
- o and l for player 2
Although this works when I press them one at a time, I cannot figure out how to make them move simultaneously, as in pressing both w and o at the same time.
Here is my code:
int x=535;
int y=350;
int dx=5;
int dy=5;
int pX=10;
int pY=520;
int pX1=1870;
int pY1=520;
int pS=5;
void setup() {
size(1920,1080);
}
void draw() {
background(0);
rect(960,0,5,1080);
rect(pX,pY,40,150);
rect(pX1,pY1,40,150);
ellipse(x,y,50,50);
x=x+dx;
y=y+dy;
bounce();
move();
move1();
}
void bounce(){
if(x>=1920 || x<=0){
dx=-dx;
}
if(y>1080 || y<0){
dy=-dy;
}
}
void move(){
if(keyPressed){
if(key == 's'){
pY+=pS;
}else if (key == 'w'){
pY-=pS;
}
}
}
void move1() {
if(keyPressed){
if(key == 'l'){
pY1+=pS;
}else if (key == 'o'){
pY1-=pS;
}
}
}
What you want to do is create a boolean value for each key you care about. Then in the keyPressed() function, you set the corresponding variable to true, and in the keyReleased() function, you set the corresponding variable to false. Then in your draw() function, you check the variables to determine which keys are pressed.
Shameless self-promotion: I wrote a tutorial on getting user input available here. Check out the Handling Multiple Keys section.

Implement a sharp right turn using atmega8 for line follower

I am new to AVR programming, and I am trying to implement a sharp right turn using atmega8. I was able to implement the straight line path but cannot implement a sharp right turn. Here is my code:
`#include <avr/io.h>
#include<util/delay.h>
int main(void)
{
DDRC=0b00000000;
DDRB=0b11111111;
int count=1,right=1;
while(1)
{
if((PINC&=0b00011111)==0b00000000)
{
PORTB=0b00000110;
}
else if((PINC&=0b00011111)==0b00001110)
{
PORTB=0&00100111;
}
else if((PINC&=0b00011111)==0b00001100)
{
PORTB=0b00000111;
}
else if((PINC&=0b00011111)==0b00000110)
{
PORTB=0b00100110;
}
else if((PINC&=0b00011111)==0b00001111)
{
if(count)
{
PORTB=0b0010011;
_delay_ms(200);
count--;
}
else if(((PINC&=0b00011111)==0b00000110)&&~(count))
{
PORTB=0B00000111;
}
}
else if((PINC&=0b00011111)==0b00011110)
{
if(right)
{
PORTB=0b0010011;
_delay_ms(200);
right--;
}
else if(((PINC&=0b00011111)==0b00000110)&&~(right))
{
PORTB=0B00100110;
}
}
}
}
This doesn't seem to work at all for right and left turns.
Any idea where I am going wrong?
Without understanding your Program (see my Comment above) iam guessing it is because you permanently write to your PIN-regsiters in the if-clauses.
PINC&=0b00011111 means:
read PINC-value
binary AND it with 0b00011111
write the result back to PINC
Depending on which AVR your code is running you toggle the output by writing a 1 to a PINX-register bit. If DDR is configured to input you toggle the pullups. This is true for the newer AVR-cores. For the old one its undefined behavior to write to the PIN-registers as they are defined as read-only.

Receiving SMS by Arduino GSM Shield and control the LED with the content of this SMS?

I am using Arduino GSM Shield receiving SMS from an Android app. And the content of this SMS will control a LED. If the content of this SMS is not "off", the LED will be on and the content will be printed in the serial monitor. But if it is "off", the LED will be off immediately. Besides, the LED will keep being on until the "off" message coming. For now, I used the code from the example of the software. But I cannot use the content of this SMS to control the status of LED. With the code below, the LED could not be turned on and the content could not be displayed on the monitor. I think it was because the sketch failed to get the whole content of this SMS. Could anybody tell me how to solve this problem? Thanks.
#include <GSM.h>
GSM gsmAccess;
GSM_SMS sms;
char senderNumber[20];
int led=13;
void setup()
{
Serial.begin(9600);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
while (!Serial) {
;
}
Serial.println("SMS Messages Receiver");
boolean notConnected = true;
while(notConnected)
{
if(gsmAccess.begin("6442")==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
Serial.println("Waiting for messages");
}
void loop()
{
char c;
int val=0;
val=digitalRead(led);
if (val==HIGH){
digitalWrite(led,HIGH);
}
if (sms.available())
{
Serial.println("Message received from:");
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);
if(sms.peek()=='#')
{
Serial.println("Discarded SMS");
sms.flush();
}
while(c=sms.read())
if(c='off'){
digitalWrite(led,LOW);
}else{
digitalWrite(led,HIGH);
Serial.print(c);
}
Serial.println("\nEND OF MESSAGE");
sms.flush();
Serial.println("MESSAGE DELETED");
}
delay(1000);
}
With this line
if(c='off'){
you are setting the value of c to "off". I guess you want to compare the value of c to the string "off" instead. Use == instead of =.
Also, what happens if someone sends "OFF" instead of "off"......? you need to handle that case as well. Try converting the SMS to lower characters before you do the compare.

Resources