I am very new to programming. I am writing a program, for an Arduino clone. I have a 12V coil relay, that works off an 12V IR switch. The relay has picked up 5V from the Arduino, and will supply 5V to pin 1, when the relay is switched EG something moving in front of the IR switch.
The program seems to ignore the incoming 5V, and continues the program as though it wasn't even trying to read it.
When I check on the serial monitor, what the pin is reading (high or low), it just shows "xxx" when there is nothing present, and doesn't react when my hand signals the switch.
I am sure it is some basic C++ code syntax error.
Code below:
void loop() {
analogWrite(channel_a_enable, 255);
//turns feeder motor on through H bridge, to feed brass case
digitalWrite(channel_a_input_1, HIGH);
digitalWrite(channel_a_input_2, LOW);
val = digitalRead (sensor_ir_pin);
//reads IR switch and checks if it has a case present yet
Serial.println(val);
if (val = HIGH)
//if brass case rolls past sensor, switching IR switch and relay
analogWrite(channel_a_enable, 0);
//turns feeder motor off
digitalWrite(channel_a_input_1, LOW);
digitalWrite(channel_a_input_2, LOW);
current_potent = analogRead(A0);
//reads the potentiometer setting (set for a max of 15 seconds time)
delay(current_potent * 14.6627);
//delays, while the brass case is in the flame, getting annealed. Also provides the multiplier for 15000 mill seconds into 1023 read point on the potentiometer
Related
I am a begginer in STM. I have STM32 NUCLEO-F411RE, Pololu 1570 6V 2220RPM DC Motor, L298N DC Motor Driver and 6V 1,3Ah Xtreme Acumulator. I want my motor to be just rotating for example with 80% duty cycle pwm. I have it connected as in this picture:Here is picture of my connection
But instead of 2 motors I have one and instead of Arduino I have STM. In my case pin ENA from motor driver is connected to PB6 pin where I set TIM4 with Channel 1 PWM generation. And IN1 pin from motor driver is connected to PA11 and IN2 to PA12. Here is the code which I add by myself to main():
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(GPIOA,Dir1_Pin,GPIO_PIN_SET); // Start motor clock wise rotation, Dir1_Pin is PA11 and Dir2_Pin is PA12
HAL_GPIO_WritePin(GPIOA,Dir2_Pin,GPIO_PIN_RESET);
HAL_TIM_Base_Start(&htim4);
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
htim4.Instance->CCR1=????; //What number should I put here to have my motor rotating with 80% duty cycle?
}
/* USER CODE END 3 */
}
I have already been looking for explanations on the internet, make calculations for a week, but didn't find anything which would work for my case. Red light on motor driver is lighting. With some of results from my calculations it was making a "tick tick" sound, but the motor was not rotating. I do not know exactly what values can be send to ENA Pin for my specific DC motor.
Duty cycle is just a high-to-low ratio of the PWM clock.
To calculate value of CCR, you have to know PWM length in timer clocks, that is usually set up in ARR, that is mapped to Init.Period structure member:
void PWM_SetDutyCycle(uint16_t dc_percent) {
// recalculate into pulse width
uint16_t dc = htim1.Init.Period * ((uint32_t)dc_percent) / ((uint32_t) 100);
htim4.Instance->CCR1 = dc;
}
I have to authenticate a transaction, say a system which will use a pre-fed authentication id to verify any user using the system. The authentication id is supposed to be changed by a super-user using communication through Serial Protocol. Each time a transaction gets completed user has to press a push button to officially finish the transaction and enable super user to feed another authentication ID.
I am able to change the authentication id using Serial event interupt in Arduino, but my pin change interupt is working only once, so I cannot finish the 2nd transaction.
I tried it without using pin change interupt also but, that made a lot of mess in my code and did not work properly as i wanted, maybe some-thing or some logic I am not able to apply correctly.
```Arduino C language`````
void setup()
{
pinMode(44, OUTPUT);
pinMode(45, OUTPUT);
pinMode(46, OUTPUT);
Serial.begin(9600);
//gsm_port.begin(9600);
// Turn on the transmission, reception, and Receive interrupt
Serial1.begin(9600);
attachInterrupt(0, pin_ISR, RISING); //0 here defines pin 2 of Mega2560
}
void pin_ISR() //ISR for when box is manually closed a latch gets closed and high value is recvd on pin 2(only pins 2,3 are GPIO interupt pin of Mega2560)//
{
b1 = digitalRead(2);
if(b1==HIGH)
{
digitalWrite(44, LOW);
digitalWrite(45, LOW);
digitalWrite(46, LOW);
memset(&fed_id[0], 1, sizeof(fed_id)); //clearing fed_id so that once used cannot be used again till new id is feeded through serial event
}
}
void serialEvent1() //Serial Rx ISR for feeding new fed_id
{
while (Serial1.available())
{
rec = Serial1.read();
a[i] = rec;
i++
}
}
void loop()
{
char key = keypad.getKey();
if (key)
{
///.....some operation here......///
switch(key)
{ //try implementing shelf not oprning feature if occupied here with each case using 3 IR sensors.
case '1': digitalWrite(44, HIGH);
break;
case '2': digitalWrite(45, HIGH);
break;
case '3': digitalWrite(46, HIGH);
break;
}
}
}//closing for if(key)
}//closing for void loop()
If the above is possible without using interupts then too i would love to have a soultion. Please help me to understand why this is not going the right way and also help me finding a solution
There was no problem with the code actually and what was discussed in comments that we need to set and reset the interrupt flags/pins, is not the case with Arduino, Arduino does it automatically, while in many controllers we do need to do those things, however the problem here was with the hardware and not any part of code. Just a resistor was creating this problem on Proteus Simulation, however on actual hardware it worked completely fine.
I recently got a HC-05 Bluetooth module for my arduino, but I cannot send or receive data from it. I used a code to turn on or off a led, but after I send a character from the Serial Monitor of my PC, I get ⸮. Also the module does not respond to any AT command. HC-05 ConnectionArduino connection I ran the Serial both in 9600 and 38400 baud but nothing changed. Also I have tried both no line ending and both NL and CR. But is wrong with this module? Here is my code:
/*
Arduino Turn LED On/Off using Serial Commands
Created April 22, 2015
Hammad Tariq, Incubator (Pakistan)
It's a simple sketch which waits for a character on serial
and in case of a desirable character, it turns an LED on/off.
Possible string values:
a (to turn the LED on)
b (tor turn the LED off)
*/
char junk;
String inputString="";
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set the baud rate to 9600, same should be of your Serial Monitor
pinMode(13, OUTPUT);
}
void loop()
{
if(Serial.available()){
while(Serial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
Serial.println(inputString);
while (Serial.available() > 0)
{ junk = Serial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the LED on
digitalWrite(13, HIGH);
}else if(inputString == "b"){ //incase of 'b' turn the LED off
digitalWrite(13, LOW);
}
inputString = "";
}
}
I will go step by step-
The connection
Arduino Pins Bluetooth Pins
RX (Pin 0) ———-> TX
TX (Pin 1) ———-> RX
5V ———-> VCC
GND ———-> GND
Connect a LED negative to GND of arduino and positive to pin 13 with a resistance valued between 220Ω – 1KΩ. And your done with the circuit.
Note : Don’t Connect RX to RX and TX to TX of Bluetooth to arduinoyou will receive no data , Here TX means Transmit and RX means Receive.
/*
* This program lets you to control a LED on pin 13 of arduino using a bluetooth module
*/
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the baud for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");
if(data == '1') // Checks whether value of data is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(data == '0') // Checks whether value of data is equal to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}
}
Link to Connection : https://halckemy.s3.amazonaws.com/uploads/image_file/file/153200/hc-05-LED%20blink%20Circuit.png
NOTE : While uploading the code , remove the TX and RX wires of Bluetooth Module from Arduino, once upload is completed, connect them.
#include <SoftwareSerial.h>
SoftwareSerial hc(2, 3); // RX | TX
void setup()
{
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
Serial.begin(9600);
Serial.println("Enter AT commands:");
hc.begin(38400); // HC-05 default speed in AT command more
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (hc.available())
Serial.write(hc.read());
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
hc.write(Serial.read());
}
Use this code to test the bluetooth module in command mode.there are two modes in hc-05. one is command mode and other is data mode.
Press the button which is on bluetooth module for few sec. then the led Toggles slowly at this point the module is in command mode and in this you can test the AT commands.
Note: Open the serial monitor in 9600 baud rate
I trie to drive a servo with the Raspberry PI using the PWM on GPIO Pin 18 i wired the setup like you can see below.
When i drive the servo i can do this without any problems the commands that i use you can see below.
gpio -g mode 18 pwm
gpio pwm-ms
gpio pwmc 192
gpio pwmr 2000
gpio -g pwm 18 150
gpio -g pwm 18 200
That works fine, to go to the position without any problem but when i try to do the same with a C program using wiringpi like you can see below.
#include <wiringPi.h>
#include <stdio.h>
int main (void)
{
printf ("Raspberry Pi wiringPi test program\n");
wiringPiSetupGpio();
pinMode (18, PWM_OUTPUT) ;
pwmSetMode (PWM_MODE_MS);
pwmSetRange (2000);
pwmSetClock (192);
pwmWrite(18,150);
delay(1000);
pwmWrite(18,200);
return 0;
}
The program and the raspberry pi chrash so i have to reboot them does anybody know what i do wrong and how i can solve the problem it is very frustrating?
I spent weeks on controlling two servo (SG90) using WiringPi and programming in C, there're three things that I recommend.
1.Using BCM GPIO instead of WiringPi Pin because controlling more than one servo, you might need more than one pin such like 1(WiringPi Pin)/18(BCM GPIO) for another servo, For RPi3 B+ version, it give access to two channels for hardware PWM. Channel 0 on gpios 12/18 and channel 1 on gpios 13/19, it's easy and no need to worry about pin mapping exists if you adpopt BCM GPIO.
2.Better make sure there is only one program access PWM. pins at one time. Based on my experience, if you find that using command like "gpio -g pwm 18 25" is workable but otherwise using code like "pwmWrite(18, 25)" doesn's get any servo responds, maybe try "ps -A" to make sure if any other program is racing the access of your servo.
3.The last and the hardest one for me, when I execute pwmWrite(18, 25)" on PWM. pin 18 triggers the same instruction onto PWM. pin 12, which means pwmWrite(18, 25) triggers pwmWrite(12, 25). To solve this situation, changing the modes of other pins of servos which should freeze without any moving to be input mode and set all of them to be pull-down.
For details, codes for controlling two servos with PWM. Channel 0 on gpios 12/18.
Basic function:
void servo_init() {
servo_open(0);
delay(DELAY_SERVO);
servo_open(1);}
and
void servo_open(int servo) {
switch (servo) {
case 0:
pullUpDnControl(SERVO_0, PUD_OFF);
pinMode(SERVO_0, PWM_OUTPUT);
pwmSetMode(PWM_MODE_MS);
pwmSetClock(PWM_CHANNEL_0_CLOCK);
pwmSetRange(PWM_CHANNEL_0_RANGE);
break;
case 1:
pullUpDnControl(SERVO_1, PUD_OFF);
pinMode(SERVO_1, PWM_OUTPUT);
pwmSetMode(PWM_MODE_MS);
pwmSetClock(PWM_CHANNEL_0_CLOCK);
pwmSetRange(PWM_CHANNEL_0_RANGE);
break;
default:
break;
}}
and
void servo_close(int servo) {
switch (servo) {
case 0:
pinMode(SERVO_0, INPUT);
pullUpDnControl(SERVO_0, PUD_DOWN);
break;
case 1:
pinMode(SERVO_1, INPUT);
pullUpDnControl(SERVO_1, PUD_DOWN);
break;
default:
break;
}}
and
void servo(int servo, int angle) {
switch (servo) {
case 0:
servo = SERVO_0;
break;
case 1:
servo = SERVO_1;
break;
default:
servo = -1;
break;
}
switch (angle) {
case 90:
value = 25;
break;
case -90:
value = 10;
break;
case 0:
value = 14;
break;
default:
break;
}
if (servo == -1) return;
pwmWrite(servo, value);}
Rotate one servo connected to 18 (BCM GPIO)
Close others before you are going to rotate one
servo_close(1);
delay(DELAY_SERVO);
Rotate
servo(0, 90);
delay(3*DELAY_MAGIC);
servo(0, 0);
Reset all of servos to their init angles for fixing servo occasional jitter
delay(DELAY_SERVO);
servo_init();
Check more source code and informations about servo and sensor on Raspberry: MY GitHub
Try "sudo ./servo"
Hope it will work.
I can't figure out why the returned value of digitalRead() is 0 (LOW), even with the code below.
Any idea?
void setup(){
Serial.begin(9600);
pinMode(4,INPUT);
}
void loop(){
digitalWrite(4,HIGH);
Serial.println(digitalRead(4));
}
Thanks
According to the Arduino Digital Pins documentation at http://arduino.cc/en/Tutorial/DigitalPins the digitalWrite(4, HIGH) on an input pin does not set the level of the pin. Because of the pinMode(4, INPUT), the digitalWrite(4, HIGH) turns on the processor's internal pullup resistor on pin 4.
digitalRead(4) will show HIGH or LOW, depending on what you have connected to pin 4. If you have nothing connected to pin 4, digitalRead(4) should always return HIGH because the digitalWrite(4, HIGH) connected the internal pullup resistor to it.
So the problem is in the circuit connected to the Arduino; it's not a software problem. I recommend you follow the advice of #ouah and use a multimeter, oscilloscope, or logic pen to find what the voltage is on pin 4, then debug your circuit connected to pin 4 to find out why that pin is low.
Also, I recommend you move the digitalWrite(4, HIGH) to setup() right after the pinMode(4, INPUT), because it is configuring the pin, and needs to be done only once.