If you specify a pattern in advance and the tilt sensor moves according to the pattern, you want to write a code that sends out a signal.

Asked 2 years ago, Updated 2 years ago, 107 views

Hello, I'm going to do a graduation work that sends out a specific output when I adjust the slope to the pattern.

I use the tilt sensor above

For example, if you set the pattern to front, back, left, and right, Therefore, the instrument must be tilted forward, backward, left and right to emit a specific signal. I'm using Arduino. What I've done so far is to print the led according to each tilt. But since then, I haven't really figured out what to do with a pattern, and I'm asking you to give me some advice, because I don't know what to do with it, so I'm asking you for advice"T"

Until now, each LED is turned on according to the direction of inclination.

Additionally, below it'since I wrote this, but it didn't move at all"T" Please give me some advice on how to make the chords Thank you! Regardless of the order of front, back, left, right, or any direction, make a pattern Compare that value with the current value, and if it's right, continue, and if it's all right, output the sound to speaker A I don't know how to set the code to output sound to speaker B while RESET I ask for your help me. Thank you.


int  OUTA;
int  OUTB;
int Red = A0; // I'm going to turn on the signal to Bluetooth first
int sensor; // I added a new one this time
typedef enum {UP, DOWN, LEFT, RIGHT} direction;
direction sequence[] = {UP, DOWN, LEFT}; // pattern of up-down-left
int position = 0;

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(Red, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  OUTA = digitalRead(6);
  OUTB = digitalRead(7);
  Sensor = 2*OUTA + OUTB; // Express OUTA and OUTB as one value of sensor to compare patterns; is it right to do well?Hah!
  if (OUTA == 0 && OUTB == 0) {
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  }
  if (OUTA == 1 && OUTB == 0) {
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  }
  if (OUTA == 0 && OUTB == 1) {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
  }
  if (OUTA == 1 && OUTB == 1) {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
  }
  Serial.print("Photo 1:"); // outa and outb and made to see how to read the sensor values
  Serial.print(digitalRead(6), DEC);
  Serial.print(" ; ");
  Serial.print("Photo 2:  ");
  Serial.println(digitalRead(7), DEC);
  Serial.print(" ; ");
  Serial.print("current_direction:  ");
  Serial.println(sensor, DEC);
  delay(1000);
  direction current_direction;
  //Save the value of current_direction according to the values of OUTA and OUTB. next time

  if (sequence[position] == current_direction) {
    position++;
    if (position == 3)
    {
      digitalWrite(Red, LOW);
    }
  }
  else {
    int position = 0;
    digitalWrite(Red, HIGH);
  }
}

arduino tilt-sensor

2022-09-21 22:57

1 Answers

For example, if I input front-rear-left-right-left, does it make a sound? You can put a value in the array and check if that value comes out.

For your information, direction is for storing directions, so you can just use int and use 0,1,2,3.

typedef enum {UP, DOWN, LEFT,RIGHT} direction;
direction sequence[]={UP,DOWN,LEFT};//up-down-left pattern
int position=0;

void loop(){
    direction current_direction;            
    //Save the value of current_direction according to the values of OUTA and OUTB. next time

    if(sequence[position] == current_direction){
        position++;
        if(position == 3){
            //Success
        }
    }
    else{
        //To initialize as soon as an invalid input is received, you can set position=0 here.
        //It should be entered as up-down-left, but it depends on whether or not you allow up-down-left.
    }
}



2022-09-21 22:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.