arduino joystick library, don't stop joystick - arduino

I'm making a gamepad on a joystick using an arduino pro micro.
Input commands such as up and down from the console, raspberry pi's gpio will output them and the arduino will receive them and input them to the game machine.
I use the ArduinoJoystickLibrary.
https://github.com/MHeironimus/ArduinoJoystickLibrary
I want to play a GBA game, but the Dpad stays typed.
How can I press the up key only once?
Also, in the gif below, the up and down keys are pressed once at a time, but pressing the right key or other keys in the middle does not respond.
As an addendum, I use a game console called retro freak.
retro freak
The code uses the following
arduino_pro_micro_gamepad.ino
#include <Joystick.h>
#define OFFSET_BUTTON 2
#define OFFSET_DPAD 18
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
4, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
void setup() {
// Initialize Button Pins
pinMode(2, INPUT_PULLUP); // Button 1
pinMode(3, INPUT_PULLUP); // Button 2
pinMode(4, INPUT_PULLUP); // Button 3
pinMode(5, INPUT_PULLUP); // Button 3
pinMode(18, INPUT_PULLUP); // D-pad Up
pinMode(19, INPUT_PULLUP); // D-pad Right
pinMode(20, INPUT_PULLUP); // D-pad Down
pinMode(21, INPUT_PULLUP); // D-pad Left
Joystick.begin();
Joystick.setXAxisRange(-1, 1);
Joystick.setYAxisRange(-1, 1);
Joystick.setYAxis(0);
Joystick.setYAxis(0);
}
// Last state of the buttons
int lastDpadState[4] = {0,0,0,0}; // Up, Right, Down, Left
int lastButtonState[4] = {0,0,0,0}; // Button 1 - 4
void updateDpad() {
for (int i = 0; i <= 3; i++) {
int currentDpadState = digitalRead(i + OFFSET_DPAD);
if (currentDpadState != lastDpadState[i]) {
switch (i) {
case 0: // UP
if (currentDpadState == 1) {
Joystick.setYAxis(-1);
}
break;
case 1: // RIGHT
if (currentDpadState == 1) {
Joystick.setXAxis(1);
}
break;
case 2: // DOWN
if (currentDpadState == 1) {
Joystick.setYAxis(1);
}
break;
case 3: // LEFT
if (currentDpadState == 1) {
Joystick.setXAxis(-1);
}
break;
}
lastDpadState[i] = currentDpadState;
}
}
}
void updateButton() {
for (int i = 0; i <= 3; i++) {
int currentButtonState = !digitalRead(i + OFFSET_BUTTON);
if (currentButtonState != lastButtonState[i]) {
Joystick.setButton(i, currentButtonState);
lastButtonState[i] = currentButtonState;
}
}
}
void loop() {
updateDpad();
updateButton();
delay(5);
}
raspi_game_input.py
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(27, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(23, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(24, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(16, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(20, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(21, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(26, GPIO.OUT, initial=GPIO.HIGH)
def on(gpio_number):
GPIO.output(gpio_number, GPIO.LOW)
sleep(0.05)
GPIO.output(gpio_number, GPIO.HIGH)
def key_select(input_key):
if input_key == 'a':
on(17)
elif input_key == 'b':
on(27)
elif input_key == 'home':
on(23)
elif input_key == 'game':
on(24)
elif input_key == 'u':
on(16)
elif input_key == 'd':
on(20)
elif input_key == 'r':
on(21)
elif input_key == 'l':
on(26)
else:
pass
if __name__ == '__main__':
try:
while True:
print('push button')
key = input()
print(f'input is ${key}')
key_select(key)
except KeyboardInterrupt:
GPIO.cleanup()

Just a little fix in the code, in setup();
Joystick.setYAxis(0);
Joystick.setYAxis(0);
change to:
Joystick.setXAxis(0);
Joystick.setYAxis(0);
Try to reset to initial values before... or after the action.
if (currentDpadState != lastDpadState[i]) {
reset();
switch (i) {
...
}
}
void reset(){
Joystick.setXAxis(0);
Joystick.setYAxis(0);
}

My problem was using a joystick instead of a hatSwitch.
The implementation I wanted was to enter the arrow keys only once, so I used hatswitch.
#include <Joystick.h>
#define OFFSET_BUTTON 2
#define OFFSET_DPAD 18
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_GAMEPAD,
9,1,
false, false, false, false, false, false,
false, false, false, false, false);
void setup() {
pinMode(2, INPUT_PULLUP); // Button 1
pinMode(3, INPUT_PULLUP); // Button 2
pinMode(4, INPUT_PULLUP); // Button 3
pinMode(5, INPUT_PULLUP); // Button 4
pinMode(6, INPUT_PULLUP); // Button 5
pinMode(7, INPUT_PULLUP); // Button 6
pinMode(8, INPUT_PULLUP); // Button 7
pinMode(9, INPUT_PULLUP); // Button 8
pinMode(10, INPUT_PULLUP); // Button 9
pinMode(18, INPUT_PULLUP); // D-pad Up
pinMode(19, INPUT_PULLUP); // D-pad Right
pinMode(20, INPUT_PULLUP); // D-pad Down
pinMode(21, INPUT_PULLUP); // D-pad Left
Joystick.begin();
}
// Last state of the pins
int lastDpadState[2][4] = {{0,0,0,0}, {0,0,0,0}};
int lastButtonState[10] = {0,0,0,0,0,0,0,0,0,0}; // Button 1 - 10
void updateDpad(){
bool valueChanged[2] = {false, false};
int currentPin = 18;
// Read pin values
for (int hatSwitch = 0; hatSwitch < 2; hatSwitch++){
for (int index = 0; index < 4; index++){
int currentDpadState = !digitalRead(currentPin++);
if (currentDpadState != lastDpadState[hatSwitch][index]){
valueChanged[hatSwitch] = true;
lastDpadState[hatSwitch][index] = currentDpadState;
}
}
}
for (int hatSwitch = 0; hatSwitch < 2; hatSwitch++)
{
if (valueChanged[hatSwitch]) {
if ((lastDpadState[hatSwitch][0] == 0)
&& (lastDpadState[hatSwitch][1] == 0)
&& (lastDpadState[hatSwitch][2] == 0)
&& (lastDpadState[hatSwitch][3] == 0)) {
Joystick.setHatSwitch(hatSwitch, -1);
}
if (lastDpadState[hatSwitch][0] == 1) {
Joystick.setHatSwitch(hatSwitch, 0); // up
}
if (lastDpadState[hatSwitch][1] == 1) {
Joystick.setHatSwitch(hatSwitch, 90); // right
}
if (lastDpadState[hatSwitch][2] == 1) {
Joystick.setHatSwitch(hatSwitch, 180); // down
}
if (lastDpadState[hatSwitch][3] == 1) {
Joystick.setHatSwitch(hatSwitch, 270);
}
} // if the value changed
} // for each hat switch
}
void updateButton() {
for (int i = 0; i <= 9; i++) {
int currentButtonState = !digitalRead(i + OFFSET_BUTTON);
if (currentButtonState != lastButtonState[i]) {
Joystick.setButton(i, currentButtonState);
lastButtonState[i] = currentButtonState;
}
}
}
void loop() {
updateDpad();
updateButton();
delay(50);
}

Related

Arduino lcd screen showing broken characters at random times

So I've been working on an arduino project for an escape room puzzle. It's essentially a bomb with a timer on an lcd screen. After all of the elements of the bomb are solved the timer stops and the lcd screen displays that the bomb has been defused and gives the group a clue to the next puzzle. 9 times out of 10 it works perfectly. But every once in a while when it is supposed to display that the bomb is defused the lcd screen just shows random broken characters. I haven't had any luck diagnosing the problem. Hoping somebody here might have an idea.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Tone.h>
#define pound 14
Tone tone1;
int Scount = 0;
int Mcount = 0;
int Hcount = 1;
int DefuseTimer = 0;
long secMillis = 0;
long interval = 1000;
char password[6] = "594432";
int currentLength = 0;
int i = 0;
char entered[6];
int ledPin = 23;
int ledPin2 = 25;
int ledPin3 = 27;
int ledPin4 = 29;
int ledPin5 = 31;
int ledPin6 = 34;
const int plugin1 = 44;
const int plugin2 = 46;
const int plugin3 = 48;
const int plugin4 = 50;
const int plugin5 = 52;
int plugin1State = 0;
int plugin2State = 0;
int plugin3State = 0;
int plugin4State = 0;
int plugin5State = 0;
const int switch1 = 37;
int switch1State = 0;
const int key1 = 40;
int key1State = 0;
int puzzle1 = 0;
int puzzle2 = 0;
int puzzle3 = 0;
int solved = 0;
LiquidCrystal lcd(7, 8, 10, 11, 12, 13);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {A4, A5, A6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(plugin1, INPUT);
pinMode(plugin2, INPUT);
pinMode(plugin3, INPUT);
pinMode(plugin4, INPUT);
pinMode(plugin5, INPUT);
digitalWrite(plugin1, HIGH);
digitalWrite(plugin2, HIGH);
digitalWrite(plugin3, HIGH);
digitalWrite(plugin4, HIGH);
digitalWrite(plugin5, HIGH);
pinMode(switch1, INPUT);
digitalWrite(switch1, HIGH);
pinMode(key1, INPUT_PULLUP);
digitalWrite(key1, HIGH);
tone1.begin(9);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0, 0);
tone1.play(NOTE_E6, 200);
delay(3000);
lcd.clear();
currentLength = 0;
}
void loop()
{
timer();
plugin1State = digitalRead(plugin1);
plugin2State = digitalRead(plugin2);
plugin3State = digitalRead(plugin3);
plugin4State = digitalRead(plugin4);
plugin5State = digitalRead(plugin5);
if (plugin1State == LOW && plugin2State == LOW && plugin3State == LOW && plugin4State == LOW && plugin5State == LOW)
{
puzzle1 = 1;
}
if (puzzle1 == 1)
{
digitalWrite(ledPin4, HIGH);
switch1State = digitalRead(switch1);
if (switch1State == LOW)
{
puzzle2 = 1;
}
}
if (puzzle2 == 1)
{
digitalWrite(ledPin5, HIGH);
key1State = digitalRead(key1);
if (key1State == LOW)
{
puzzle3 = 1;
}
if (key1State == HIGH)
{
digitalWrite(ledPin6, LOW);
}
}
if (puzzle3 == 1)
{
digitalWrite(ledPin6, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
while (currentLength < 6)
{
timer();
char key2 = keypad.getKey();
if (key2 == "#")
{
currentLength = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
}
else if (key2 != NO_KEY)
{
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
lcd.print(key2);
entered[currentLength] = key2;
currentLength++;
tone1.play(NOTE_C6, 200);
delay(100);
lcd.noCursor();
lcd.setCursor(currentLength + 6, 0);
lcd.print("*");
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
}
}
if (currentLength == 6)
{
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4] && entered[5] == password[5])
{
solved = 1;
while (solved == 1)
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("BOMB 1 DEFUSED");
currentLength = 0;
digitalWrite(ledPin3, HIGH);
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("RELEASE");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("TOXIC GAS");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("CLUE: %&#$#");
delay(6000);
}
}
else
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Wrong Password!");
delay(1500);
currentLength = 0;
}
}
}
}
void timer()
{
if (Hcount <= 0)
{
if ( Mcount < 0 )
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("The Bomb Has ");
lcd.setCursor (0, 1);
lcd.print("Exploded!");
while (Mcount < 0)
{
digitalWrite(ledPin, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
}
}
}
lcd.setCursor (0, 1); // sets cursor to 2nd line
lcd.print ("Timer:");
if (Hcount >= 10)
{
lcd.setCursor (7, 1);
lcd.print (Hcount);
}
if (Hcount < 10)
{
lcd.setCursor (7, 1);
lcd.write ("0");
lcd.setCursor (8, 1);
lcd.print (Hcount);
}
lcd.print (":");
if (Mcount >= 10)
{
lcd.setCursor (10, 1);
lcd.print (Mcount);
}
if (Mcount < 10)
{
lcd.setCursor (10, 1);
lcd.write ("0");
lcd.setCursor (11, 1);
lcd.print (Mcount);
}
lcd.print (":");
if (Scount >= 10)
{
lcd.setCursor (13, 1);
lcd.print (Scount);
}
if (Scount < 10)
{
lcd.setCursor (13, 1);
lcd.write ("0");
lcd.setCursor (14, 1);
lcd.print (Scount);
}
if (Hcount < 0)
{
Hcount = 0;
}
if (Mcount < 0)
{
Hcount --;
Mcount = 59;
}
if (Scount < 1) // if 60 do this operation
{
Mcount --; // add 1 to Mcount
Scount = 59; // reset Scount
}
if (Scount > 0) // do this oper. 59 times
{
unsigned long currentMillis = millis();
if (currentMillis - secMillis > interval)
{
tone1.play(NOTE_G5, 200);
secMillis = currentMillis;
Scount --; // add 1 to Scount
digitalWrite(ledPin2, HIGH); // sets the LED on
delay(10); // waits for a second
digitalWrite(ledPin2, LOW); // sets the LED off
delay(10); // waits for a second
//lcd.clear();
}
}
}
That would most likely be a an electrical issues either with your circuit of your LCD. The fact that a simple code like yours (not trying to insult you in any way) works 9/10 of times means that there is probably nothing wrong with the code (I've seen none).
For quick check, try reinstalling the IDE. That might update the Arduino libraries that get downloaded with the IDE. I doubt that it would solve the issue but that is an quick free easy way to try
I would personnaly suggest to disconnect everything, and reconnecting them. And if it doesn't work, then return that LCD and get yourself a new one.
I had the same issue when I used a non shielded 1m ribbon cable for the 16x2 LCD. The main board with the MCU had to be placed further away from the display. It showed random character for any electromagnetic interference. For example, turning on a fluorescent tube, starting an electric screwdriver, relay switches etc. Shortening the ribon cable solved the problem.
Anyway, the LCD is sensitive to electromagnetic interference. Keep it away from these sources.

Arduino-4X4 matrix keypad with I/O shift register

I need help. I have done some research and my little understanding of keypad scanning is that the ShiftIn value of Input Column should return zero (0) when a keypad button is pressed. Mine is only returning 255 (or 11111111) in BIN. All I need is to track the zero value when a key is pressed and then scan the keys matrix to display the pressed key. I will appreciate any help. I have added my code and schematic.
]1
const int kbdRows = 4;
const int kbdCols = 4;
int LatchIn = 2; //165 pin1
int ClockPin = 3; // 595 pin11 & 165 pin2
int DataIn = 4; //165 pin9
int LatchOut = 5; // 595 pin12
int DataOut = 6; //595 pin14
int led = 7;
int PinState = 0;
char keys[kbdRows][kbdCols] = {
{ '1','2','3','4' },
{ '5','6','7','8' },
{ '9','0','A','B' },
{ 'C','D','E','F' }
};
byte KeyIsDown() {
int row;
int col;
int rowBits;
int colBits;
rowBits = 0X10;
for (row = 0; row < kbdRows; row++) {
digitalWrite(ClockPin, LOW);
digitalWrite(LatchOut, LOW);
shiftOut(DataOut, ClockPin, LSBFIRST, rowBits);
digitalWrite(LatchOut, HIGH);
delay(5);
digitalWrite(ClockPin, HIGH);
digitalWrite(LatchIn, LOW);
delay(5);
digitalWrite(LatchIn, HIGH);
colBits = shiftIn(DataIn, ClockPin, LSBFIRST);
for (col = 0; col < kbdCols; col++) {
if (colBits==0) {
// not sure what condition to put here
byte keypressed = keys[kbdRows][kbdCols]; here
// I know this is the right stuff to return here
}
return colBits;
colBits = colBits >> 1;
}
rowBits = rowBits << 1;
}
}
void setup() {
pinMode(ClockPin, OUTPUT);
pinMode(DataOut, OUTPUT);
pinMode(DataIn, INPUT_PULLUP);
pinMode(LatchOut, OUTPUT);
pinMode(LatchIn, OUTPUT);
digitalWrite(LatchOut, HIGH);
digitalWrite(LatchIn, HIGH);
Serial.begin(9600);
digitalWrite(led, HIGH);
}
void loop() {
byte retColBit = KeyIsDown();
Serial.print("ColBit: ");
Serial.println(retColBit,BIN);
delay(500);
PinState = digitalRead(DataOut);
Serial.print("DataOut: ");
Serial.println(PinState,BIN);
delay(500);
}

Why am I not able to call this method after I push a button?

I'm using a pushbutton as a toggle switch. Press it and it does "stuff A." Press it again and it does "stuff B." Why am I not able to call my method checkButtons_slow()?
int prev = 0;
int current = 0;
int val4 = 0;
int val5 = 0;
int ledPin = 13;
int prev = 0;
int current = 0;
Servo ZServo;
void setup() {
ZServo.attach(9);
pinMode(pushD3, INPUT_PULLUP);
digitalWrite(3, HIGH);
pinMode(pushD4, INPUT_PULLUP);
digitalWrite(4, HIGH);
pinMode(pushD5, INPUT_PULLUP);
digitalWrite(5, HIGH);
pinMode(pushD6, INPUT_PULLUP);
digitalWrite(6, HIGH);
pinMode(ledPin, OUTPUT);
}
void loop() {
if(digitalRead(3) == LOW) {
current = 1 - current;
}
if(current == 1 && prev == 0) {
checkButtons_slow();
//test: ZServo.write(110);
delay(500); //half a second
}
if(current == 0 && prev == 1) {
ZServo.write(80);
delay(500); //half a second
}
prev = current;
}
Here's my method:
void checkButtons_slow() {
val4 = digitalRead(pushD4);
val5 = digitalRead(pushD5);
if (val4 == LOW) {
ZServo.write(88);
} else if (val5 == LOW) {
ZServo.write(99);
} else {
ZServo.write(91); //GUESSED ON 92; SHOULD TECHNICALLY BE 90
}
}
So the commented out //test: ZServo.write(110); works. What am I missing with the checkButtons_slow();?
If you change void loop() to this then it will work to toggle the method on and off.
void loop() {
if (digitalRead(3) == LOW) {
num_presses++;
delay(500);
}
if ((num_presses % 2) == 0) {
//even
checkButtons_slow();
}
else if(num_presses == 0) {
ZServo.write(90);
}
else {
ZServo.write(85);
}
}

Arduino and processing controlling servo

I am trying to control a servo using processing.
Basically, what I want is to press a button on processing and make the servo go from 0 to 180 degrees and back every two seconds and if I press another button on processing I should be able to move the servo using a potentiometer, however I am not able to make it work properly.
For example, when I pressed the POT button, the servo goes to the desired position, however if I move the potentiometer, the servo doesn't move unless I click the pot button again.
This is the relevant code for Arduino:
void loop ()
{
if(Serial.available()){
val= Serial.read();
while(val == 1){
digitalWrite(ledCPM, HIGH);
digitalWrite(ledPot, LOW);
digitalWrite(ledFSR,LOW);
cpmMovement();
val = Serial.read();
}
while(val == 2){
digitalWrite(ledPot, HIGH);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR,LOW);
potMovement();
val = Serial.read();
}
while(val == 3){
digitalWrite(ledPot, LOW);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR,HIGH);
fsrMovement();
val = Serial.read();
}
}
}
void cpmMovement(){unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval){
previousMillis = currentMillis;
if(positionservo == 0){
positionservo = 179;
myservo1.write(positionservo);
}
else{
positionservo = 0;
myservo1.write(positionservo);
}
}
}
void potMovement(){
int analogValuePot = analogRead(Pot)/4;
Serial.print("Potentiometer reading= ");
Serial.println(analogValuePot); // This will print the raw force value
int valuePot = map(analogValuePot, 0, 255, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(valuePot); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
}
void fsrMovement(){
force = analogRead(FSR); // Reads the FSR
Serial.print("Force sensor reading = ");
Serial.println(force); // This will print the raw force value
int pos = map(force, 0, 1023, 0, 175); // Scales the force reading to degrees for servo control
Serial.print("servomotor degrees = ");
Serial.println(pos); // This will print the adjusted servo reading (an angle)
myservo1.write(pos); // Write the new angle to the servo
delay(150); // Delay 150 milliseconds before taking another reading
}
And here you can see the relevant processing code as everything else works ok
import processing.serial.*;
void mousePressed(){
println("Coordinates: " + mouseX + "," + mouseY);
if(pressedCPMButton && currentColorCPM==butColorCPM){ //Changing color CPM to pressed button color
currentColorCPM = butpreColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
myPort.write(1); //Send 1 to port
}else if(pressedCPMButton && currentColorCPM==butpreColorCPM){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
}
if(pressedPOTButton && currentColorPOT==butColorPOT){
currentColorCPM = butColorCPM;
currentColorPOT = butpreColorPOT;
currentColorFSR = butColorFSR;
myPort.write(2); //Send 2 to port
}else if(pressedPOTButton && currentColorPOT==butpreColorPOT){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
}
if(pressedFSRButton && currentColorFSR==butColorFSR){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butpreColorFSR;
myPort.write(3); //Send 3 to port
}else if(pressedFSRButton && currentColorFSR==butpreColorFSR){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
}
}
boolean pressedButtonCPM(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean pressedButtonPOT(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean pressedButtonFSR(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}

Rectangle pattern using arduino Mega

I am using an Arduino Mega to implement a Rectangle pattern movement of a device which mean the length and breadth should decrease each time it reaches the end edges. I have written the code but it is not working
//servo header
#include <Servo.h>
#include <Math.h>
Servo myservo;//servo Object
//motor pin config
int m1r = 9;
int m1f = 10;
int m2f = 12;
int m2r = 11;
//infrared counter
int ir1val=0;//pulse count from irval
int ir2val=0;//pulse count from irval
int len=1;
int bred=2;
//Different sensors used
int temp;
int buzz = 8;
const float radpulse = 0.05;//1 pulse = 0.05 meter
double pulsecount1;//pulse counter;
double pulsecount2;//pulse counter;
int pc1;//integer
int pc2;
void setup() {
// initialize the Motor pin as an output:
pinMode(m1f, OUTPUT);
pinMode(m1r, OUTPUT);
pinMode(m2f, OUTPUT);
pinMode(m2r, OUTPUT);
pinMode(buzz,OUTPUT);
myservo.attach(A0);//BLDC motor attachment
Serial.begin(9600);// standard serial bandwidth of all modules
}
void loop()
{
pulsecount1=len/radpulse;
pc1=(int)round(pulsecount1);
pulsecount2=bred/radpulse;
pc2=(int)round(pulsecount2);
pc1=3;
pc2=2;
myservo.write(30);
delay(5000);
labelL1:
if((pc2 == 0)&&(pc1 == 0))
{
goto Stop;
}
while(pc1 > ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto hibernate;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m2f,HIGH);
digitalWrite(m1f,HIGH);
//digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir1val++;
}
}
if(pc1 == ir1val)
{
/*if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc1--;
ir1val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelB1;
}
labelB1:
while(pc2 != ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto label2;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m1f,HIGH);
digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir2val++;
}
}
if(pc2 == ir2val)
{
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc2=pc2--;
ir2val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelL1;
}
Stop:
myservo.write(0);// stop servo
digitalWrite(m1f,LOW);// stop motor
digitalWrite(m2f,LOW);//stop motor
digitalWrite(buzz,HIGH);//Buzzer
delay(5000);
goto Stop;
/*
hibernate:
temp=((5.0*analogRead(A2)*100)/1024);
if(temp >40)
{
delay(1000);
goto hibernate;
}
*/}

Resources