Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1069

Arduino using a button to switch on and off an IR laser

$
0
0

Hi folks, just needing some help with this code, I'm trying to make it so when I push the button and it counts up it will turn on the IR laser and will stay on until the button is pressed again where it will turn off and wait for the button to be pressed again. My issue is that I can get the IR laser to turn on fine and everything else with the button works but I can't figure out how to turn off the laser once its on. Any help would be greatly appreciated.
Boba_ServoOLED_code.ino (4.3 KB)

/*!
 * @File  DFRobot_IraserSensor.ino
 * @brief  In this example, the infrared laser ranging sensor is used to measure the distance, and the sensor data is processed to obtain the measured distance
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence  The MIT License (MIT)
 * @author  [liunian](nian.liu@dfrobot.com)
 * @version  V1.0
 * @date  2020-08-13
 */
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);//Define software serial, 3 is TX, 2 is RX
char buff[4]={0x80,0x06,0x03,0x77};
unsigned char data[11]={0};




#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>

#include <Adafruit_SSD1306.h>
#include <splash.h>

#include <ezButton.h>
ezButton limitSwitch(11);

#include <Servo.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

//int Button=11; //attaches button to pin 11


int y,x=0;


void setup()
{
  
  //pinMode(Button, INPUT);
  Serial.begin(115200);
  mySerial.begin(9600);
  //limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
    myservo.write(180); //Sets servo motor to 180 degrees
    TIMSK0=0;
  }
}

//void(* resetFunc) (void) = 0;//declare reset function at address 0

void LEDdisplay (float output){
  display.clearDisplay();
  display.setTextSize(2); // Draw 2X-scale text
  display.setTextColor(WHITE);
  display.setCursor(55, 20);
  display.setRotation(2);
  display.println(String(output)+"m");
  display.display();      // Show initial text
  delay(150);
}

void loop()
{
limitSwitch.loop(); // MUST call the loop() function first  
//y = limitSwitch.isPressed();
//int y = limitSwitch.getState();
  if (limitSwitch.isPressed()){
 
   //x=x+1;
   x++;
   Serial.println(x);
  }
  if (x==0){ //button has not been pressed
   myservo.attach(9);  // attaches the servo on pin 8 to the servo object
   //myservo.write(180); //Sets servo motor to 180 degrees
   delay(500);
   myservo.detach();
   display.display();
   display.clearDisplay(); //Clears OLED screen
   
   
    }
  if (x==1){ //button has been pressed once

   myservo.attach(9);  // attaches the servo on pin 8 to the servo object
   myservo.write(0); //sets servo to 0 degrees
   delay(500);
   myservo.detach();
   display.display();
   mySerial.print(buff);
  while (x==1)
  {
  //int y = limitSwitch.getState();
  if (limitSwitch.isPressed()){
 
   //x=x+1;
   x++;
   Serial.println(x);
   delay(200);
   break;
  }
    if(mySerial.available()>0)//Determine whether there is data to read on the serial 
    {
      delay(50);
      for(int i=0;i<11;i++)
      {
        data[i]=mySerial.read();
      }
      unsigned char Check=0;
      for(int i=0;i<10;i++)
      {
        Check=Check+data[i];
      }
      Check=~Check+1;
      if(data[10]==Check)
      {
        if(data[3]=='E'&&data[4]=='R'&&data[5]=='R')
        {
          Serial.println("80.0m>");
        }
        else
        {
          float distance=0;
          distance=(data[3]-0x30)*100+(data[4]-0x30)*10+(data[5]-0x30)*1+(data[7]-0x30)*0.1+(data[8]-0x30)*0.01+(data[9]-0x30)*0.001;
          Serial.print("Distance = ");
          Serial.print(distance,3);
          Serial.println(" M");

          Serial.println("DISTANCE"); //show text on serial monitor

          LEDdisplay(distance);
        
          }
          
        }
      }
      else
      {
        Serial.println("!]£$/^&?");
      }
    }
    delay(20);
  }
  if (x==3){
    display.display();
    display.clearDisplay(); //Clears OLED screen
    myservo.write(180); //Sets servo motor to 180 degrees
    
    x=0;
    //resetFunc(); //call reset 
  }
}

3 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 1069

Trending Articles