I would like to stop DC motors after the counter reaches a value e,g num=3 (HIGH's) as shown in the following image:
White line is 3cm thick, black box 30cm squared!
The two IR sensors, int rightSens=30; and int leftSens=32; are working and give a 1 on sensing a black surface, and 0 on any other, i.e. white and in the absence of anything.
I would like the robot to count as in my loop, 3 LOWs incase counting white or 2 HIGHs incase of counting black,then do something e.g. stop, as shown with the while loop.
Something must be wrong, it seems like not counting because it doesn't stop after passing 3 boxes or 3 white lines. any help very appreciated!
int pwm=2; //initializing pin 2 as pwm
int leftMotorin_1=22;//right
int leftMotorin_2=24;
int pwm2=3;
int rightMotorin_3=26;
int rightMotorin_4=28;
//sensor
int rightSens=30;
int leftSens=32;
int rightSensState;
int leftSensState;
//Variables for counting lines
int previousState=0;
int currentState=0;
void setup ()
{
pinMode(pwm,OUTPUT); // set pwm pin as output
pinMode(leftMotorin_1,OUTPUT); //logic pins are also set as output
pinMode(leftMotorin_2,OUTPUT);
pinMode(pwm2,OUTPUT); // set pwm pin as output
pinMode(rightMotorin_3,OUTPUT); //logic pins are also set as output
pinMode(rightMotorin_4,OUTPUT);
//pinMode(obSens,INPUT);
pinMode(rightSens,INPUT);
pinMode(leftSens,INPUT);
}
void readSensors()
{
rightSensState = digitalRead(rightSens);
leftSensState = digitalRead(leftSens);
}
void moveForward()
{
digitalWrite(leftMotorin_1,HIGH);
digitalWrite(leftMotorin_2,LOW);
analogWrite(pwm,150);
digitalWrite(rightMotorin_3,HIGH);
digitalWrite(rightMotorin_4,LOW);
analogWrite(pwm2,150);
}
void stopWheels()
{
//For brake
digitalWrite(leftMotorin_1,LOW);
digitalWrite(leftMotorin_2,LOW);
digitalWrite(rightMotorin_3,LOW);
digitalWrite(rightMotorin_4,LOW);
}
void loop()
{
int count=0;
int num=3;
moveForward();
readSensors();
(currentState=(leftSensState && rightSensState));
if (currentState != previousState );
{
if (currentState==HIGH)
count++;
previousState=currentState;
}
while (count!=num) {
moveForward();
readSensors();
(currentState=(leftSensState && rightSensState));
if (currentState != previousState );
{
if (currentState==HIGH)
count+=1;
previousState=currentState;
}
if (counter == num)
{
stopWheels();
delay(20);
break;
}
}
}
Related
we are given an array and its size and a interger k we need to find all/count of all subsequence that has constant arr[i]-arr[i-1] diff for all element and it size should be k
i have tryed my approach and it is clear to understand also but it just printing count i.e cnt as 0 why any one
#include<bits/stdc++.h>
#include
using namespace std;
void subseq(vector<int>&arr,int idx,int n,int k,int& cnt,vector<int>&nums){
if(idx==n-1){
if(nums.size()==k){
bool flag=1;
int dist=nums[1]-nums[0];
for(int i=2;i<n;i++){
if(nums[i]-nums[i-1]==dist){
continue;
}else{
flag=0;
}
}
if(flag)
cnt+=1;
}
return;
}
nums.push_back(arr[idx]);
subseq(arr,idx+1,n,k,cnt,nums);
nums.pop_back();
subseq(arr,idx+1,n,k,cnt,nums);
}
int main(){
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
vector<int>v(n);
// cout<<1<<endl;
for(int i=0;i<n;i++)
cin>>v[i];
vector<int>x;
int cnt=0;
// cout<<1<<endl;
subseq(v,0,n,m,cnt,x);
cout<<cnt<<endl;
}
return 0;
}
problem is int min redeclared as a different kind of symbol;
Try to check the code and change the whole number, but I have the same problem, regardless of whether I change it to 0 1 2 3 4. (Sorry, I'm not good at English and just started to learn coding from education through Google and YouTube.)
This is my code:
int int_count;
int sec=0;
int min=1;
int flag_One_Time_Send_Old = 100;
void timer0_ISR(void) // the RTCC (timer0) overflows (255->0).
if(--int_count==0) // per second.
sec++;
int_count=INTS_PER_SECOND;
if (sec==60)
{
min++;
sec=0;
Serial.print("min: ");
Serial.println(min);
}
Serial.print("sec: ");
Serial.println(sec);
}
Thank for help
I hope this works for you. min has been renamed as minute as min is used as min() macro function by Arduino.
int int_count; int sec=0; int minute=1; //min renamed as minute
int flag_One_Time_Send_Old = 100;
void timer0_ISR(void) // the RTCC (timer0) overflows (255->0).
if(--int_count==0) // per second.
sec++;
int_count=INTS_PER_SECOND;
if (sec==60)
{
minute++;
sec=0;
Serial.print("minutes: ");
Serial.println(minute);
}
Serial.print("sec: ");
Serial.println(sec);
}
After watching this video. https://www.youtube.com/watch?v=QBj8OLig8Kg
Which is how to create a homekit system without having to use a brigde I wanted to create my own.
My idea is to have 2 different lights that are connected to the internet and I handle them with siri and 2 external buttons that can turn off the light without having to always use siri. (for example if my cell phone runs out of battery). All this in the same ESP 8266.
I could already operate the two lights without the need of a homebridge with the following code
#define Relay1 16 //D0
#define Relay2 5 //D1
#define button1 14 //D5
#define button2 12 //D6
#define button3 13 //D7
bool state1 = false;
bool state2 = false;
// ---------------------------------------------------- Inicializacion de wifi -------------------------------------------------
static void wifi_init() {
struct sdk_station_config wifi_config = {
.ssid = WIFI_SSID,
.password = WIFI_PASSWORD,
};
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&wifi_config);
sdk_wifi_station_connect();
}
// -------------------------------------------------------- Logica de Relay1 -----------------------------------------------------
void relay1_write(bool on) {
gpio_write(Relay1, on ? 1 : 0);
}
void relay1_init() {
gpio_enable(Relay1, GPIO_OUTPUT);
relay1_write(state1);
}
void relay1_identify_task(void *_args) {
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
relay1_write(true);
vTaskDelay(100 / portTICK_PERIOD_MS);
relay1_write(false);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
vTaskDelay(250 / portTICK_PERIOD_MS);
}
relay1_write(state1);
vTaskDelete(NULL);
}
void relay1_identify(homekit_value_t _value) {
printf("Relay 1 identify\n");
xTaskCreate(relay1_identify_task, "Relay Identify", 128, NULL, 2, NULL);
}
homekit_value_t relay1_on_get() {
return HOMEKIT_BOOL(state1);
}
void relay1_on_set(homekit_value_t value) {
if (value.format != homekit_format_bool) {
printf("Invalid value format: %d\n", value.format);
return;
}
state1 = value.bool_value;
relay1_write(state1);
}
// -------------------------------------------------------- Logica de Relay2 -----------------------------------------------------
void relay2_write(bool on) {
gpio_write(Relay2, on ? 1 : 0);
}
void relay2_init() {
gpio_enable(Relay2, GPIO_OUTPUT);
relay2_write(state2);
}
void relay2_identify_task(void *_args) {
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
relay2_write(true);
vTaskDelay(100 / portTICK_PERIOD_MS);
relay2_write(false);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
vTaskDelay(250 / portTICK_PERIOD_MS);
}
relay2_write(state2);
vTaskDelete(NULL);
}
void relay2_identify(homekit_value_t _value) {
printf("Relay 2 identify\n");
xTaskCreate(relay2_identify_task, "Relay Identify", 128, NULL, 2, NULL);
}
homekit_value_t relay2_on_get() {
return HOMEKIT_BOOL(state2);
}
void relay2_on_set(homekit_value_t value) {
if (value.format != homekit_format_bool) {
printf("Invalid value format: %d\n", value.format);
return;
}
state2 = value.bool_value;
relay2_write(state2);
}
// ------------------------------------------------ Configuracion del server de Homekit ------------------------------------------
homekit_accessory_t *accessories[] = {
HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_lightbulb, .services=(homekit_service_t*[]){
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]){
HOMEKIT_CHARACTERISTIC(NAME, "Cuarto"),
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Estonian Port"),
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "ASD123"),
HOMEKIT_CHARACTERISTIC(MODEL, "C.U.C.A"),
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.1"),
HOMEKIT_CHARACTERISTIC(IDENTIFY, relay1_identify),
NULL
}),
HOMEKIT_SERVICE(LIGHTBULB, .primary=true, .characteristics=(homekit_characteristic_t*[]){
HOMEKIT_CHARACTERISTIC(NAME, "Luz"),
HOMEKIT_CHARACTERISTIC(ON, false,
.getter=relay1_on_get,
.setter=relay1_on_set
),
NULL
}),
NULL
}),
HOMEKIT_ACCESSORY(.id=2, .category=homekit_accessory_category_lightbulb, .services=(homekit_service_t*[]){
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]){
HOMEKIT_CHARACTERISTIC(NAME, "Cuarto"),
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Estonian Port"),
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "ASD123"),
HOMEKIT_CHARACTERISTIC(MODEL, "C.U.C.A"),
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.1"),
HOMEKIT_CHARACTERISTIC(IDENTIFY, relay2_identify),
NULL
}),
HOMEKIT_SERVICE(LIGHTBULB, .primary=true, .characteristics=(homekit_characteristic_t*[]){
HOMEKIT_CHARACTERISTIC(NAME, "Luz"),
HOMEKIT_CHARACTERISTIC(ON, false,
.getter=relay2_on_get,
.setter=relay2_on_set
),
NULL
}),
NULL
}),
NULL
};
homekit_server_config_t config = {
.accessories = accessories,
.password = "111-11-111"
};
// ----------------------------------------------------------- MAIN ----------------------------------------------------------
void user_init(void) {
uart_set_baud(0, 115200);
wifi_init();
relay1_init();
relay2_init();
homekit_server_init(&config);
}
but i cant figure out how to implemet the two external buttons. I tried implementing:
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop() {
if (digitalRead(button1))
{
relay1_write(state1);
}
}
but it gives me error, since, I have no way to implement the library .
I also try to understand this example.
https://github.com/maximkulkin/esp-homekit-demo/tree/master/examples/button
but it does not apply to what I want to do since in that case because the button is recognized by homekit and I want it to just turn off and turn on the light
Thanks so much for your time!
While writing my question I realized something and after thinking it better I think I should connect the relay of the output to a combined light key. in that way it would give the possibility of turning the light on and off in an analogical way. Sorry for the inconvenience! thank you
I use an Arduino Uno with Arduino IDE 1.8.3. I have two arrays. I want to write a Deputy function that can add two arrays, and return the result to the main function and print it.
But I want to use x(sizeof(a)), but it seems not correct...
How do I solve this problem?
This is my code:
int a[]={1,2,3,4,5,6},b[]={1,1,1,1,1,1};
void setup() {
Serial.begin(9600);
int *p;
p = add(a,b);
for(int i=0;i<4;i++){
Serial.print(*(p+i));
}
}
void loop() {
}
int * add(int *a,int *b) {
int x = sizeof(a);
int y = sizeof(b);
static int z[4];
for(int i=0;i<4;i++) {
z[i]=a[i]+b[i];
}
return z;
}
int* a does not know the size of the array.
Easiest pass it as an extra parameter.
The next problem is that your static result cannot change its size dynamically.
static has additional problems anyway, in general.
int* add(const int *a,const int *b, int* result, byte size) {
for(byte i=0; i<size; i++) {
result[i]=a[i]+b[i];
}
return result;
}
Returning the result as the return value may be convenient.
I was trying to write items to the EEPROM and later read them out. I was finding the reading back I was not getting the same as I put in at times. I narrow down to an example I can show you. Below I read into variables 2 address.
const int start_add_type = (EEPROM.length() - 10);
const int start_add_id = (EEPROM.length() - 4);
I then look at the value (via RS232)
Serial.begin(9600);
Serial.println(start_add_type);
Serial.println(start_add_id);
of them at the start of the setup() and see I get
1014
1020
I then look again at the end
Serial.println(start_add_type);
Serial.println(start_add_id);
and I get
1014
818
I cannot see why this should change. I did try calling them const e.g. const
const int start_add_type = (EEPROM.length() - 10);
const int start_add_id = (EEPROM.length() - 4);
but this gave the same result. So here I sit very puzzled at what I must have missed. Anyone got any idea?
#include "EEPROM.h"
int start_add_type = (EEPROM.length() - 10);
int start_add_id = (EEPROM.length() - 4);
char ID[7] = "ENCPG2";
char Stored_ID[5];
char Input[10];
//String Type;
void setup()
{
Serial.begin(9600);
Serial.println(start_add_type);
Serial.println(start_add_id);
// start_add = (EEPROM.length() - 10); // use this method to be PCB independent.
for (int i = 0; i < 6; i++)
{
Stored_ID[i] = EEPROM.read(start_add_type + i); // Read the ID into the EEPROM.
}
if (Stored_ID != ID) // Check if the one we have got is the same as the one in this code ID[7]
{
for (int i = 0; i < 6; i++)
{
EEPROM.write(start_add_type + i, ID[i]); // Write the ID into the EEPROM.
}
}
Serial.println(start_add_type);
Serial.println(start_add_id);
}
void loop()
{
}
You are overwriting your memory in this loop:
for (int i = 0; i < 6; i++)
{
Stored_ID[i] = EEPROM.read(start_add_type + i);
}
Stored_ID array is 5 bytes long, so writing to Stored_ID[5] will rewrite also the start_add_id variable, thus the weird value 818, which equals to 0x0332 HEX and 0x32 is the '2' character of your ID
For fixing this issue, declare Stored_ID in this way:
char Stored_ID[6];
if (Stored_ID != ID)
This is nonsense: You compare two different addresses, which are never equal. If you want to compare the content, you should do it in a loop. (e.g. directly when reading the EEPROM value into Stored_ID[i] )
Alternatively, Stored_ID could be a 0-terminated text as well and you might use
if (strcmp(Stored_ID, ID) != 0)