So this is what I have so far. I want it to be that if I press the left side of the screen, the red square shows up, and when I touch the right side the robot turns right. Also when I stop pressing it stops the action. What do I need to add to get to that?
while(true){
if(Brain.Screen.pressing()){
Brain.Screen.setFillColor(red);
Brain.Screen.drawRectangle(240,120,100,100);
}
else{
Brain.Screen.clearScreen();
}
if(Brain.Screen.pressing()){
rightMotor.spin(reverse);
leftMotor.spin(forward);
}
else{
rightMotor.stop();
leftMotor.stop();
}
}
only check for pressing once in the while loop, then use xPosition and/or yPosition to determine the last position pressed on the screen, make a decision based on the numbers returned, for example, if xPosition() is less than 240 do one action else do the other.
https://api.vexcode.cloud/v5/html/classvex_1_1brain_1_1lcd.html
1 Like
so like this?, sorry Iām not that good. I may not have been clear, I need it that if I press one side it does that action but when I let go it stops.
while(true){
if(Brain.Screen.xPosition()<240){
Brain.Screen.setFillColor(red);
Brain.Screen.drawRectangle(240,120,100,100);
}
else{
Brain.Screen.clearScreen();
}
if(Brain.Screen.xPosition()>240){
rightMotor.spin(reverse);
leftMotor.spin(forward);
}
else{
rightMotor.stop();
leftMotor.stop();
}
}
I was thinking of something more like this.
int main() {
bool screenPressed = false;
while(1) {
if( Brain.Screen.pressing() && !screenPressed ) {
// screen was pressed
// set flag so we only execute this code once
screenPressed = true;
// now decide if left or right side of the screen
if( Brain.Screen.xPosition() < 240 ) {
// left hand side of screen
// do something.
}
else {
// right hand side of screen
// do something else
}
}
else
if( !Brain.Screen.pressing() ) {
// screen is not pressed
screenPressed = false;
// do something when not pressed
}
this_thread::sleep_for(20);
}
}
9 Likes