Why is my PIC not blinking its leds? - c++

I am trying to get started with pic24's, specifically the PIC24FJ64GA002, and I have looked in the datasheet at the registers and whatnot, but I still cannot get it to blink the leds. When I run it via debug it runs correctly, but when I try to actually run it on the pic it seems to not run at all.
I am using an external Oscillator, a 8MHZ Oscillator specifically, connected to pins 9(OSCI) and 10 (OSCO). Compiler is C30 in Mplab.
Datasheet link is: http://ww1.microchip.com/downloads/en/DeviceDoc/39881D.pdf
The code is below
//include basic header definition
#include <p24FJ64GA002.h>
//config
_CONFIG2(0x0200);
_CONFIG1(0x0800);
int i;
//main loop
int main(void)
{
OSCCON = 0x2280; //select external OSC, no PLL
AD1PCFG = 0xFFFF; //set to all digital I/O
TRISA = 0x0000; //configure all PortA as output
while(1) //Loop forever
{
LATAbits.LATA0 = 1; //RA0 = 1
Wait();
LATAbits.LATA0 = 1; //RA0 = 1
Wait();
}
}
int Wait(void) // gives me a nice delay of 1/3rd a second or so
{
for (int i = 0; i < 30000; i++)
{
for (int i = 0; i < 30; i++);
}
}

You need to go hi, wait, then lo, wait... you are just going hi, wait, hi, wait.
while(1) //Loop forever
{
LATAbits.LATA0 = 1; //RA0 = 1
Wait();
LATAbits.LATA0 = 0; //RA0 = 1
Wait();
}

What optimization level are you compiling with? If it's only working in debug, it's possible the optimizer is reducing the whole Wait() function to a no-op. Try declaring `volatile int i'.

Thank you guys for all your help, but it was that I set the config bits wrong, when I set them in the config editor in mplab all works well.
Thank you for all your help!

Related

Arduino serial monitor lagging and cant get it to work

I wrote this program to check some things m but for me it is not working
` #define outputA 7
#define outputB 3
int counter = 0;
int aState;
int aLastState;
int StateAreTheSamePrinted = 0;
int StatePrinted = 0;
void setup() {
// put your setup code here, to run once:
pinMode(outputA, INPUT);
pinMode(3, INPUT);
Serial.begin(115200);
aLastState = digitalRead(outputA);
Serial.print("Arduino started");
}
void loop() {
// put your main code here, to run repeatedly:
aState = digitalRead(outputA);
if(StatePrinted == 0)
{
Serial.print(aState);
StatePrinted = 1;
}
if(aState == aLastState && StateAreTheSamePrinted == 0)
{
Serial.print("States are the same");
StateAreTheSamePrinted = 1;
}
if(aState != aLastState)
{
if(aState == 1){
Serial.print("A is high \n");
}
else
{
Serial.print("A is low \n");
}
StateAreTheSamePrinted = 0;
StatePrinted = 0;
}
aLastState = aState;
}
It always prints once arduino started, 1 (state of input), and states are the same, when I wire 5v from arduino into port 7, sometimes it react once, sometimes n ot, after few minutes it start priting output like 50-100 lines of messages and stops and lags again. Does anyone got into this problem?
`
I was expecting that after giving power into arduino 7 port it would print A is high or A is low and toggle between them
Sounds like you need to "debounce" after the state changes. When you hook 5v into the pin, it might not make a stable connection right away so it changes state very quickly a few times. A simple fix is to put a ~10ms delay after it detects a state change.

why does my serial print twice on Arduino IDE via Serial Monitor?

I have only put in one 'print command' however I get two print reads.
The programme drives two stepper motors.
The moveSteps value = 48
The motor stops briefly and print's '48' when the programme starts to run and then '48' again when the if is triggered, just before the programme ends.
Only one '48' should be printed. Any ideas why this happens?
/*
Precise movement with stop
Moves the robot 20mm forwards and 20mm backwards
Rob Miles (edited by Dileepa Ranawake)
April 2017
Version 1.0
*/
int motorDelay;
byte left1,left2,left3,left4;
byte right1,right2,right3,right4;
float wheelDiameter = 68.5;
float stepsPerRevolution = 512;
float mmsPerStep = (wheelDiameter * 3.1416) / stepsPerRevolution;
int moveCount;
int moveSteps; // number of steps the motor is to move
void leftForwards()
{
left1=7; left2=6; left3=5; left4=4;
}
void leftReverse()
{
left1=4; left2=5; left3=6; left4=7;
}
void rightForwards()
{
right1=8; right2=9; right3=10; right4=11;
}
void rightReverse()
{
right1=11; right2=10; right3=9; right4=8;
}
int calculateDistanceSteps(float distanceInMM)
{
return distanceInMM / mmsPerStep + 0.5;
}
void setup() {
leftForwards();
rightForwards();
pinMode(left1,OUTPUT);
pinMode(left2,OUTPUT);
pinMode(left3,OUTPUT);
pinMode(left4,OUTPUT);
digitalWrite(left1,HIGH);
pinMode(right1,OUTPUT);
pinMode(right2,OUTPUT);
pinMode(right3,OUTPUT);
pinMode(right4,OUTPUT);
digitalWrite(right1,HIGH);
motorDelay=1200;
moveCount=0;
moveSteps = calculateDistanceSteps(20);
Serial.begin(9800);
}
void loop() {
moveCount = moveCount + 1;
if (moveCount==moveSteps)
{
digitalWrite(left1,LOW);
digitalWrite(right1,LOW);
Serial.println(moveCount);
exit(0);
}
digitalWrite(left2,HIGH);
digitalWrite(right2,HIGH);
delayMicroseconds(motorDelay);
digitalWrite(left1,LOW);
digitalWrite(right1,LOW);
delayMicroseconds(motorDelay);
digitalWrite(left3,HIGH);
digitalWrite(right3,HIGH);
delayMicroseconds(motorDelay);
digitalWrite(left2,LOW);
digitalWrite(right2,LOW);
delayMicroseconds(motorDelay);
digitalWrite(left4,HIGH);
digitalWrite(right4,HIGH);
delayMicroseconds(motorDelay);
digitalWrite(left3,LOW);
digitalWrite(right3,LOW);
delayMicroseconds(motorDelay);
digitalWrite(left1,HIGH);
digitalWrite(right1,HIGH);
delayMicroseconds(motorDelay);
digitalWrite(left4,LOW);
digitalWrite(right4,LOW);
delayMicroseconds(motorDelay);
}
Serial Monitor prints out 4848
I have also noticed that just opening serial monitor makes the stepper motor move!
Using exit() with an Arduino is not standard. It basically disables all interrupts and enters an infinite loop. You could restructure your loop() like this to avoid it:
void loop()
{
// Still moving?
if (moveCount < moveSteps) {
moveCount = moveCount + 1;
// Move complete
if (moveCount == moveSteps)
{
digitalWrite(left1,LOW);
digitalWrite(right1,LOW);
Serial.println(moveCount);
}
else {
digitalWrite(left2,HIGH);
digitalWrite(right2,HIGH);
//etc.....
}
}
}
Also, your loop delays 1200µs 8x. That's only 1200 × 8 = 9600 µs = 9.6 ms. If moveSteps = 48 then the entire loop will only take 460.8 ms. The program is running once before you open the serial monitor then a second time after. What happens if you push the reset button after you've opened the Serial Monitor?
Have you considered using the Arduino's built in Stepper Library?
Lastly, in the future, consider posting questions like this at [arduino.se].

How to communicate boolean values between programs from Arduino and Console

I am looking to communicate boolean values to and from two programs on arduino and windows console. I am working in C++ and using Visual Studio on the computer end and the standard arduino version of C++ on the arduino. All searches I have tried led me to using a windows form and a button on UI. I want this to all be done automatically and have both programs wait until the other has iterated. I've tried some Serial Port samples but none of them have worked so far for one reason or another.
This is the console code I have:
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;
const int PIXEL_X = 1153;
const int PIXEL_Y = 636;
const int RED = 0;
const int BLUE = 50;
const int GREEN = 200;
const int TOLERANCE = 30;
int main(void) {
COLORREF color;
HDC hDC;
do {// Get the device context for the screen
Sleep(50);
hDC = GetDC(NULL);
// Retrieve the color at that position
color = GetPixel(hDC, PIXEL_X, PIXEL_Y);
// Release the device context again
ReleaseDC(GetDesktopWindow(), hDC);
printf("%i %i %i", GetRValue(color), GetGValue(color), GetBValue(color));
int flag = 0;
if (fabs(GetRValue(color) - RED) > TOLERANCE) {
flag += 1;
}
if (fabs(GetGValue(color) - GREEN) > TOLERANCE) {
flag += 1;
}
if (fabs(GetBValue(color) - BLUE) > TOLERANCE) {
flag += 1;
}
if (flag >= 2) {
return 0;
}
//TODO 1: Send Serial data to continue and wait for return statement
} while (true);
return 0;
}
EDIT: This is my arduino code
#include <Servo.h>
Servo myServo;
Servo myServo2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myServo.attach(2);
myServo2.attach(3);
}
void A_button() {
myServo.write(125);
delay(1000);
myServo.write(97);
delay(250);
}
void L_button() {
myServo2.write(72);
delay(1000);
myServo2.write(90);
delay(250);
}
void loop() {
for(int i=0; i < 16; i++) {
A_button();
Serial.print("1 ");
delay(1500);
}
delay(3000);
// TODO 2: send Serial data
L_button();
Serial.print("0 ");
}
EDIT: Thank you for the response. I want the console application code to wait until the arduino code goes to TODO 2, then the console application runs until TODO 1 and checks if the RGB values are in their tolerances. If the RGB values are within the tolrance, then I want the arduino code to continue iterating until the TODO 2 again and repeat. I don't have any C++ code for the serial communication because none of the examples I've tried have compiled, and I can't use a windows form as I understand it.
I want to have the code wait at the TODO, but I have absolutely no idea how to do this and would appreciate any help. I will elaborate if any needs me to.
Thank you in advance.

What does the wait() function do?

Hi I started learning Java and am currently trying to learn C++. I have this piece of code and cannot workout what it does. I am assuming it makes the program wait for a certain period of time before it starts. But some further explanation would very useful.
I have added comments to sections for which I would like some further explanation.
for (;;) {
wait (0.02); //What does this do?
if (ab1_On) {
con += 104;
ab1_On = 0; //Why is the value reset to 0?
}
if (ab2_On) {
con += 208;
ab2_On = 0; //Why is the value reset to 0?
}
con++;
if (con > 311) {
con -= 312;
}
for (int i=0; i<3; i++) {
bright[i] = brilvl (con + (i * 104));
}
}
}
wait() is a function defined in the mbed SDK.
https://developer.mbed.org/handbook/Wait
In your program wait(0.02) will block execution for 20 milliseconds.
for (;;) is an infinite loop, it will run forever. The wait() may be being used to prevent the effects of switch bounce if ab1_on and ab2_on are being set by some mechanical switch.

OpenFrameworks/C++/Arduino: UDP SendAll fails at 1473 chars

am sending char * from OF to a teensy board.
Here is my OF code:
void ofApp::draw() {
string message = "";
int total = 1472;
for (int i = 0; i < total; i++) {
message += (char)ofRandom(0,255);
}
udpConnection.SendAll(message.c_str(), message.length());
}
Here is my teensy code:
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.println("Got it!");
Udp.read((char*)packetBuffer, 648*3);
for (int i = 0; i < 3*NUM_LEDS; i+=3) {
leds[i/3].setRGB(packetBuffer[i], packetBuffer[i+1], packetBuffer[i+2]);
}
FastLED.show();
}
}
The teensy code responds when it receives a packet of any size, which works fine up to 1472 chars. In the OF code, as soon as the length of the char * length is increased to 1473, the teensy stops receiving anything, and I am not getting any run time errors OF-side. Does anyone know why this would happen/what the fix is? I need to scale this up to 1944 chars eventually
Thanks,
Collin
What's the MTU size? 1473 sound close to the default.
You may do some experiment by increase the MTU on both side.